Jump to content

[SOLVED] Prestashop 1.7.6.4 custom module adding js and css


Recommended Posts

Hello,

I did a custom module for prestashop 1.7.6.4, simplest to learn, and works. My module has one page .twig (modules\ps_import\views\templates\admin\import.html.twig), now I need to add one JS and one CSS, but I don't find the correct documentation online.

Can you help me to find the documentation or can you give me a link to github example?

tnx in advance

Edited by massimopasquali (see edit history)
Link to comment
Share on other sites

Hi, I saw your example and I would ask you:
I read on prestashop doc that addcss is deprecated, but you in your had used addCss. Is corret used addCss, there is onother way?

public function hookDisplayBackOfficeHeader()

{

// Use addCss : registerStylesheet is only for front controller.

$this->context->controller->addCss(

$this->_path.'views/css/fbsample_botraining.css'

);

}

Link to comment
Share on other sites

Ok I have done some tests and if I try to register my hook into the Install method It doesn't work, also the package is istalled. The JS is like not exist:


    public function install()
    {
        if (Shop::isFeatureActive()) {
            Shop::setContext(Shop::CONTEXT_ALL);
        }

        if (!parent::install() ||
            !$this->registerHook('leftColumn') ||
            !$this->registerHook('header') ||
            !Configuration::updateValue('IMPORT_DATA_NAME', 'ps_importdata')
        ) {
            return false;
        }

        return parent::install()
        && $this->installTab()
        && $this->hookDisplayBackOfficeHeader();
    }


    private function installTab()
    {
        $tabId = (int) Tab::getIdFromClassName('ImportDataPantheraController');
        if (!$tabId) {
            $tabId = null;
        }

        $tab = new Tab($tabId);
        $tab->active = 1;
        $tab->class_name = 'ImportDataPantheraController';
        $tab->name = array();
        foreach (Language::getLanguages() as $lang) {
            $tab->name[$lang['id_lang']] = 'Import Data Panthera';
        }
        $tab->id_parent = (int) Tab::getIdFromClassName('AdminParentModulesSf');
        $tab->module = $this->name;
        return $tab->save();
    }

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

INSTEAD IF I REGISTER MY HOOT LIKE THAT IT'S WORK

 public function install()
    {
        if (Shop::isFeatureActive()) {
            Shop::setContext(Shop::CONTEXT_ALL);
        }

        if (!parent::install() ||
            !$this->registerHook('leftColumn') ||
            !$this->registerHook('header') ||
            !Configuration::updateValue('IMPORT_DATA_NAME', 'ps_importdata')
        ) {
            return false;
        }

$this->hookDisplayBackOfficeHeader()

        return parent::install()
        && $this->installTab();
    }

Can you tell me why?

tnx in advance

 

Link to comment
Share on other sites

When you register your module on a hook, the function hookXXXX with XXX corresponding to the name of the hook is automatically called by PrestaShop when the hook is triggered.

DispalyBackOfficeHeader is triggered when PrestaShop generates the <head></head> section of a page on the back office. The addJS function allows you to add a js file to the page. 

If you call the addJS function when installing the module, the addition of the JS file is not taken into account since the header of the page is not generated after but before the execution of the install() function.

 

Link to comment
Share on other sites

  • 2 weeks later...
  • 1 year later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...