Skip to content

Commit 3bc1b88

Browse files
committed
Issue #2904570 by edwardaa, bojanz: $this->entityId always NULL in plugins in Drupal 8.4.x
1 parent 2db51c7 commit 3bc1b88

File tree

8 files changed

+31
-160
lines changed

8 files changed

+31
-160
lines changed

Diff for: modules/checkout/src/CheckoutFlowPluginCollection.php

-62
This file was deleted.

Diff for: modules/checkout/src/Entity/CheckoutFlow.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Drupal\commerce_checkout\Entity;
44

5-
use Drupal\commerce_checkout\CheckoutFlowPluginCollection;
5+
use Drupal\commerce\CommerceSinglePluginCollection;
66
use Drupal\Core\Config\Entity\ConfigEntityBase;
77

88
/**
@@ -83,7 +83,7 @@ class CheckoutFlow extends ConfigEntityBase implements CheckoutFlowInterface {
8383
/**
8484
* The plugin collection that holds the checkout flow plugin.
8585
*
86-
* @var \Drupal\commerce_checkout\CheckoutFlowPluginCollection
86+
* @var \Drupal\commerce\CommerceSinglePluginCollection
8787
*/
8888
protected $pluginCollection;
8989

@@ -138,13 +138,13 @@ public function set($property_name, $value) {
138138
*
139139
* Ensures the plugin collection is initialized before returning it.
140140
*
141-
* @return \Drupal\commerce_checkout\CheckoutFlowPluginCollection
141+
* @return \Drupal\commerce\CommerceSinglePluginCollection
142142
* The plugin collection.
143143
*/
144144
protected function getPluginCollection() {
145145
if (!$this->pluginCollection) {
146146
$plugin_manager = \Drupal::service('plugin.manager.commerce_checkout_flow');
147-
$this->pluginCollection = new CheckoutFlowPluginCollection($plugin_manager, $this->plugin, $this->configuration, $this->id);
147+
$this->pluginCollection = new CommerceSinglePluginCollection($plugin_manager, $this->plugin, $this->configuration, $this->id);
148148
}
149149
return $this->pluginCollection;
150150
}

Diff for: modules/checkout/src/Form/CheckoutFlowForm.php

+7-14
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Drupal\commerce\Form\CommercePluginEntityFormBase;
66
use Drupal\commerce_checkout\CheckoutFlowManager;
7-
use Drupal\Core\Entity\EntityInterface;
87
use Drupal\Core\Form\FormStateInterface;
98
use Symfony\Component\DependencyInjection\ContainerInterface;
109

@@ -45,6 +44,12 @@ public function form(array $form, FormStateInterface $form_state) {
4544
$checkout_flow = $this->entity;
4645
$plugins = array_column($this->pluginManager->getDefinitions(), 'label', 'id');
4746
asort($plugins);
47+
// Use the first available plugin as the default value.
48+
if (!$checkout_flow->getPluginId()) {
49+
$plugin_ids = array_keys($plugins);
50+
$plugin = reset($plugin_ids);
51+
$checkout_flow->setPluginId($plugin);
52+
}
4853

4954
$form['#tree'] = TRUE;
5055
$form['#attached']['library'][] = 'commerce_checkout/admin';
@@ -63,7 +68,7 @@ public function form(array $form, FormStateInterface $form_state) {
6368
],
6469
];
6570
$form['plugin'] = [
66-
'#type' => 'select',
71+
'#type' => 'radios',
6772
'#title' => $this->t('Plugin'),
6873
'#options' => $plugins,
6974
'#default_value' => $checkout_flow->getPluginId(),
@@ -80,18 +85,6 @@ public function form(array $form, FormStateInterface $form_state) {
8085
return $this->protectPluginIdElement($form);
8186
}
8287

83-
/**
84-
* {@inheritdoc}
85-
*/
86-
protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
87-
/** @var \Drupal\commerce_checkout\Entity\CheckoutFlowInterface $entity */
88-
// The parent method tries to initialize the plugin collection before
89-
// setting the plugin.
90-
$entity->setPluginId($form_state->getValue('plugin'));
91-
92-
parent::copyFormValuesToEntity($entity, $form, $form_state);
93-
}
94-
9588
/**
9689
* {@inheritdoc}
9790
*/

Diff for: modules/payment/src/Entity/PaymentGateway.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace Drupal\commerce_payment\Entity;
44

5+
use Drupal\commerce\CommerceSinglePluginCollection;
56
use Drupal\commerce\ConditionGroup;
67
use Drupal\commerce_order\Entity\OrderInterface;
7-
use Drupal\commerce_payment\PaymentGatewayPluginCollection;
88
use Drupal\Core\Config\Entity\ConfigEntityBase;
99

1010
/**
@@ -113,7 +113,7 @@ class PaymentGateway extends ConfigEntityBase implements PaymentGatewayInterface
113113
/**
114114
* The plugin collection that holds the payment gateway plugin.
115115
*
116-
* @var \Drupal\commerce_payment\PaymentGatewayPluginCollection
116+
* @var \Drupal\commerce\CommerceSinglePluginCollection
117117
*/
118118
protected $pluginCollection;
119119

@@ -247,13 +247,13 @@ public function set($property_name, $value) {
247247
*
248248
* Ensures the plugin collection is initialized before returning it.
249249
*
250-
* @return \Drupal\commerce_payment\PaymentGatewayPluginCollection
250+
* @return \Drupal\commerce\CommerceSinglePluginCollection
251251
* The plugin collection.
252252
*/
253253
protected function getPluginCollection() {
254254
if (!$this->pluginCollection) {
255255
$plugin_manager = \Drupal::service('plugin.manager.commerce_payment_gateway');
256-
$this->pluginCollection = new PaymentGatewayPluginCollection($plugin_manager, $this->plugin, $this->configuration, $this->id);
256+
$this->pluginCollection = new CommerceSinglePluginCollection($plugin_manager, $this->plugin, $this->configuration, $this->id);
257257
}
258258
return $this->pluginCollection;
259259
}

Diff for: modules/payment/src/PaymentGatewayPluginCollection.php

-62
This file was deleted.

Diff for: modules/payment/tests/src/Unit/CreditCardTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44

55
use Drupal\commerce_payment\CreditCard;
66
use Drupal\commerce_payment\CreditCardType;
7+
use Drupal\Tests\UnitTestCase;
78

89
/**
910
* @coversDefaultClass \Drupal\commerce_payment\CreditCard
1011
* @group commerce
1112
*/
12-
class CreditCardTest extends \PHPUnit_Framework_TestCase {
13+
class CreditCardTest extends UnitTestCase {
1314

1415
/**
1516
* @covers ::getTypes

Diff for: modules/tax/src/Entity/TaxType.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Drupal\commerce_tax\Entity;
44

5-
use Drupal\commerce_tax\TaxTypePluginCollection;
5+
use Drupal\commerce\CommerceSinglePluginCollection;
66
use Drupal\Core\Config\Entity\ConfigEntityBase;
77

88
/**
@@ -83,7 +83,7 @@ class TaxType extends ConfigEntityBase implements TaxTypeInterface {
8383
/**
8484
* The plugin collection that holds the tax type plugin.
8585
*
86-
* @var \Drupal\commerce_tax\TaxTypePluginCollection
86+
* @var \Drupal\commerce\CommerceSinglePluginCollection
8787
*/
8888
protected $pluginCollection;
8989

@@ -157,13 +157,13 @@ public function set($property_name, $value) {
157157
*
158158
* Ensures the plugin collection is initialized before returning it.
159159
*
160-
* @return \Drupal\commerce_tax\TaxTypePluginCollection
160+
* @return \Drupal\commerce\CommerceSinglePluginCollection
161161
* The plugin collection.
162162
*/
163163
protected function getPluginCollection() {
164164
if (!$this->pluginCollection) {
165165
$plugin_manager = \Drupal::service('plugin.manager.commerce_tax_type');
166-
$this->pluginCollection = new TaxTypePluginCollection($plugin_manager, $this->plugin, $this->configuration, $this->id);
166+
$this->pluginCollection = new CommerceSinglePluginCollection($plugin_manager, $this->plugin, $this->configuration, $this->id);
167167
}
168168
return $this->pluginCollection;
169169
}

Diff for: modules/tax/src/TaxTypePluginCollection.php renamed to src/CommerceSinglePluginCollection.php

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
<?php
22

3-
namespace Drupal\commerce_tax;
3+
namespace Drupal\commerce;
44

55
use Drupal\Component\Plugin\Exception\PluginException;
66
use Drupal\Component\Plugin\PluginManagerInterface;
77
use Drupal\Core\Plugin\DefaultSingleLazyPluginCollection;
88

99
/**
10-
* A collection of tax type plugins.
10+
* A collection that stores a single plugin, aware of its parent entity ID.
1111
*/
12-
class TaxTypePluginCollection extends DefaultSingleLazyPluginCollection {
12+
class CommerceSinglePluginCollection extends DefaultSingleLazyPluginCollection {
1313

1414
/**
15-
* The tax type entity ID this plugin collection belongs to.
15+
* The entity ID this plugin collection belongs to.
1616
*
1717
* @var string
1818
*/
1919
protected $entityId;
2020

2121
/**
22-
* Constructs a new TaxTypePluginCollection object.
22+
* Constructs a new CommerceSinglePluginCollection object.
2323
*
2424
* @param \Drupal\Component\Plugin\PluginManagerInterface $manager
2525
* The manager to be used for instantiating plugins.
@@ -28,20 +28,21 @@ class TaxTypePluginCollection extends DefaultSingleLazyPluginCollection {
2828
* @param array $configuration
2929
* An array of configuration.
3030
* @param string $entity_id
31-
* The tax type entity ID this plugin collection belongs to.
31+
* The entity ID this plugin collection belongs to.
3232
*/
3333
public function __construct(PluginManagerInterface $manager, $instance_id, array $configuration, $entity_id) {
34-
parent::__construct($manager, $instance_id, $configuration);
35-
3634
$this->entityId = $entity_id;
35+
// The parent constructor initializes the plugin, so it needs to be called
36+
// after $this->entityId is set.
37+
parent::__construct($manager, $instance_id, $configuration);
3738
}
3839

3940
/**
4041
* {@inheritdoc}
4142
*/
4243
protected function initializePlugin($instance_id) {
4344
if (!$instance_id) {
44-
throw new PluginException("The tax type '{$this->entityId}' did not specify a plugin.");
45+
throw new PluginException("The entity '{$this->entityId}' did not specify a plugin.");
4546
}
4647

4748
$configuration = ['_entity_id' => $this->entityId] + $this->configuration;

0 commit comments

Comments
 (0)