Manikandan
Manikandan

Reputation: 1816

How to add JS for modules in prestashop 1.5?

Am developing a module in prestashop 1.5. Am using displayAdminProductsExtra hook to display tpl file in admin tab. when i include my jquery code in tpl, it works fine. but when i try to make it as new file and include its not working. so far I tried the below methods..

using displayBackOfficeHeader to register a hook and called like this..

public function hookdisplayBackOfficeHeader($params) 
{
    $this->context->controller->addJS(($this->_path).'abc.js');
}

and I tried to add it in displayAdminProductsExtra also like this..

$this->context->controller->addJS(_MODULE_DIR_.$this->module->name.'/views/js/abc.js'); //first tried..
$this->context->controller->addJS(($this->_path).'abc.js','all'); //second tried this..

And I tried with getcontent like this..

public function getContent()
{   
    $this->_html= '<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js">
        <script src="../modules/abc/abc.js" type="text/javascript" ></script>';
    return $this->_html;
}

but these methods didn't add my js file. Dont know where am making mistake.any help would be appreciated.

Upvotes: 2

Views: 11845

Answers (2)

Maksim T.
Maksim T.

Reputation: 178

The hook DisplayHeader (or Header) is for registering assets in a front-office pages!

This is right for your JavaScript path:

$this->context->controller->addJS($this->_path . 'abc.js');

But you didn't register jQuery assets by this method before registering your JavaScript:

$this->context->controller->addJquery();

Also, you should not use the hook DisplayBackOfficeHeader. Instead of it, you should use the hook ActionAdminControllerSetMedia.

Here is the detailed information, how to register JavaScript in a back-office (in admin pages).

Upvotes: 0

esmoreno
esmoreno

Reputation: 666

When you are creating a Prestashop module, you must add the function hookHeader and within it, the line that adds the js in your page.

Would need something like this:

public function hookHeader ($ params)
{
     $ this-> controller-> addJS (($ this-> _path). 'abc.js');
}

On the other hand, looking at the code of the module blockcategories in blockcategories.php file we see the following:

public function displayForm()
{
...
}

This function is to create a page for module configuration, in the same way you use other modules. Maybe it's a simpler option, but faster to develop.

Regards

Upvotes: 4

Related Questions