Plugins will be initialized before setting $this->entityId, therefor the plugin's entityId is always NULL.

For example PaymentGatewayPluginCollection:

class PaymentGatewayPluginCollection extends DefaultSingleLazyPluginCollection {
  public function __construct(PluginManagerInterface $manager, $instance_id, array $configuration, $entity_id) {
    parent::__construct($manager, $instance_id, $configuration);

    // The plugin (identified by $instance_id) has been initialized when reaching here.

    $this->entityId = $entity_id;
  }
}

Because 'DefaultSingleLazyPluginCollection' has changed in Drupal 8.4.x:

@@ -52,10 +52,7 @@ class DefaultSingleLazyPluginCollection extends LazyPluginCollection {
    */
   public function __construct(PluginManagerInterface $manager, $instance_id, array $configuration) {
     $this->manager = $manager;
-    $this->instanceId = $instance_id;
-    // This is still needed by the parent LazyPluginCollection class.
-    $this->instanceIDs = [$instance_id => $instance_id];
-    $this->configuration = $configuration;
+    $this->addInstanceId($instance_id, $configuration);
   }
 
   /**
@@ -95,6 +92,8 @@ class DefaultSingleLazyPluginCollection extends LazyPluginCollection {
    */
   public function addInstanceId($id, $configuration = NULL) {
     $this->instanceId = $id;
+    // Reset the list of instance IDs since there can be only one.
+    $this->instanceIDs = [];
     parent::addInstanceId($id, $configuration);
     if ($configuration !== NULL) {
       $this->setConfiguration($configuration);

This line $this->setConfiguration($configuration); causes the plugin to be initialized.

This affects PaymentGatewayPluginCollection, TaxTypePluginCollection, CheckoutFlowPluginCollection and ShippingMethodPluginCollection.

Comments

edwardaa created an issue. See original summary.

bojanz’s picture

Assigned: Unassigned » bojanz

Working on this.

  • bojanz committed 3bc1b88 on 8.x-2.x
    Issue #2904570 by edwardaa, bojanz: $this->entityId always NULL in...
bojanz’s picture

Assigned: bojanz » Unassigned
Status: Active » Fixed

1) Replaced PaymentGatewayPluginCollection, TaxTypePluginCollection, CheckoutFlowPluginCollection with a single CommerceSinglePluginCollection

2) Moved $this->entityId init above the constructor call, added a comment explaining why it's important.

3) Fixed CheckoutFlowForm which was initializing the plugin collection with an empty plugin ID, causing a crash.

4) Tackled on a fix for CreditCardTest which was the last remaining 8.4.x test failure.

Commented in the core issue #2851635: DefaultSingleLazyPluginCollection retains stale instance IDs which broke BC.

Status: Fixed » Closed (fixed)

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