Advanced attributes to declare multiple hooks

Created on 24 May 2023, about 1 year ago
Updated 16 June 2023, about 1 year ago

Problem/Motivation

I am using hux in a project where I do a lot of this, for different entity types:

  #[Hook('user_insert'), Hook('user_update')]

I also want to introduce a new entity hook type, to register existing entities when my module is installed, without pinging all the other implementations of update and insert hooks.
These would essentially do the same as update and insert.
So it would become this:

  #[Hook('user_insert'), Hook('user_update'), Hook('user_introduce_to_mymodule')]

This can become quite repetitive.

Proposed resolution

It would be nice if this could be shortened, using a custom attribute, like so:

  #[EntityHook('user', ['update', 'insert', 'introduce_to_mymodule'])]

or like so:

  #[EntityWriteHook('user')]

I see two possible ways to do this:

  1. Provide a base class for custom attribute classes, where the constructor receives a list of hook references, which is then exposed as a public property. Or use a setter/adder method.
  2. Provide an interface for custom attribute classes, with a method that returns a list of hook references, instead of a single hook reference.

class EntityHook extends MultiHookAttributeBase {
  public function __construct(string $entity_type, array $operations) {
    foreach ($operations as $operation) {
      $this->addHookImplementation($entity_type . '_' . $operation);
    }
  }
}

class EntityWriteHook extends EntityHook {
  public function __construct(string $entity_type) {
    parent::__construct($entity_type, ['insert', 'update', 'introduce_to_my_module']);
  }
}

Remaining tasks

User interface changes

API changes

New base class or interface for custom attribute classes.

Data model changes

✨ Feature request
Status

Closed: won't fix

Version

1.2

Component

Code

Created by

πŸ‡©πŸ‡ͺGermany donquixote

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @donquixote
  • πŸ‡¦πŸ‡ΊAustralia dpi Perth, Australia

    The steps required to achieve this are not far off whats needed for ✨ Dynamic hook ID specific attributes Active , effectively Hux just needs to treat an attribute as representing more than one hook. This would involve:

    • Introduce a interface for Hook attr.
      I dont think it makes sense for others to extend the existing Hook class as the main entry into attributes is the constructor, and having different constructor signatures extending the original doesnt seem right.
    • A method on the new interface and existing \Drupal\hux\Attribute\Hook, getHooks()
    • Finalise Hook class
    • Update HuxDiscovery use instanceof strategy with getAttributes.
    • Update HuxDiscovery to invoke and iterate over the returned hooks.

    It'd be good to hear opinions on the design as getting it right is my main blocker for ✨ Dynamic hook ID specific attributes Active

  • Status changed to Closed: won't fix about 1 year ago
  • πŸ‡¦πŸ‡ΊAustralia dpi Perth, Australia

    Closing in favor of ✨ Dynamic hook ID specific attributes Active .

    Theres some good ideas here, lets continue

Production build 0.69.0 2024