Problem/Motivation

Ran into this while working on #3364109: Configuration schema & required values: add test coverage for `nullable: true` validation support.

signature \Drupal\Component\Plugin\ConfigurableInterface::setConfiguration(array $configuration) (note type hint)
🆚
signature \Drupal\Component\Plugin\LazyPluginCollection::setConfiguration($configuration) (note NO type hint)
🆚
\Drupal\Core\Plugin\DefaultSingleLazyPluginCollection::setConfiguration() implementation of LazyPluginCollection::setConfiguration():

  public function setConfiguration($configuration) {
    $this->configuration = $configuration;
    $plugin = $this->get($this->instanceId);
    if ($plugin instanceof ConfigurableInterface) {
      $plugin->setConfiguration($configuration);
    }
    return $this;
  }

Steps to reproduce

$block = Block::load('block.block.olivero_content');
$block->set('settings', NULL);
$typed_data = $this->container->get('typed_data_manager');
$definition = $typed_data->createDataDefinition('entity:block');
$violations = $typed_data->create($definition, $block)->validate();

Results in:

TypeError: Drupal\Core\Block\BlockBase::setConfiguration(): Argument #1 ($configuration) must be of type array, null given, called in /Users/wim.leers/core/core/lib/Drupal/Core/Plugin/DefaultSingleLazyPluginCollection.php on line 85

/Users/wim.leers/core/core/lib/Drupal/Core/Block/BlockPluginTrait.php:79
/Users/wim.leers/core/core/lib/Drupal/Core/Plugin/DefaultSingleLazyPluginCollection.php:85
/Users/wim.leers/core/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php:165
…

Same problem in \Drupal\Core\Plugin\DefaultLazyPluginCollection::setConfiguration() (try visibility on

Proposed resolution

Make
\Drupal\Core\Plugin\DefaultSingleLazyPluginCollection::setConfiguration()
\Drupal\Core\Plugin\DefaultLazyPluginCollection::setConfiguration()
treat NULL as [] — see #8 for rationale.

Remaining tasks

None.

User interface changes

None.

API changes

None.

Data model changes

None.

Release notes snippet

N/A

Issue fork drupal-3404023

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

Wim Leers created an issue. See original summary.

wim leers’s picture

Assigned: wim leers » Unassigned
Status: Active » Needs review
wim leers’s picture

IMHO this is such a simple change (purely to do with function signatures) that test coverage is excessive here?

wim leers’s picture

PHPStan gets in the way once again. Locally, PHPStan does not complain 😬

Tried an alternative implementation.

mglaman’s picture

I don't know why it passed locally, but it's probably trusting the phpdoc which probably says it's always an array and never null.

wim leers’s picture

Thanks, @mglaman, that makes sense!

Note that an alternative solution could be to modify
\Drupal\Core\Config\Entity\ConfigEntityBase::set():

  public function set($property_name, $value) {
    if ($this instanceof EntityWithPluginCollectionInterface) {
      $plugin_collections = $this->getPluginCollections();
      if (isset($plugin_collections[$property_name])) {
        // If external code updates the settings, pass it along to the plugin.
        $plugin_collections[$property_name]->setConfiguration($value);
      }
    }
…

👆 We could make it not call ::setConfiguration() if $value is NULL.

mglaman’s picture

I read null as reset/no config. So I'd transform null to [] and still call. Otherwise the intent to remove or clear configuration wouldn't happen and existing configuration would persist.

The idea that null === remove.

smustgrave’s picture

Status: Needs review » Needs work

If I'm understanding correctly believe the suggestion is to revert

https://git.drupalcode.org/project/drupal/-/merge_requests/5540/diffs?co...

Right?

wim leers’s picture

Issue summary: View changes
Status: Needs work » Needs review

Done.

phenaproxima’s picture

This looks good, but I think it could use a comment. Also, don't we need test coverage for this?

smustgrave’s picture

Status: Needs review » Needs work

NW for the inline comment.

in #4 was mentioned test coverage may be overkill but if there's valid concern then test coverage should be added too.

wim leers’s picture

Assigned: Unassigned » wim leers
borisson_’s picture

I'm not sure about the solution chosen here, should we deprecate calling DefaultSingleLazyPluginCollection::setConfiguration() with NULL, and attempt to make it typehinted in drupal 11?

wim leers’s picture

Assigned: wim leers » Unassigned
Status: Needs work » Needs review

@phenaproxima: Test coverage added. The test-only job fails 👍

@borisson_: Done. But note that it's not that simple, because \Drupal\Component\Plugin\LazyPluginCollection::setConfiguration() is an abstract method, which contrib/custom modules may be implementing too. How can we warn those? Turns out it is simple: https://3v4l.org/dcCF0 👍 CR: https://www.drupal.org/node/3406191

borisson_’s picture

Status: Needs review » Reviewed & tested by the community

Looks great now.

phenaproxima’s picture

Status: Reviewed & tested by the community » Needs review

One small question about adding a future-proof type hint.

wim leers’s picture

Assigned: Unassigned » phenaproxima

AFAICT PHPStan does not allow us to do that. See MR for PHPStan output.

phenaproxima’s picture

What the hell is PHPStan talking about??? That is contravariant.

From https://www.php.net/manual/en/language.oop5.variance.php (my emphasis):

Covariance allows a child's method to return a more specific type than the return type of its parent's method. Whereas, contravariance allows a parameter type to be less specific in a child method, than that of its parent.

phenaproxima’s picture

Oh, I see -- PHPStan is annoyed because of the direction of the contravariance, since this MR doesn't change \Drupal\Component\Plugin\LazyPluginCollection::setConfiguration.

But then I gotta wonder...why aren't we also changing that method too? The doc comment says that $configuration needs to be an array.

wim leers’s picture

But then I gotta wonder...why aren't we also changing that method too? The doc comment says that $configuration needs to be an array.

Because the end goal is public function setConfiguration(array $configuration), without the ?.

And if we set it to ?array now, then it's impossible to change the typehint to array in 11.

array works, ?array does not work

phenaproxima’s picture

Status: Needs review » Reviewed & tested by the community

Yeah, that makes sense. It's too bad we can't change the type hints now, but in light of the discovery about LazyPluginCollection, I think what we have is a good approach. In Drupal 11 we can fix it all properly, array $configuration everywhere!

Adjusting credit and restoring RTBC.

wim leers’s picture

Assigned: phenaproxima » Unassigned

Yay!

alexpott’s picture

Status: Reviewed & tested by the community » Needs review

Asked a question / have a suggestion on the MR.

wim leers’s picture

See #15 and #21 for 3v4l.org examples for why the current typehint additions are AFAICT fine and @phenaproxima's last MR comment is AFAICT correct.

What are we missing?

wim leers’s picture

Status: Needs review » Reviewed & tested by the community

AFAICT everything is actually addressed:

  1. I've applied Alex' language suggestions.
  2. The remarks Alex posted were apparently on suggestions by @phenaproxima that I already explained why they could not work.

So: back to RTBC. Alex mentioned in #24 a question was asked on the MR, but I don't see any question, only responses on _suggestion threads_ that were responded to here in the issue? 🤔

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 871bc1a and pushed to 11.x. Thanks!

  • alexpott committed 871bc1ac on 11.x
    Issue #3404023 by Wim Leers, phenaproxima, mglaman, borisson_, alexpott...
quietone’s picture

Published the CR

Status: Fixed » Closed (fixed)

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