addJS function not working for admin in prestashop

I am trying to add javascript file in prestashop admin using backOfficeHeader hook using a module but nothing happened. My code is given below.

public function install()
{
    if (!parent::install()
        || !$this->registerHook('backOfficeHeader'))
        return false;

    return parent::install() && 
     $this->registerHook('backOfficeHeader');
}

 public function hookBackOfficeHeader() {
   $this->context->controller->addJS(_MODULE_DIR_.$this->module->name.'js/hs_custom.js');
 }


ANSWERS:


If you are using PS 1.5 or 1.6 you should use hook "actionAdminControllerSetMedia".

Your module installer should check which prestashop version is used and then register the needed hook.

 if (version_compare(substr(_PS_VERSION_, 0, 3), '1.5', '<'))
      $this->registerHook('BackOfficeHeader');
 else
      $this->registerHook('actionAdminControllerSetMedia');

Then you need to addJS on each hook in its version format: PS>=1.5

 public function hookActionAdminControllerSetMedia($params) { 
     $this->context->controller->addJS($this->_path.'views/js/hs_custom.js');
 }

PS<=1.4

public function hookBackOfficeHeader($params) { 
    Tools::addJS($this->_path.'views/js/hs_custom.js');
}

did u try to check addJS path? I think nothing more can be possible if other JS files working. Try to use $this->_path.

$this->context->controller->addJS($this->_path.'views/js/hs_custom.js');

1) Output path and check if it is valid. 2) Reload page and check network. Page load your script or not? 3) Remember to reset module if u change something with hooks. 4) Check module hooks.


For me "this->_path" dosn't work. My solution is to use $_SERVER['DOCUMENT_ROOT']

public function hookActionAdminControllerSetMedia($params)
{
    // add necessary javascript to products back office
    if($this->context->controller->controller_name == 'AdminProducts' && Tools::getValue('id_product'))
    {
        $this->context->controller->addJS($_SERVER['DOCUMENT_ROOT']."/modules/apl/views/js/jquery.ui.touch-punch.min.js");
    }
}


 MORE:


 ? Prestashop Admin panel Menu's Not visible
 ? Starter Theme Prestashop 1.7 is blank
 ? Starter Theme Prestashop 1.7 is blank
 ? Starter Theme Prestashop 1.7 is blank
 ? Prestashop1.6 theme on prestashop 1.7
 ? Prestashop 1.7 Installation Ubuntu 14.04 LAMP Too many redirects
 ? Prestashop 1.7 Update Error
 ? Prestashop 1.7 unable to save order address
 ? Prestashop 1.7 unable to display custom module
 ? Prestashop 1.7 stock manager option not showing