- Issue created by @berdir
- π§πͺBelgium kristiaanvandeneynde Antwerp, Belgium
Declaring all your hooks as extensions of Hook would definitely solve this as we can then support a "source" property on the base Hook class that we can scan for as described in the IS. The only drawback I see is we'd be creating tons of one-of attributes. They would be contained to namespaces that have Hook in them, so I suppose that could be fine.
So maybe add an optional "source" property to Hook that we can specify in the Hook constructor as a named argument and then extensions of Hook can prefill it for you? The default value could be empty or "drupal" to indicate that it's a core hook.
Anyways, big +1 to the idea. We shouldn't add classes to the container if we know they will never be called.
- πΊπΈUnited States nicxvan
There is a related issue I can't find at the moment. I think this needs to be solved with a condition, I'm wondering if it should be a separate attribute or a parameter. Both options have pros and cons.
But either way, rather than a simple module exists it could be a callback.
There would be some serious restrictions, but you could conditionally add the hooks based on that result.
For example define the hook only if another module does not exist.
- π¨πSwitzerland berdir Switzerland
I think modules being installed or not is by far the most common use case for this and I'd rather focus on that. callbacks imply dynamic logic like something based on settings, but then you have to rebuild the container if that changes. Seems more complex to support and define. Could also design the attribute in a way that allows to extend it later?
#[ConditionalRegister(moduleExists: 'config_translation')]
And then we could add a callback parameter if there is a valid use case for that later.
- π§πͺBelgium kristiaanvandeneynde Antwerp, Belgium
Adding an attribute to the class that skips it for registration if the dependency is unmet seems like a good solution to the optional constructor argument problem. If we know that the dependency needs to exist for our hooks to be registered as a service, we can safely use the dependency's services with autowiring.