Problem/Motivation

As of #2852656, plugins can have object-based definitions. However the typing in the docblocks is set only to array for the $pluginDefinition property of core/lib/Drupal/Component/Plugin/PluginBase.php and the return value of getPluginDefinition in core/lib/Drupal/Component/Plugin/PluginInspectionInterface.php.

Proposed resolution

These docblocks should be updated from array to \Drupal\Component\Plugin\Definition\PluginDefinitionInterface|array.

Issue fork drupal-3417521

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:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jayhuskins created an issue. See original summary.

jayhuskins’s picture

Issue summary: View changes

jayhuskins’s picture

Status: Active » Needs review
smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs Review Queue Initiative

Reading CR, believe the update is correct.

quietone’s picture

I'm triaging RTBC issues. I read the IS and the comments. 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 » Fixed

Backported as a docs-only improvement.

Committed and pushed ed6f70f8b8 to 11.x and c3ceee3d74 to 10.3.x and 2a7b91d0d4 to 10.2.x. Thanks!

  • longwave committed 2a7b91d0 on 10.2.x
    Issue #3417521 by jayhuskins: Update typing for plugin definition to...

  • longwave committed c3ceee3d on 10.3.x
    Issue #3417521 by jayhuskins: Update typing for plugin definition to...

  • longwave committed ed6f70f8 on 11.x
    Issue #3417521 by jayhuskins: Update typing for plugin definition to...

mikeryan’s picture

We're having CI failures on our app with Drupal 10.2.4, which passes under Drupal 10.2.3, and this appears to be the relevant change.

Error: Cannot access offset 'id' on array|Drupal\Component\Plugin\Definition\PluginDefinitionInterface.
 ------ --------------------------------------------------------------------- 
  Line   src/ActiveSitePluginBase.php                                         
 ------ --------------------------------------------------------------------- 
  54     Cannot access offset 'id' on                                         
         array|Drupal\Component\Plugin\Definition\PluginDefinitionInterface.  
 ------ --------------------------------------------------------------------- 

The triggering code:

  /**
   * {@inheritdoc}
   */
  public function id(): string {
    return $this->pluginDefinition['id'];
  }

This should still be correct usage, shouldn't it? Core is still full of instances of $this->pluginDefinition['key'].

mstrelan’s picture

@mikeryan you likely have a higher phpstan level than core. I think you might need to change to something like this:

  public function id(): string {
    if (is_array($this->pluginDefinition)) {
      return $this->pluginDefinition['id'];
    }
    return $this->pluginDefinition->id();
  }

Or coerce phpstan in to believing pluginDefinition is always an array:

  public function id(): string {
      assert(is_array($this->pluginDefinition));
      return $this->pluginDefinition['id'];
  }

Edit:

Or you could probably override the typehint on the property in your plugin class:

  /**
   * The plugin definition.
   *
   * @var array
   */
  protected $pluginDefinition;
mikeryan’s picture

@mstrelan Thanks! assert() did the job nicely...

Status: Fixed » Closed (fixed)

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