Target specific pages from a Prestashop Module’s Hook

You can easily restrict a Prestashop module’s display by adding exceptions in the back office positions. However, if you automate the process, there is a small trick that can be used when creating a module’s hook, which will also open a whole set of new possibilities to display page-specific content.

The php_self context variable

Each Prestashop page has its own name, setup in the appropriate controller. It’s the body id assigned to a page as the $page_name variable in smarty. However, if you tried to get the page name from within a module, you might have noticed it’s only assigned to smarty. So is there a way to target each specific page from within a module? Yes, indeed. Here is the code:

$this->context->controller->php_self

It can be really useful if you only want to target specific pages within a module. Let’s see a practical example.

Target specific pages from a module’s hook

Say, for the only sake of this example, that we only want to display a top of pages module if the current page is the homepage. Let’s use blocksearch.

In this case, We would only need to edit blocksearch.php, locate the hookTop method:


	public function hookTop($params)
	{
		if (!$this->isCached('blocksearch-top.tpl', $this->getCacheId('blocksearch-top')))
		{
			$this->calculHookCommon($params);
			$this->smarty->assign('blocksearch_type', 'top');
		}

		return $this->display(__FILE__, 'blocksearch-top.tpl', $this->getCacheId('blocksearch-top'));
	}

And add this at the beginning of the function:

if('index' != $this->context->controller->php_self)
	return false;

We a re simply telling it: if the current page is not the homepage, kill the module so it doesn’t display anything.

Just think at the possibilities at this point: you can display different content, and do diffrent things from the same hook, depending on the current page.

How to? First, decide the pages that need specific behaviors. Use var_dump($this->context->controller->php_self) to check each page’s exact name. Then simply add a switch or if operator, that checks the current page name and acts properly each time, like:

if('index' == $this->context->controller->php_self)
	// Do something and display a template for the homepage
else if('category' == $this->context->controller->php_self)
	// Do something and display a template if we are in the category page
else // not in any of the 2 pages above
	return false: // don't display anything

You like the tuts and want to say "thank you"? Well, you can always feel free to donate:

  • andreas

    i love you man… been trying to unhook some module from unregistered unhook option. thanks man

  • Anto

    Nemo please.

    If statement for one category id ¿?

    Thanks

    • NemoPS

      For the category id itself, Tools::getValue(‘id_category’) == THE_ONE_TO_CHECK

      • Anto

        I try this and no works

        Tools::getValue(‘id_category’) !=4 $this->context->controller->php_self)

        return false;

        Parse error: syntax error, unexpected T_VARIABLE

        very thanks nemo

      • Anto

        This is the statement

        public function hookDisplayBanner($params)

        {

        if ( Tools::getValue(‘id_category’) == 4 $this->context->controller->php_self)

        return false;

        }

        return $this->hookDisplayTop($params);

        }

        but no works
        very thanks for the support

  • Adrian

    i have this error on payment module (bankwire and cashondelivery)

    Notice: Undefined property: BankwirePaymentModuleFrontController::$php_self

  • http://www.vintage-adventures.com Jim

    I answered my own question about PS version 1.4. Here is a modification to the hookHeader function that does the following things:

    1. If for any reason PHP_SELF is not set, then go ahead and load the CSS (just in case the user is on home page).
    2. If website exists on a subdirectory, then the variable __PS_BASE_URI__ will handle parsing off the subdirectory from the haystack search target string. This is not really needed, though, since the function looks anywhere in the target string, not just at the beginning of the target string.
    2. slash is included in search string, to help add specificity and help avoid garbage URLs from triggering CSS to load.
    3. index.php will be found if it occurs anywhere in the search target string (if ‘index.php’ is prefaced by a slash). This is useful for using this function on other types of pages, where the .php file occurs later in the path.
    4. Variations of this function could include an array with a FOREACH loop, that limits CSS loading to specific pages, or prevents CSS from loading on the pages defined in the array.
    5. The IF statement must be changed to account for excluding pages (rather than including pages). Excluding or including the desired action from CATALOG_MODE can also be handled in the IF statement.

    The code:


    public function hookHeader($params)
    {
    $php_self = $_SERVER['PHP_SELF'];
    $haystack = substr($php_self, strlen(__PS_BASE_URI__)-1);
    $needle = '/index.php';
    $position = strpos($haystack, $needle);

    if (isset($php_self) && $position === false)
    return;

    Tools::addCSS(($this->_path).'homefeatured.css', 'all');
    return;
    }

  • http://www.vintage-adventures.com Jim

    How to do this in Prestashop version 1.4? ‘context’ is undefined in 1.4..

  • http://www.lancastrian-it.co.uk/ shoulders

    this is an excellent nugget of information. I am going to use it to disable address editing for client who have not reached a certain spend threshold.

  • doat

    Yes, this works fine! Thank you very much!
    And sorry, i didn’t see your answer before.
    if ('3'== Tools::getValue('id_cms'))

  • http://nemops.com Nemo

    Should be Tools::getValue(‘id_cms’);

    Of course this only works in cms pages
    :)

  • doat

    Hi,
    this really works great!
    but i’m trying to target a specific cms page and i can’t find the way to write its name.
    var_dump gives me: string(3)”cms”
    so I think there is a pb with the brackets, i can’t resolve.
    Do you have an idea?

    • http://nemops.com Nemo

      In that case you need to target the cms id :)

      • doat

        yes! my question is how to?

        • doat

          am i on the good way with these research?

          if ('3' == $this->context->id_cms->php_self)
          if ('3' == $this->context->cms->id)

Store Top Sales

You like the tuts and want to say "thank you"? Well, you can always feel free to donate: