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

  1. Create a class with a hook following the documentation here.
  2. Add a services.yml file for your hook class and include autowire: true
  3. 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.

Command icon 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

angelamnr created an issue. See original summary.

svendecabooter’s picture

Status: Active » Needs review

Thanks, that seems like a good addition.

Could we also take the opportunity to set autowire: true in 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.

deviktta’s picture

I confirm MR 26 works as expected, thank you both!

svendecabooter’s picture

Status: Needs review » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

kreynen’s picture

I 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.

svendecabooter’s picture

What error are you running into exactly?

benjamin_dk’s picture

Chiming 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:

In DefinitionErrorExceptionPass.php line 54:
                                                                                                                                                                                                                                                             
  Cannot autowire service "externalauth.authmap": argument "$connection" of method "Drupal\externalauth\Authmap::__construct()" references class "Drupal\Core\Database\Connection" but no such service exists. You should maybe alias this class to one of   
  these existing services: "database", "database.replica".                                                                                                                                                                                                   
                                                                                                                                                                                                                                                            
Script bash scripts/update-database.sh handling the update-database event returned with error code 1

svendecabooter’s picture

I 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.

benjamin_dk’s picture

Thanks for your effort @svendecabooter. We are using the module as a dependency (v2.0) to simplesamlphp_auth and 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...

svendecabooter’s picture

If 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?

benjamin_dk’s picture

You are right, it works now! Thanks for your help :)