Problem/Motivation
Drupal has been expanding its support of services that can have their arguments autowired. One exciting new example of this is the ability to use object-oriented hooks in Drupal 11.
When adding the externalauth.authmap service to an autowired hook class, I ran into this error:
Cannot autowire service "Drupal\custom_module\Hook\MyCustomHooks": argument "$authmap" of method "__construct()" references interface "Drupal\externalauth\AuthmapInterface" but no such service exists. You should maybe alias this interface to the existing "externalauth.authmap" service.
Steps to reproduce
- Create a class with a hook following the documentation here.
- Add a services.yml file for your hook class and include autowire: true
- Make sure to include a construct method for your class as shown below and observe the error message:
<?php
namespace Drupal\custom_module\Hook;
use Drupal\externalauth\AuthmapInterface;
class MyCustomHooks {
public function __construct(
private AuthmapInterface $authmap,
) {}
...
}
Proposed resolution
Create aliases for externalauth.authmap and externalauth.externalauth in externalauth.services.yml.
Issue fork externalauth-3543663
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #3
svendecabooterThanks, that seems like a good addition.
Could we also take the opportunity to set
autowire: truein the services defined in externalauth.services.yml?Autowiring is supported since 9.3, and since our min. required version is 9.5, that should work.
Comment #4
deviktta commentedI confirm MR 26 works as expected, thank you both!
Comment #5
svendecabooterComment #9
kreynen commentedI just ran into this error running 2.0.9 on 9.5 running on PHP8.1 when running drush si for our install profile. Rolling back to 2.0.8 fixed the issue. Not having the issue in sites running 10.6.3 with PHP8.3.
I realize that one solution is to upgrade sites to a supported version of Drupal. We are working on that, but I wanted to share this in case someone else who is still running D9 experiences this.
Comment #10
svendecabooterWhat error are you running into exactly?
Comment #11
benjamin_dk commentedChiming in here as I also experience similar problems running on Drupal 9.5.11, PHP 8.0.30 and updating externalauth to v2.0.9:
Comment #12
svendecabooterI have created a new release 2.0.10 that should fix this issue for sites prior to Drupal 10.1.
See #3575914: Drupal 9 autowiring regressions.
When you run `composer update` you should be able to only update to 2.0.10, and not to the newer 2.0.11, which re-introduces the autowiring support.
To keep getting support for this module, you will need to update your site to a Drupal version that is still supported.
Comment #13
benjamin_dk commentedThanks for your effort @svendecabooter. We are using the module as a dependency (v2.0) to
simplesamlphp_authand therefore we probably will not be able to pin the version 2.0.10. Maybe if the autowire update had been made as a minor version instead of patch it could have worked? Anyway we should just get our sites updated to D10 asap...Comment #14
svendecabooterIf 2.0.11 has a Composer-enforced dependency of Drupal 10.1+, it would not be installed when you still run Drupal 9 I think? No need to do any manual pinning of the release?
Comment #15
benjamin_dk commentedYou are right, it works now! Thanks for your help :)