How to disable module hooks for certain controllers in Prestashop?

3.4k views Asked by At

I'm writing my own module and the essential option is to control controller from the module options.

I know how to control tpl and js via module options but I can't get the way to control Prestashop controller from the module php file.

Simply I want to know the way how to do it.

I want to have four checkboxes with options to enable or disable module in four controllers like index, cms, category, product.

I have right now:

$values = array('index','product','cms','category');
 if(in_array(Tools::getValue('controller'), $values)){
 return $this->display(__FILE__, 'mymodule.tpl');
}

And this code display tpl file content in this four controllers in homepage (index), cms, category and the product pages. But how to put there some trigger to enable/disable values from the array?

1

There are 1 answers

4
gskema On BEST ANSWER

Make a configuration field for the controllers:

public function getContent()
{
    if (Tools::isSubmit('MY_CONTROLLERS_LIST')) {
        Configuration::updateValue('MY_CONTROLLERS_LIST', (string)Tools::getValue('PS_MY_CONTROLLERS'));
    }

    $value = Configuration::get('MY_CONTROLLERS_LIST');

    return '<form action="" method="POST"><input name="PS_MY_CONTROLLERS" value="'.$value.'"><input type="submit" value="Save"></form>';
}

public function hookDisplayTop()
{
    $value = Configuration::get('PS_MY_CONTROLLERS');
    $controllers = explode(',', $value);

    if(in_array(Tools::getValue('controller'), $controllers)){
        return $this->display(__FILE__, 'mymodule.tpl');
    }

    return false;
}

This will appear in Modules > Modules > Configure (of your module). There are form helpers which can render PrestaShop forms, but this is simplified example showing that you will need aform that submits to the same page.

Controllers value for this example should be index,category,cms,product.

Another way

Go to Modules > Positions Find your hook (for example displayTop) and your module, click Edit. Then selected the pages where you don't want to show the block.

Generating Options Form

The correct way (but more complicated) would be to generate the form using HelperForm or HelperOptions classes that come with PrestaShop