Display module output anywhere

Creative Commons Licence

I came across a post on the Prestshop forum recently that was discussing how to display the contents of a single module on a page. The example proposed worked, but it seems to me that it worked only through sheer luck rather than design as all that was being done was to display a smarty .tpl file. The smarty variables required for the module used in the example were all included as global values, so it worked, but most other modules would need to be able to execute their underlying hook code in order to display anything meaningful.

For those who would like the flexibility of adding the contents of a module anywhere they like regardless of Prestashop’s hook locations, here’s how I propose it should be done:

// Set up some variables to make the code clearer
// Any module supporting the hook named below
$modulename = 'homefeatured';
// Set the following to any of the hook names
// as stored in the ps_hooks table.
$hookname = 'home';
// Make a proper function name using
// the Prestashop convention
$hookFunction = 'hook'.$hookname;
// Finally, some hooks expect parameters to be passed
$hookArgs = array();
// Perform the magic
$moduleInstance = Module::getInstanceByName($modulename);
if (is_callable(array($moduleInstance, $hookFunction)))
  echo call_user_func(array($moduleInstance,$hookFunction), $hookArgs);
else
  echo 'Well that didn\'t work!';

Note that this will only work on a page that has correctly included the necessary Prestashop core code (/config/config.inc.php at a minimum, plus either the standard header.php or init.php depending on whether you wish to display the output in a standard page or not).

As an aside to the above, it’s also possible to implement any additional public member function in a module class definition and then call it using the above code ($hookFunction being the full name of the function to be called). This is handy should you wish to write modules that could for example also include some code to be executed externally via cron.

A full example that generates some output follows, although note that there’s no css included, so it may not look too pretty! It can be called anything, but should be uploaded to the root folder of your store in order to allow it to find the include files.

include('config/config.inc.php');
include('init.php');
 $modulename = 'homefeatured';
 $hookname = 'home';
 $hookFunction = 'hook'.$hookname;
 $hookArgs = array();
 $moduleInstance = Module::getInstanceByName($modulename);
 if (is_callable(array($moduleInstance, $hookFunction)))
   echo call_user_func(array($moduleInstance, $hookFunction), $hookArgs);
 else
   echo 'Well that didn\'t work!';

Sharing is caring!

Posted on January 2, 2010 | Related Categories: Development Resources, Tips and Tricks | 7 comments

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

7 Responses to Display module output anywhere

  1. Pingback: Tired of hooks? Try a Plugin (Prestashop 1.4) | Development Resources

  2. Dux says:

    I have been toying with the idea of a more or less total reconstruction of the front office, without completely hacking apart the core files, and how it handles both this and that. Being able to display what you want, where you want it, being a big part of what I seek to accomplish. I am absolutely no professional programmer though, and this might just have put me on a track to actually start puzzling something together to atleast partially accomplish what I have in mind.

  3. Jane says:

    Hello, could please tell me in which file should I put the first code?
    Thanks.

  4. guest says:

    where the heck do you put the code in?

  5. Dude says:

    Hi, very interesting post, I’m quite interested in this kind of technique!
    I’m using a MVC php framework to display all the parts of my website with my custom cms, I just need to output some part of prestashop into my already built website !
    I think that it’s what you achieve here ! I’m not too sure that I have understand how it works !! Could you explain a bit more and maybe throw a test page..

    It could be a very interesting path to follow…

    Thanks.

  6. Sorry that the code is wrong, i think this is much better but still didnt work also

    $modulename = ‘statscatalog’;
    $hookname = ‘backOfficeHome’;
    $hookFunction = ‘hook’.$hookname;
    $hookArgs = array();
    $moduleInstance = Module::getInstanceByName($modulename);
    if (is_callable(array($moduleInstance, $hookFunction)))
    echo call_user_func(array($moduleInstance, $hookFunction), $hookArgs);
    else
    echo ‘Well that didn\’t work!’;

  7. i haved tried to put the stats catalog module and display it on the backoffice welcome page. But i`ve no luck.

    echo Module::hookExec(‘backOfficeHome’);

    $modulename = ‘statscatalog’;
    $hookname = ‘adminCustomers’;
    $hookFunction = ‘hook’.$hookname;
    $hookArgs = array();
    $moduleInstance = Module::getInstanceByName($modulename);
    if (is_callable(array($moduleInstance, $hookFunction)))
    echo call_user_func(array($moduleInstance, $hookFunction), $hookArgs);
    else
    echo ‘Dont works!’;