Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing Documentation: Model Service - $_hook_id #226

Open
RK311y opened this issue Jul 31, 2021 · 0 comments
Open

Missing Documentation: Model Service - $_hook_id #226

RK311y opened this issue Jul 31, 2021 · 0 comments

Comments

@RK311y
Copy link
Contributor

RK311y commented Jul 31, 2021

Suggested Change

The Model Service documentation is missing a description of the $_hook_id property. The example I have found that uses this property is within the Member Model.

Reasoning

I was developing an add-on that contains a Contact model which belongsTo the ee:Member model.
Being curious in the process in which member models are saved/updated/inserted/deleted, I went searching for the extension hook call before_member_save($member, $values).

Based on the documentation, I was lead to believe that all extension hooks are invoked via some code that looks like:

if (ee()->extensions->active_hook('some_hook_method') === TRUE)
{
    $str = ee()->extensions->call('some_hook_method', $hook_args...);
}

Unable to locate the existence of such code, with the hook method before_member_save, I did some deeper digging.

I learned that Models, which extend the ExpressionEngine\Service\Model class invoke the forwardEventToHooks($event) method. Walking through the code of this method, I discovered that:

  1. Models which have a $_hook_id property defined are exposed to extension hook calls (Event name, either 'insert', 'update', 'save', or 'delete') based on the following convention:
$forwarded = array(
    'before' . $uc_first_event => 'before_' . $hook_basename . '_' . $event,
    'after' . $uc_first_event => 'after_' . $hook_basename . '_' . $event
);
  1. Internal events on the Model are also emitted without having to call them externally: For example:
$model = ee('Model')->get('Member')->first();
$model->validate(); // internally, $this->emit('beforeValidate') and $this->emit('afterValidate') are called
$model->save(); // internally, $this->emit('beforeSave') and $this->emit('afterSave') are called
// .... etc

Before I was attaching my own events for these actions and having to emit (invoke) them manually.

Additional context

I would recommend adding this documentation here: Building your own Models

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant