Single invoke should merge results if multiple implementations for same module

Created on 9 June 2023, about 1 year ago
Updated 10 June 2023, about 1 year ago

Problem/Motivation

With Hux, a single module can implement the same hook multiple times.
In ->invokeAll(), the return values of these implementations are merged.
In ->invoke(), all but the last return value is lost.

This causes a problem e.g. if a module has multiple #[Hook('schema')].
Actually, hook_schema() seems to work quite ok with hux, except for this one problem.

Steps to reproduce

Proposed resolution

Merge the results, if more than one.

Remaining tasks

User interface changes

API changes

Data model changes

πŸ“Œ Task
Status

Needs review

Version

1.2

Component

Miscellaneous

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
  • πŸ‡©πŸ‡ͺGermany donquixote

    Actually it is a bit more complicated.

    If the hook implementations return something other than an array, then:

    • core ->invoke() will return the literal value, e.g. "a".
    • core ->invokeAll() will return a list of values, omitting NULL values, e.g. ["a", "b", "c"].
    • hux ->invoke(), if more than one implementation, must still return a single non-array value. So it must discard other values.

    If the hook implementations return arrays, then:

    • core ->invoke() will return the literal array, e.g. ["a"].
    • core ->invokeAll() will return the merged array, e.g. ["a", "b", "c"].
    • hux ->invoke(), if more than one implementation, can return the merged array.

    If implementations have different value types, it gets complicated.

  • πŸ‡©πŸ‡ͺGermany donquixote

    Examples:

    This is just one possible way to handle this.
    Perhaps null values should _not_ be ignored?

  • πŸ‡©πŸ‡ͺGermany donquixote

    Perhaps we should throw an exception if there are incompatible return types in the same module that would cause data loss.

    Actually I like this as a general rule for all cases:

    If multiple implementations exist in one module, they must each return array or null

    This will only be thrown if invoke() is called, never on invokeAll().

    h3. Backwards breaking changes
    Projects or modules that newly add Hux won't have breaking changes, because without hux there can only be one implementation per module.

    Projects or modules that already have multiple implementations with Hux could see a breaking change, but only if their previous implementation was ambiguous or relied on the overwriting.

  • πŸ‡©πŸ‡ͺGermany donquixote

    Wow, I really need a better way to edit tables to insert here in raw html.
    Some more variations and comparison with current version and core.

  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.1.x + Environment: PHP 8.1 & MariaDB 10.3.22
    last update about 1 year ago
    40 pass, 1 fail
  • @donquixote opened merge request.
  • Status changed to Needs review about 1 year ago
  • πŸ‡©πŸ‡ͺGermany donquixote

    The MR contains different versions as separate commits.
    We can pick and choose once we decide what we want.

Production build 0.69.0 2024