Allow non \Drupal\myModule namespaced hook services

Created on 8 February 2023, almost 2 years ago
Updated 9 February 2023, almost 2 years ago

Problem/Motivation

This module is an amazing concept and should be the way to do hooks in D11.
However a module is still needed.
Services can be added to the container via settings.php but these services can not have hux unless the namespace is \Drupal\modulename\

we could tell drupal that the modulename is "none" since hooks are expected to be from modules.

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Feature request
Status

Active

Version

1.0

Component

Code

Created by

🇨🇭Switzerland bircher 🇨🇿

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

  • Issue created by @bircher
  • 🇦🇺Australia dpi Perth, Australia

    It seems like an odd usage, adding hook definitions in services defined by a container YAML.

    Presently you should be able to define more namespaces to expand on `\Drupal` in your own custom directories. Then use the `hooks` service tag like hux_test.services.yml. That should get you going with container YAMLS.

    I've renamed the issue accordingly. Let me know if this isnt right.

    As for supporting arbitrary namespaces, I'm open to the idea so long as we have tests. The regex detection stuff in HuxCompilerPass is the primary concern. This could be resolved with service tags or PHP attributes indicating a module name. In the end we need to associate each hook definition with a module, since core ModuleHandler requires it.

    This is certainly not something I'm planning on working on any time soon, so if you have some time to contribute that would be appreciated.

  • 🇦🇺Australia dpi Perth, Australia
  • 🇨🇭Switzerland bircher 🇨🇿

    Cool! Thanks for the reply!

    Yes I know that the module handler needs it, but from my tests the module handler doesn't care if the module exists or not.
    My workaround is to define a dev-PSR4 namespace in the root composer.json for \Drupal\mytests and then add a tests.services.yml that defines the services I need in my tests there. (I use it mostly to decorate/replace services interacting with third party APIs to log their interaction instead.) I created this issue because using a hook to alter some things was easier because it is not with a system easily pluggable with services.
    Usually my test services don't live in \Drupal\something but in \MyCode\something since they are not from a module. (Of course i could put all of this in a module and just install the module in the test environment too.. but then I would have a test module one could technically install on prod, while the dev-PSR4 namespace is just not loaded and one can not edit settings.php from the admin UI to add the services.yml)

    As for implementation, we could require another tag on such services like hook_module and throw the exception only when it doesn't have that.

  • 🇦🇺Australia dpi Perth, Australia

    Would make sense as an attribute. Something like #[AsModule], with TARGET_CLASS.

    Similar to how \Drupal\hux\Attribute\ReplaceOriginalHook can specify a $moduleName.

Production build 0.71.5 2024