Problem/Motivation

In #3112298: Replace REQUEST_TIME in classes with direct container access @alexpott tried to autowire a controller:

  public function __construct(
    protected ?TimeInterface $time = NULL
  ) {

TimeInterface is declared as an alias so this should work, but because the argument is nullable it did not actually work and the argument had to be explicitly autowired:

  public function __construct(
    #[Autowire(service: 'datetime.time')]
    protected ?TimeInterface $time = NULL
  ) {

Steps to reproduce

Proposed resolution

Allow nullable types to be autowired the same way as non-nullable ones.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Issue fork drupal-3410222

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

longwave created an issue. See original summary.

alexpott made their first commit to this issue’s fork.

alexpott’s picture

Status: Active » Needs review

mondrake’s picture

Status: Needs review » Reviewed & tested by the community

Nice one, very useful

quietone’s picture

I'm triaging RTBC issues. I read the IS, the comments and the MR. I didn't find any unanswered questions or other work to do.

Leaving at RTBC.

longwave’s picture

Version: 11.x-dev » 10.2.x-dev
Status: Reviewed & tested by the community » Patch (to be ported)

Committed da98f3b and pushed to 11.x. Thanks!

As a bug fix and given that autowiring was present in 10.2.x this is eligible for backport, but doesn't cherry-pick cleanly - if someone can backport the patch we can commit it there as well.

  • longwave committed a4b69560 on 11.x
    Issue #3410222 by alexpott: Autowiring does not support nullable types
    
longwave’s picture

Whoops, forgot to push (thanks @alexpott), done now.

alexpott’s picture

Status: Patch (to be ported) » Reviewed & tested by the community

Created https://git.drupalcode.org/project/drupal/-/merge_requests/6364 for the cherry pick - the core/modules/toolbar/src/Controller/ToolbarController.php changes were not necessary because that change was not in 10.2.x

longwave’s picture

Status: Reviewed & tested by the community » Fixed

Committed 0b80bc5 and pushed to 10.2.x. Thanks!

  • longwave committed 0b80bc53 on 10.2.x
    Issue #3410222 by alexpott: Autowiring does not support nullable types
    

Status: Fixed » Closed (fixed)

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