diff --git a/modules/checkout/src/CheckoutFlowPluginCollection.php b/modules/checkout/src/CheckoutFlowPluginCollection.php deleted file mode 100644 index c7b8ba984d..0000000000 --- a/modules/checkout/src/CheckoutFlowPluginCollection.php +++ /dev/null @@ -1,62 +0,0 @@ -entityId = $entity_id; - } - - /** - * {@inheritdoc} - * - * @return \Drupal\commerce_checkout\Plugin\Commerce\CheckoutFlow\CheckoutFlowInterface - * The checkout flow plugin. - */ - public function &get($instance_id) { - return parent::get($instance_id); - } - - /** - * {@inheritdoc} - */ - protected function initializePlugin($instance_id) { - if (!$instance_id) { - throw new PluginException("The checkout flow '{$this->entityId}' did not specify a plugin."); - } - - $configuration = ['_entity_id' => $this->entityId] + $this->configuration; - $plugin = $this->manager->createInstance($instance_id, $configuration); - $this->set($instance_id, $plugin); - } - -} diff --git a/modules/checkout/src/Entity/CheckoutFlow.php b/modules/checkout/src/Entity/CheckoutFlow.php index 030f0db866..785a061b8d 100644 --- a/modules/checkout/src/Entity/CheckoutFlow.php +++ b/modules/checkout/src/Entity/CheckoutFlow.php @@ -2,7 +2,7 @@ namespace Drupal\commerce_checkout\Entity; -use Drupal\commerce_checkout\CheckoutFlowPluginCollection; +use Drupal\commerce\CommerceSinglePluginCollection; use Drupal\Core\Config\Entity\ConfigEntityBase; /** @@ -83,7 +83,7 @@ class CheckoutFlow extends ConfigEntityBase implements CheckoutFlowInterface { /** * The plugin collection that holds the checkout flow plugin. * - * @var \Drupal\commerce_checkout\CheckoutFlowPluginCollection + * @var \Drupal\commerce\CommerceSinglePluginCollection */ protected $pluginCollection; @@ -138,13 +138,13 @@ public function set($property_name, $value) { * * Ensures the plugin collection is initialized before returning it. * - * @return \Drupal\commerce_checkout\CheckoutFlowPluginCollection + * @return \Drupal\commerce\CommerceSinglePluginCollection * The plugin collection. */ protected function getPluginCollection() { if (!$this->pluginCollection) { $plugin_manager = \Drupal::service('plugin.manager.commerce_checkout_flow'); - $this->pluginCollection = new CheckoutFlowPluginCollection($plugin_manager, $this->plugin, $this->configuration, $this->id); + $this->pluginCollection = new CommerceSinglePluginCollection($plugin_manager, $this->plugin, $this->configuration, $this->id); } return $this->pluginCollection; } diff --git a/modules/checkout/src/Form/CheckoutFlowForm.php b/modules/checkout/src/Form/CheckoutFlowForm.php index a7864577f4..654be162b7 100644 --- a/modules/checkout/src/Form/CheckoutFlowForm.php +++ b/modules/checkout/src/Form/CheckoutFlowForm.php @@ -4,7 +4,6 @@ use Drupal\commerce\Form\CommercePluginEntityFormBase; use Drupal\commerce_checkout\CheckoutFlowManager; -use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Form\FormStateInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -45,6 +44,12 @@ public function form(array $form, FormStateInterface $form_state) { $checkout_flow = $this->entity; $plugins = array_column($this->pluginManager->getDefinitions(), 'label', 'id'); asort($plugins); + // Use the first available plugin as the default value. + if (!$checkout_flow->getPluginId()) { + $plugin_ids = array_keys($plugins); + $plugin = reset($plugin_ids); + $checkout_flow->setPluginId($plugin); + } $form['#tree'] = TRUE; $form['#attached']['library'][] = 'commerce_checkout/admin'; @@ -63,7 +68,7 @@ public function form(array $form, FormStateInterface $form_state) { ], ]; $form['plugin'] = [ - '#type' => 'select', + '#type' => 'radios', '#title' => $this->t('Plugin'), '#options' => $plugins, '#default_value' => $checkout_flow->getPluginId(), @@ -80,18 +85,6 @@ public function form(array $form, FormStateInterface $form_state) { return $this->protectPluginIdElement($form); } - /** - * {@inheritdoc} - */ - protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) { - /** @var \Drupal\commerce_checkout\Entity\CheckoutFlowInterface $entity */ - // The parent method tries to initialize the plugin collection before - // setting the plugin. - $entity->setPluginId($form_state->getValue('plugin')); - - parent::copyFormValuesToEntity($entity, $form, $form_state); - } - /** * {@inheritdoc} */ diff --git a/modules/payment/src/Entity/PaymentGateway.php b/modules/payment/src/Entity/PaymentGateway.php index 072b2f772f..f93a1fbaae 100644 --- a/modules/payment/src/Entity/PaymentGateway.php +++ b/modules/payment/src/Entity/PaymentGateway.php @@ -2,9 +2,9 @@ namespace Drupal\commerce_payment\Entity; +use Drupal\commerce\CommerceSinglePluginCollection; use Drupal\commerce\ConditionGroup; use Drupal\commerce_order\Entity\OrderInterface; -use Drupal\commerce_payment\PaymentGatewayPluginCollection; use Drupal\Core\Config\Entity\ConfigEntityBase; /** @@ -113,7 +113,7 @@ class PaymentGateway extends ConfigEntityBase implements PaymentGatewayInterface /** * The plugin collection that holds the payment gateway plugin. * - * @var \Drupal\commerce_payment\PaymentGatewayPluginCollection + * @var \Drupal\commerce\CommerceSinglePluginCollection */ protected $pluginCollection; @@ -247,13 +247,13 @@ public function set($property_name, $value) { * * Ensures the plugin collection is initialized before returning it. * - * @return \Drupal\commerce_payment\PaymentGatewayPluginCollection + * @return \Drupal\commerce\CommerceSinglePluginCollection * The plugin collection. */ protected function getPluginCollection() { if (!$this->pluginCollection) { $plugin_manager = \Drupal::service('plugin.manager.commerce_payment_gateway'); - $this->pluginCollection = new PaymentGatewayPluginCollection($plugin_manager, $this->plugin, $this->configuration, $this->id); + $this->pluginCollection = new CommerceSinglePluginCollection($plugin_manager, $this->plugin, $this->configuration, $this->id); } return $this->pluginCollection; } diff --git a/modules/payment/src/PaymentGatewayPluginCollection.php b/modules/payment/src/PaymentGatewayPluginCollection.php deleted file mode 100644 index 6aff7d1f28..0000000000 --- a/modules/payment/src/PaymentGatewayPluginCollection.php +++ /dev/null @@ -1,62 +0,0 @@ -entityId = $entity_id; - } - - /** - * {@inheritdoc} - * - * @return \Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\PaymentGatewayInterface - * The payment gateway plugin. - */ - public function &get($instance_id) { - return parent::get($instance_id); - } - - /** - * {@inheritdoc} - */ - protected function initializePlugin($instance_id) { - if (!$instance_id) { - throw new PluginException("The payment gateway '{$this->entityId}' did not specify a plugin."); - } - - $configuration = ['_entity_id' => $this->entityId] + $this->configuration; - $plugin = $this->manager->createInstance($instance_id, $configuration); - $this->set($instance_id, $plugin); - } - -} diff --git a/modules/payment/tests/src/Unit/CreditCardTest.php b/modules/payment/tests/src/Unit/CreditCardTest.php index b983abcd32..a20f07bf9c 100644 --- a/modules/payment/tests/src/Unit/CreditCardTest.php +++ b/modules/payment/tests/src/Unit/CreditCardTest.php @@ -4,12 +4,13 @@ use Drupal\commerce_payment\CreditCard; use Drupal\commerce_payment\CreditCardType; +use Drupal\Tests\UnitTestCase; /** * @coversDefaultClass \Drupal\commerce_payment\CreditCard * @group commerce */ -class CreditCardTest extends \PHPUnit_Framework_TestCase { +class CreditCardTest extends UnitTestCase { /** * @covers ::getTypes diff --git a/modules/tax/src/Entity/TaxType.php b/modules/tax/src/Entity/TaxType.php index 2d93a7418b..2a09314c3f 100644 --- a/modules/tax/src/Entity/TaxType.php +++ b/modules/tax/src/Entity/TaxType.php @@ -2,7 +2,7 @@ namespace Drupal\commerce_tax\Entity; -use Drupal\commerce_tax\TaxTypePluginCollection; +use Drupal\commerce\CommerceSinglePluginCollection; use Drupal\Core\Config\Entity\ConfigEntityBase; /** @@ -83,7 +83,7 @@ class TaxType extends ConfigEntityBase implements TaxTypeInterface { /** * The plugin collection that holds the tax type plugin. * - * @var \Drupal\commerce_tax\TaxTypePluginCollection + * @var \Drupal\commerce\CommerceSinglePluginCollection */ protected $pluginCollection; @@ -157,13 +157,13 @@ public function set($property_name, $value) { * * Ensures the plugin collection is initialized before returning it. * - * @return \Drupal\commerce_tax\TaxTypePluginCollection + * @return \Drupal\commerce\CommerceSinglePluginCollection * The plugin collection. */ protected function getPluginCollection() { if (!$this->pluginCollection) { $plugin_manager = \Drupal::service('plugin.manager.commerce_tax_type'); - $this->pluginCollection = new TaxTypePluginCollection($plugin_manager, $this->plugin, $this->configuration, $this->id); + $this->pluginCollection = new CommerceSinglePluginCollection($plugin_manager, $this->plugin, $this->configuration, $this->id); } return $this->pluginCollection; } diff --git a/modules/tax/src/TaxTypePluginCollection.php b/src/CommerceSinglePluginCollection.php similarity index 65% rename from modules/tax/src/TaxTypePluginCollection.php rename to src/CommerceSinglePluginCollection.php index 064512237d..463580fa12 100644 --- a/modules/tax/src/TaxTypePluginCollection.php +++ b/src/CommerceSinglePluginCollection.php @@ -1,25 +1,25 @@ entityId = $entity_id; + // The parent constructor initializes the plugin, so it needs to be called + // after $this->entityId is set. + parent::__construct($manager, $instance_id, $configuration); } /** @@ -41,7 +42,7 @@ public function __construct(PluginManagerInterface $manager, $instance_id, array */ protected function initializePlugin($instance_id) { if (!$instance_id) { - throw new PluginException("The tax type '{$this->entityId}' did not specify a plugin."); + throw new PluginException("The entity '{$this->entityId}' did not specify a plugin."); } $configuration = ['_entity_id' => $this->entityId] + $this->configuration;