PrestaShop Forum

The best place in the world to ask questions about PrestaShop and get advice from our passionate community!

PrestaShop Forum

Jump to content

 

[SOLVED] How to create a new hook in 1.5? Am I doing it wrong?

hook modules register
40 replies to this topic
#21
Fabien Serny

    PrestaShop Fanatic

  • Members
  • PipPipPipPip
  • 1481 posts
No problem, so as I said, this only work in 1.5.
In one of your template, just add the hook you want

For example, index
{hook h='displayMyNewHook'}

Then, you can create one (or more) module with this method :
/modules/mymodule/
/modules/mymodule/mymodule.php

Content of mymodule.php

class MyModule extends Module
{
    function __construct()
    {
        $this->name = 'mymodule';
        $this->tab = 'advertising_marketing';
        $this->author = 'Your name';
        $this->version = '1.0';
        $this->need_instance = 0;

        parent::__construct();

        $this->displayName = $this->l('My Module');
        $this->description = $this->l('Example of module');
    }

    public function install()
    {
	   // Here the registerHook method will check if the hook exists, if not it will add it to table ps_hook
        return (parent::install() AND $this->registerHook('displayMyNewHook');
    }

    public function hookDisplayMyNewHook($params)
    {
	   return 'myDisplay';
    }
}


Now, you have "myDisplay" displayed where you put your hook.
If you want to see this hook in your adminpanel to have the possibility to change order of module which are hooked on.
You will have (unfortunately) to go to you phpmyadmin and set the flag "position" to 1 in the table ps_hook for this hook.

Tell me if something is not clear :)
[b]Fabien Serny[b] • Developer | DéveloppeurPrestaShop | Twitter

#22
smartdatasoft

    PrestaShop Apprentice

  • Members
  • PipPip
  • 65 posts

View PostFabien Serny, on 02 February 2013 - 04:32 PM, said:

No problem, so as I said, this only work in 1.5.
In one of your template, just add the hook you want

For example, index
{hook h='displayMyNewHook'}

Then, you can create one (or more) module with this method :
/modules/mymodule/
/modules/mymodule/mymodule.php

Content of mymodule.php

class MyModule extends Module
{
	function __construct()
	{
		$this->name = 'mymodule';
		$this->tab = 'advertising_marketing';
		$this->author = 'Your name';
		$this->version = '1.0';
		$this->need_instance = 0;

		parent::__construct();

		$this->displayName = $this->l('My Module');
		$this->description = $this->l('Example of module');
	}

	public function install()
	{
	   // Here the registerHook method will check if the hook exists, if not it will add it to table ps_hook
		return (parent::install() AND $this->registerHook('displayMyNewHook');
	}

	public function hookDisplayMyNewHook($params)
	{
	   return 'myDisplay';
	}
}


Now, you have "myDisplay" displayed where you put your hook.
If you want to see this hook in your adminpanel to have the possibility to change order of module which are hooked on.
You will have (unfortunately) to go to you phpmyadmin and set the flag "position" to 1 in the table ps_hook for this hook.

Tell me if something is not clear :)
yes it work. We have build custom hook like this way last month for out one great module.

#23
shoulders

    PrestaShop Apprentice

  • Members
  • PipPip
  • 357 posts

View PostFabien Serny, on 31 January 2013 - 04:12 PM, said:

The fact that you do not see it in your back office, it's because you have to set the flag "position" to 1 in your table.
(I forgot to set this flag when I coded it).

Has this flag been added yet? Justs so i know.

tar

#24
Fabien Serny

    PrestaShop Fanatic

  • Members
  • PipPipPipPip
  • 1481 posts
What do you mean by "added" ?
This flag is existing for a while (even in PrestaShop 1.4). It's just not set to value 1 when you create it by registering a module on it.
[b]Fabien Serny[b] • Developer | DéveloppeurPrestaShop | Twitter

#25
shoulders

    PrestaShop Apprentice

  • Members
  • PipPip
  • 357 posts
Hi Fabien

what i ment was, when a new hook, is registered in the new way, earlier in the thread you said:

Quote

For making the hook appear in you admin panel, you just have to set the flag "position" to 1 in your database.
and then you said

Quote

The fact that you do not see it in your back office, it's because you have to set the flag "position" to 1 in your table.
(I forgot to set this flag when I coded it).

Were you going to alter the registerHook function to set the positional flag to 1.

I know we can use the 'Display Non Positional Hooks' option in the backend

Edited by shoulders, 04 March 2013 - 05:35 PM.


#26
Fabien Serny

    PrestaShop Fanatic

  • Members
  • PipPipPipPip
  • 1481 posts
Hi,

Sorry I did not understand your question at first ^^
I'm not working at PrestaShop anymore (since last july). So no, I am not going to alter the registerHook function.
But you should ask for it on GitHub or on the forge.
[b]Fabien Serny[b] • Developer | DéveloppeurPrestaShop | Twitter

#27
shoulders

    PrestaShop Apprentice

  • Members
  • PipPip
  • 357 posts
i will get this added to the forge.

#28
xezus

    PrestaShop Apprentice

  • Members
  • PipPip
  • 27 posts
Hi,
I used this method of creating hooks but I can't display anything in this new created hook. I want display a homeslider in my new hook, but I getting message: "This module cannot be transplanted to this hook."

I added my new hook into center of page

<!-- Center -->
<div id="center_column" class=" grid_5">
<div class="sliderPlease">
{hook h='sliderHook'}
</div>

then I added it into the install method in the homeslider.php

if (parent::install() && $this->registerHook('displayHome') && $this->registerHook('actionShopDataDuplication') && $this->registerHook('sliderHook'))

And at the bottom of the homeslider.php I added hook function

public function sliderHook($params)
{
	if(!$this->_prepareHook())
return;

// Check if not a mobile theme
if ($this->context->getMobileDevice() != false)
return false;

$this->context->controller->addJS($this->_path.'js/jquery.bxSlider.min.js');
$this->context->controller->addCSS($this->_path.'bx_styles.css');
$this->context->controller->addJS($this->_path.'js/homeslider.js');
return $this->display(__FILE__, 'homeslider.tpl');
}
But It's not working. The hook is installed and I can see it in BO, but I can't hook anything inside it.
Please could somebody tell my what I do wrong????

Edited by xezus, 27 March 2013 - 11:39 PM.


#29
Fabien Serny

    PrestaShop Fanatic

  • Members
  • PipPipPipPip
  • 1481 posts
Hi Xezus,

It's because you have to rename your method hookSliderHook() instead of sliderHook() :)

Edited by Fabien Serny, 28 March 2013 - 08:24 PM.

[b]Fabien Serny[b] • Developer | DéveloppeurPrestaShop | Twitter

#30
xezus

    PrestaShop Apprentice

  • Members
  • PipPip
  • 27 posts
Omg, Thanks a lot! You saved me a hours of my live :)

#31
kalai20078

    PrestaShop Apprentice

  • Members
  • PipPip
  • 26 posts
Hello Fabien,

This is a useful topic.

I Want to hook a module in my CMS page how can i do this with the help of your guide here.
If you can guide it would be great...

Thanks...

#32
smartdatasoft

    PrestaShop Apprentice

  • Members
  • PipPip
  • 65 posts
you can read this post

http://www.prestasho...-into-cms-page/

There you will find more details. If you need more help inform me.

#33
kalai20078

    PrestaShop Apprentice

  • Members
  • PipPip
  • 26 posts
Hi smartdatasoft

Thankyou for your response i actually followed the post you have give in your response, still i cant make it work i have added a post in the below link the same ill past it here...

http://www.prestasho...ge/page__st__20

any help would be greatly appreciated.

Am using PS 1.5.3.1

I want hook a module which already installed in home page and i have duplicated the module uploaded into the modules directory.

I followed exactly the same steps mentioned here in this post as followes

#1. Created a hook entry in PS_Hook table (HOOK_DESIGNMYSHIRT)

#2. In cms.tpl file from Module/Mymodule/cms.tpl i added the following

{if ($cms->link_rewrite == 'designmyshirt')}
{$HOOK_DESIGHNMYSHIRT}
{/if}

#3. Im cms.php file from the root directory i added the following


$smarty->assign(array(
'cms' => $cms,
'HOOK_DESIGHNMYSHIRT' => Module::hookExec('designmyshirt'), // Custom hook for mycmspage
'content_only' => intval(Tools::getValue('content_only'))
));
$smarty->display(_PS_THEME_DIR_.'cms.tpl');

#4. In the modules folder the module php file mymodule.php i added the following

public function install(){
if (parent::install() && $this->registerHook('HOOK_DESIGHNMYSHIRT'))


after at the end of this same file i added the below function

function hookdesignmyshirt($params)
{
if ($xml = $this->_xml)
{
global $cookie, $smarty;
$smarty->assign(array(
'xml' => $xml,
'title' => 'title_'.$cookie->id_lang,
'text' => 'text_'.$cookie->id_lang
));
return $this->display(__FILE__, $this->cms.'.tpl');
}
return false;
}

After this i installed the module and checked in the BO position. the module is displayed in the hook in BO
When i check the CMS page nothing is displayed even if i configure the module from BO.

I checked the Apache error log i get this following error

Notice: Undefined index: HOOK_DESIGHNMYSHIRT in G:\\WAMP Server\\wamp\\www\\rave\\cache\\smarty\\compile\\e2fae568d255bdaa47e61a9405a623865dd4d436.file.cms.tpl.php on line 40, referer: http://localhost/rav...70f78b7d317be98

I want this module to be placed in center of the CMS page...

Have i did everything correctly please help me to solve this issue....


Thanks....

#34
Fabien Serny

    PrestaShop Fanatic

  • Members
  • PipPipPipPip
  • 1481 posts
Hi Kalai,

If you're on PrestaShop 1.5, you do not have to create an entry in ps_hook and you don't have to make modification in cms.php.
You just have to follow what I'm saying on my differents posts in this topic
- Add your hook in cms.tpl {hook h='designMyShirt'}
- Then in your module, add the registerHook('designMyShirt')
- Create in your module the method hookDesignMyShirt()
[b]Fabien Serny[b] • Developer | DéveloppeurPrestaShop | Twitter

#35
Fabien Serny

    PrestaShop Fanatic

  • Members
  • PipPipPipPip
  • 1481 posts
And about your error (even if you did not use the right way), your problem might comme the fact you wrote
$this->registerHook('HOOK_DESIGHNMYSHIRT') instead of $this->registerHook('designmyshirt')
[b]Fabien Serny[b] • Developer | DéveloppeurPrestaShop | Twitter

#36
kalai20078

    PrestaShop Apprentice

  • Members
  • PipPip
  • 26 posts
Thanks Fabien ill give it a try...

#37
kalai20078

    PrestaShop Apprentice

  • Members
  • PipPip
  • 26 posts
Thanks Fabien it works....

#38
the_raven

    PrestaShop Apprentice

  • Members
  • PipPip
  • 52 posts

View PostFabien Serny, on 02 February 2013 - 04:32 PM, said:

No problem, so as I said, this only work in 1.5.
In one of your template, just add the hook you want

For example, index
{hook h='displayMyNewHook'}

Then, you can create one (or more) module with this method :
/modules/mymodule/
/modules/mymodule/mymodule.php

Content of mymodule.php

class MyModule extends Module
{
	function __construct()
	{
		$this->name = 'mymodule';
		$this->tab = 'advertising_marketing';
		$this->author = 'Your name';
		$this->version = '1.0';
		$this->need_instance = 0;

		parent::__construct();

		$this->displayName = $this->l('My Module');
		$this->description = $this->l('Example of module');
	}

	public function install()
	{
	   // Here the registerHook method will check if the hook exists, if not it will add it to table ps_hook
		return (parent::install() AND $this->registerHook('displayMyNewHook');
	}

	public function hookDisplayMyNewHook($params)
	{
	   return 'myDisplay';
	}
}


Now, you have "myDisplay" displayed where you put your hook.
If you want to see this hook in your adminpanel to have the possibility to change order of module which are hooked on.
You will have (unfortunately) to go to you phpmyadmin and set the flag "position" to 1 in the table ps_hook for this hook.

Tell me if something is not clear :)

hi fabien , after awhile i did your instruction and it works fine...thank you
but i have one unknown problem
for example made some changes to blockreinsurance module and hook it to new hook that its made with your instruction , but CSS doesn't load with new hook!
as you know css load with this line :

$this->context->controller->addCSS($this->_path.'style.css', 'all');

it works properly with Footer Hook ( and other hooks too ) but when i use your method css doesnt load and must to define css codes to global css.

i add this div to footer.tpl :

<div class="bankicon">{hook h='BankHook'}</div>

and this is detail of module php :


public function install()
{
return parent::install() &&
$this->installDB() &&
Configuration::updateValue('blockbankicon_nbblocks', 5) &&
$this->registerHook('BankHook') && $this->installFixtures();
}


public function hookBankHook($params)
{

// Check if not a mobile theme
if ($this->context->getMobileDevice() != false)
return false;

$this->context->controller->addCSS($this->_path.'style.css', 'all');
if (!$this->isCached('blockbankicon.tpl', $this->getCacheId()))
{
$infos = $this->getListContent($this->context->language->id);
$this->context->smarty->assign(array('infos' => $infos, 'nbblocks' => count($infos)));
}
return $this->display(__FILE__, 'blockbankicon.tpl', $this->getCacheId());
}

what you think about this issue ?

Edited by the_raven, 12 May 2013 - 12:14 PM.


#39
Fabien Serny

    PrestaShop Fanatic

  • Members
  • PipPipPipPip
  • 1481 posts
Hi Raven,

For what I remember addCSS and addJS only works in hookHeader for 1.4
The loop which write css and js inclusion is in header.tpl and it is displayed before others hooks are called.

But I thought it should work on 1.5 since a layout is present.
I will test it and come back to you.
[b]Fabien Serny[b] • Developer | DéveloppeurPrestaShop | Twitter

#40
the_raven

    PrestaShop Apprentice

  • Members
  • PipPip
  • 52 posts

View PostFabien Serny, on 13 May 2013 - 08:57 AM, said:

Hi Raven,

For what I remember addCSS and addJS only works in hookHeader for 1.4
The loop which write css and js inclusion is in header.tpl and it is displayed before others hooks are called.

But I thought it should work on 1.5 since a layout is present.
I will test it and come back to you.

thank you man..i appreciate you spending time for us..

but as i said, addCSS work's in 1.5 and module load css when i use other hooks , but problem occure only with new Hook that i've created with new method..

for example in this sample :
public function hookFooter($params)
{
css load correctly...

Edited by the_raven, 15 May 2013 - 07:44 AM.