Missing Documentation: Model Service - $_hook_id

Ouverte
#226 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Évaluation

Difficulté
2/5
Temps estimé
1-3 heures
Accessibilité débutants
35/100
Type d'issue
Documentation
Clarté
Clairement spécifiée
Activité
À l'abandon
Stack technique
php
Domaine
documentation

Piste de recherche

Start with the “Building your own Models” guide named in the issue, then check the linked Model and Member source files to confirm how $_hook_id is used. Document the property and the hook naming behavior described in the issue; done means the guide explains which models expose extension hooks and how their event names are formed.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

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

Langage dominant
CSS
Étoiles
22
Forks
79
Merge moyen
2 j 1 h
PR mergées (30 j)
6

Préparer son environnement

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de ExpressionEngine/ExpressionEngine-User-Guide

Toutes les issues de ExpressionEngine/ExpressionEngine-User-Guide

Issues similaires

Plus d'issues Documentation

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.