Skip to content

Commit

Permalink
Issue #2904570 by edwardaa, bojanz: $this->entityId always NULL in pl…
Browse files Browse the repository at this point in the history
…ugins in Drupal 8.4.x
  • Loading branch information
bojanz committed Sep 12, 2017
1 parent 2db51c7 commit 3bc1b88
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 160 deletions.
62 changes: 0 additions & 62 deletions modules/checkout/src/CheckoutFlowPluginCollection.php

This file was deleted.

8 changes: 4 additions & 4 deletions modules/checkout/src/Entity/CheckoutFlow.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Drupal\commerce_checkout\Entity;

use Drupal\commerce_checkout\CheckoutFlowPluginCollection;
use Drupal\commerce\CommerceSinglePluginCollection;
use Drupal\Core\Config\Entity\ConfigEntityBase;

/**
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
Expand Down
21 changes: 7 additions & 14 deletions modules/checkout/src/Form/CheckoutFlowForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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';
Expand All @@ -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(),
Expand All @@ -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}
*/
Expand Down
8 changes: 4 additions & 4 deletions modules/payment/src/Entity/PaymentGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
Expand Down
62 changes: 0 additions & 62 deletions modules/payment/src/PaymentGatewayPluginCollection.php

This file was deleted.

3 changes: 2 additions & 1 deletion modules/payment/tests/src/Unit/CreditCardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions modules/tax/src/Entity/TaxType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Drupal\commerce_tax\Entity;

use Drupal\commerce_tax\TaxTypePluginCollection;
use Drupal\commerce\CommerceSinglePluginCollection;
use Drupal\Core\Config\Entity\ConfigEntityBase;

/**
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
<?php

namespace Drupal\commerce_tax;
namespace Drupal\commerce;

use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Core\Plugin\DefaultSingleLazyPluginCollection;

/**
* A collection of tax type plugins.
* A collection that stores a single plugin, aware of its parent entity ID.
*/
class TaxTypePluginCollection extends DefaultSingleLazyPluginCollection {
class CommerceSinglePluginCollection extends DefaultSingleLazyPluginCollection {

/**
* The tax type entity ID this plugin collection belongs to.
* The entity ID this plugin collection belongs to.
*
* @var string
*/
protected $entityId;

/**
* Constructs a new TaxTypePluginCollection object.
* Constructs a new CommerceSinglePluginCollection object.
*
* @param \Drupal\Component\Plugin\PluginManagerInterface $manager
* The manager to be used for instantiating plugins.
Expand All @@ -28,20 +28,21 @@ class TaxTypePluginCollection extends DefaultSingleLazyPluginCollection {
* @param array $configuration
* An array of configuration.
* @param string $entity_id
* The tax type entity ID this plugin collection belongs to.
* The entity ID this plugin collection belongs to.
*/
public function __construct(PluginManagerInterface $manager, $instance_id, array $configuration, $entity_id) {
parent::__construct($manager, $instance_id, $configuration);

$this->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);
}

/**
* {@inheritdoc}
*/
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;
Expand Down

0 comments on commit 3bc1b88

Please sign in to comment.