Simple module hooks

edited April 2014 in Modules
I'm trying to get module hooks to work for simple modules but not having much luck, so I'm wondering whether module hooks only work for advanced modules?

I'm trying to implement before_create_whatsons and after_create_whatsons hooks, but they are not triggered.

I have created a 'Whatsons' class as follows:
class Whatsons_model extends Base_module_model { <removed irrelevant detail...> public function after_create_whatsons($data) { log_message('debug', __FILE__."::after_create_whatsons: date=".print_r($data, 1)); } public function before_create_whatsons($data) { log_message('debug', __FILE__."::before_create_whatsons: date=".print_r($data, 1)); } }

and added the following to application/config/hooks.php:
$hook['before_create_whatsons'] = array( 'class' => 'Whatsons_model', 'function' => 'before_create_whatsons', 'filename' => 'Whatsons_model.php', 'filepath' => 'models', 'params' => array(), 'module' => 'whatsons', ); $hook['after_create_whatsons'] = array( 'class' => 'Whatsons_model', 'function' => 'after_create_whatsons', 'filename' => 'Whatsons_model.php', 'filepath' => 'models', 'params' => array(), 'module' => 'whatsons', );

What am I doing wrong?

Comments

  • edited 4:39AM
    Actually, module hooks only work for simple modules. What you have, looks like it should work.

    Just to confirm, you have an advanced module called "whatsons" at fuel/modules/whatsons. In that advanced module you have a model named Whatsons_model at fuel/modules/whatsons/models/Whatsons_model.php correct? And you are trying to run the hook right before and after model data is saved in the CMS correct?
  • edited 4:39AM
    No, I have a simple module called whatsons, not an advanced module.

    At first I didn't include a whatson record model, but now that I have, I figured out that it has event handlers available that do what I what them to do, however I never was able to get the before_create and after_create hooks to work - they simply aren't getting invoked.
  • edited 4:39AM
    I think the issue may be because you specified the "module" parameter to "whatsons", which is actually the advanced module's name (I know that is a little confusing) and you can actually leave that empty.
  • edited 4:39AM
    Made no difference.
  • edited 4:39AM
    So just to be clear, this didn't work, meaning your fuel/application/models/Whatsons_model::before_create_whatsons() method was not called?
    $hook['before_create_whatsons'] = array( 'class' => 'Whatsons_model', 'function' => 'before_create_whatsons', 'filename' => 'Whatsons_model.php', 'filepath' => 'models', 'params' => array() );
    If not, does the model get loaded at all can you tell?
  • edited 4:39AM
    The model gets loaded but the hook functions don't get called.
  • edited 4:39AM
    If you change the hook key from "before_create_whatsons" to "before_create_module", does it run (that is a generic hook name that will run for every module)? Basically, the fuel/modules/fuel/controllers/module.php file "_run_hook" method should be getting called when you save so I would check
  • edited 4:39AM
    Found the culprit...

    In MY_Hooks.php function _run_hook() uses the following as the $filepath :
    fuel/application/../modules/whatsons/models/Whatsons_model.php

    while this file actually lives here:
    fuel/application/models/whatsons/models/Whatsons_model.php

    While investigating this I also noticed another bug in MY_Hooks.php:
    the code block starting at line 72:
    { // ----------------------------------- // Add params to data to be passed to hooks // ----------------------------------- if (!empty($params)) { if (!isset($this->hooks[$which])) { $this->hooks[$which]['params'] = array_merge($this->hooks[$which], $params); } else { $this->hooks[$which]['params'] = $params; } } $this->_run_hook($this->hooks[$which]); }

    should read like this:
    { // ----------------------------------- // Add params to data to be passed to hooks // ----------------------------------- if (!empty($params)) { if (isset($this->hooks[$which])) { $this->hooks[$which]['params'] = array_merge($this->hooks[$which], $params); } else { $this->hooks[$which]['params'] = $params; } } $this->_run_hook($this->hooks[$which]); }
    The actual error is on line 78 - the ! should not be there...
  • edited 4:39AM
    Thanks for the report. I've posted a fix for the hook parameter issue in the develop branch.
Sign In or Register to comment.