From 4366f38aa7bbd0d36bde3cd3a039d6d2da7e931d Mon Sep 17 00:00:00 2001 From: Bojan Zivanovic Date: Mon, 12 Jun 2017 14:35:13 +0200 Subject: [PATCH] Issue #2884870 by bojanz: Plugin configuration is not validated/submitted by the plugin that built the form --- modules/payment/src/Entity/PaymentGateway.php | 16 +++ .../src/Entity/PaymentGatewayInterface.php | 18 +++ .../payment/src/Form/PaymentGatewayForm.php | 19 +-- .../PaymentGateway/PaymentGatewayBase.php | 9 +- .../src/Functional/PaymentGatewayTest.php | 8 +- .../FunctionalJavascript/PromotionTest.php | 16 +-- modules/tax/src/Entity/TaxType.php | 16 +++ modules/tax/src/Entity/TaxTypeInterface.php | 18 +++ modules/tax/src/Form/TaxTypeForm.php | 19 +-- .../src/Plugin/Commerce/TaxType/Custom.php | 9 +- .../Plugin/Commerce/TaxType/TaxTypeBase.php | 9 +- src/Element/PluginConfiguration.php | 135 ++++++++++++++++++ src/Element/PluginSelect.php | 87 ++--------- ...luginItemTest.php => PluginSelectTest.php} | 19 +-- 14 files changed, 261 insertions(+), 137 deletions(-) create mode 100644 src/Element/PluginConfiguration.php rename tests/src/FunctionalJavascript/{PluginItemTest.php => PluginSelectTest.php} (95%) diff --git a/modules/payment/src/Entity/PaymentGateway.php b/modules/payment/src/Entity/PaymentGateway.php index 7fdd86e1e3..019bd1d7e7 100644 --- a/modules/payment/src/Entity/PaymentGateway.php +++ b/modules/payment/src/Entity/PaymentGateway.php @@ -137,6 +137,22 @@ public function setPluginId($plugin_id) { return $this; } + /** + * {@inheritdoc} + */ + public function getPluginConfiguration() { + return $this->configuration; + } + + /** + * {@inheritdoc} + */ + public function setPluginConfiguration(array $configuration) { + $this->configuration = $configuration; + $this->pluginCollection = NULL; + return $this; + } + /** * {@inheritdoc} */ diff --git a/modules/payment/src/Entity/PaymentGatewayInterface.php b/modules/payment/src/Entity/PaymentGatewayInterface.php index 1ab41001d5..3197cd7685 100644 --- a/modules/payment/src/Entity/PaymentGatewayInterface.php +++ b/modules/payment/src/Entity/PaymentGatewayInterface.php @@ -56,4 +56,22 @@ public function getPluginId(); */ public function setPluginId($plugin_id); + /** + * Gets the payment gateway plugin configuration. + * + * @return string + * The payment gateway plugin configuration. + */ + public function getPluginConfiguration(); + + /** + * Sets the payment gateway plugin configuration. + * + * @param array $configuration + * The payment gateway plugin configuration. + * + * @return $this + */ + public function setPluginConfiguration(array $configuration); + } diff --git a/modules/payment/src/Form/PaymentGatewayForm.php b/modules/payment/src/Form/PaymentGatewayForm.php index b312ea44ca..9a83055100 100644 --- a/modules/payment/src/Form/PaymentGatewayForm.php +++ b/modules/payment/src/Form/PaymentGatewayForm.php @@ -102,9 +102,11 @@ public function form(array $form, FormStateInterface $form_state) { ], ]; $form['configuration'] = [ - '#parents' => ['configuration'], + '#type' => 'commerce_plugin_configuration', + '#plugin_type' => 'commerce_payment_gateway', + '#plugin_id' => $plugin, + '#default_value' => $gateway->getPluginConfiguration(), ]; - $form['configuration'] = $gateway->getPlugin()->buildConfigurationForm($form['configuration'], $form_state); $form['status'] = [ '#type' => 'checkbox', '#title' => $this->t('Enabled'), @@ -121,17 +123,6 @@ public static function ajaxRefresh(array $form, FormStateInterface $form_state) return $form; } - /** - * {@inheritdoc} - */ - public function validateForm(array &$form, FormStateInterface $form_state) { - parent::validateForm($form, $form_state); - - /** @var \Drupal\commerce_payment\Entity\PaymentGatewayInterface $gateway */ - $gateway = $this->entity; - $gateway->getPlugin()->validateConfigurationForm($form['configuration'], $form_state); - } - /** * {@inheritdoc} */ @@ -140,7 +131,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) { /** @var \Drupal\commerce_payment\Entity\PaymentGatewayInterface $gateway */ $gateway = $this->entity; - $gateway->getPlugin()->submitConfigurationForm($form['configuration'], $form_state); + $gateway->setPluginConfiguration($form_state->getValue(['configuration'])); } /** diff --git a/modules/payment/src/Plugin/Commerce/PaymentGateway/PaymentGatewayBase.php b/modules/payment/src/Plugin/Commerce/PaymentGateway/PaymentGatewayBase.php index fbcf0e424c..04a340cda8 100644 --- a/modules/payment/src/Plugin/Commerce/PaymentGateway/PaymentGatewayBase.php +++ b/modules/payment/src/Plugin/Commerce/PaymentGateway/PaymentGatewayBase.php @@ -35,6 +35,8 @@ abstract class PaymentGatewayBase extends PluginBase implements PaymentGatewayIn /** * The ID of the parent config entity. * + * Not available while the plugin is being configured. + * * @var string */ protected $entityId; @@ -73,9 +75,10 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition parent::__construct($configuration, $plugin_id, $plugin_definition); $this->entityTypeManager = $entity_type_manager; - // The plugin most know the ID of its parent config entity. - $this->entityId = $configuration['_entity_id']; - unset($configuration['_entity_id']); + if (isset($configuration['_entity_id'])) { + $this->entityId = $configuration['_entity_id']; + unset($configuration['_entity_id']); + } // Instantiate the types right away to ensure that their IDs are valid. $this->paymentType = $payment_type_manager->createInstance($this->pluginDefinition['payment_type']); foreach ($this->pluginDefinition['payment_method_types'] as $plugin_id) { diff --git a/modules/payment/tests/src/Functional/PaymentGatewayTest.php b/modules/payment/tests/src/Functional/PaymentGatewayTest.php index a7303918af..4088e9bef1 100644 --- a/modules/payment/tests/src/Functional/PaymentGatewayTest.php +++ b/modules/payment/tests/src/Functional/PaymentGatewayTest.php @@ -39,8 +39,8 @@ public function testPaymentGatewayCreation() { $values = [ 'label' => 'Example', 'plugin' => 'example_offsite_redirect', - 'configuration[redirect_method]' => 'post', - 'configuration[mode]' => 'test', + 'configuration[form][redirect_method]' => 'post', + 'configuration[form][mode]' => 'test', 'status' => '1', // Setting the 'id' can fail if focus switches to another field. // This is a bug in the machine name JS that can be reproduced manually. @@ -76,8 +76,8 @@ public function testPaymentGatewayEditing() { $this->drupalGet('admin/commerce/config/payment-gateways/manage/' . $payment_gateway->id()); $values += [ - 'configuration[redirect_method]' => 'get', - 'configuration[mode]' => 'live', + 'configuration[form][redirect_method]' => 'get', + 'configuration[form][mode]' => 'live', ]; $this->submitForm($values, 'Save'); diff --git a/modules/promotion/tests/src/FunctionalJavascript/PromotionTest.php b/modules/promotion/tests/src/FunctionalJavascript/PromotionTest.php index 791bca29ee..df9339406b 100644 --- a/modules/promotion/tests/src/FunctionalJavascript/PromotionTest.php +++ b/modules/promotion/tests/src/FunctionalJavascript/PromotionTest.php @@ -51,18 +51,18 @@ public function testCreatePromotion() { $this->getSession()->getPage()->selectFieldOption('offer[0][plugin_select][target_plugin_id]', 'commerce_promotion_product_percentage_off'); $this->waitForAjaxToFinish(); - $this->getSession()->getPage()->fillField('offer[0][plugin_select][target_plugin_configuration][amount]', '10.0'); + $this->getSession()->getPage()->fillField('offer[0][plugin_select][target_plugin_configuration][form][amount]', '10.0'); // Change, assert any values reset. $this->getSession()->getPage()->selectFieldOption('offer[0][plugin_select][target_plugin_id]', 'commerce_promotion_order_percentage_off'); $this->waitForAjaxToFinish(); - $this->assertSession()->fieldValueNotEquals('offer[0][plugin_select][target_plugin_configuration][amount]', '10.0'); - $this->getSession()->getPage()->fillField('offer[0][plugin_select][target_plugin_configuration][amount]', '10.0'); + $this->assertSession()->fieldValueNotEquals('offer[0][plugin_select][target_plugin_configuration][form][amount]', '10.0'); + $this->getSession()->getPage()->fillField('offer[0][plugin_select][target_plugin_configuration][form][amount]', '10.0'); $this->getSession()->getPage()->selectFieldOption('conditions[0][plugin_select][target_plugin_id]', 'commerce_promotion_order_total_price'); $this->waitForAjaxToFinish(); - $this->getSession()->getPage()->fillField('conditions[0][plugin_select][target_plugin_configuration][amount][number]', '50.00'); - $this->getSession()->getPage()->checkField('conditions[0][plugin_select][target_plugin_configuration][negate]'); + $this->getSession()->getPage()->fillField('conditions[0][plugin_select][target_plugin_configuration][form][amount][number]', '50.00'); + $this->getSession()->getPage()->checkField('conditions[0][plugin_select][target_plugin_configuration][form][negate]'); // Confirm that the usage limit widget works properly. $this->getSession()->getPage()->hasCheckedField(' Unlimited'); @@ -110,13 +110,13 @@ public function testCreatePromotionWithEndDate() { $name = $this->randomMachineName(8); $edit = [ 'name[0][value]' => $name, - 'offer[0][plugin_select][target_plugin_configuration][amount]' => '10.0', + 'offer[0][plugin_select][target_plugin_configuration][form][amount]' => '10.0', ]; $this->getSession()->getPage()->fillField('conditions[0][plugin_select][target_plugin_id]', 'commerce_promotion_order_total_price'); $this->waitForAjaxToFinish(); - $edit['conditions[0][plugin_select][target_plugin_configuration][amount][number]'] = '50.00'; + $edit['conditions[0][plugin_select][target_plugin_configuration][form][amount][number]'] = '50.00'; // Set an end date. $this->getSession()->getPage()->checkField('end_date[0][has_value]'); @@ -155,7 +155,7 @@ public function testEditPromotion() { $new_promotion_name = $this->randomMachineName(8); $edit = [ 'name[0][value]' => $new_promotion_name, - 'offer[0][plugin_select][target_plugin_configuration][amount]' => '20', + 'offer[0][plugin_select][target_plugin_configuration][form][amount]' => '20', ]; $this->submitForm($edit, 'Save'); diff --git a/modules/tax/src/Entity/TaxType.php b/modules/tax/src/Entity/TaxType.php index 54f79718d4..b8282bd1aa 100644 --- a/modules/tax/src/Entity/TaxType.php +++ b/modules/tax/src/Entity/TaxType.php @@ -110,6 +110,22 @@ public function setPluginId($plugin_id) { return $this; } + /** + * {@inheritdoc} + */ + public function getPluginConfiguration() { + return $this->configuration; + } + + /** + * {@inheritdoc} + */ + public function setPluginConfiguration(array $configuration) { + $this->configuration = $configuration; + $this->pluginCollection = NULL; + return $this; + } + /** * {@inheritdoc} */ diff --git a/modules/tax/src/Entity/TaxTypeInterface.php b/modules/tax/src/Entity/TaxTypeInterface.php index 1364df0d77..9438ca7e09 100644 --- a/modules/tax/src/Entity/TaxTypeInterface.php +++ b/modules/tax/src/Entity/TaxTypeInterface.php @@ -38,4 +38,22 @@ public function getPluginId(); */ public function setPluginId($plugin_id); + /** + * Gets the tax type plugin configuration. + * + * @return string + * The tax type plugin configuration. + */ + public function getPluginConfiguration(); + + /** + * Sets the tax type plugin configuration. + * + * @param array $configuration + * The tax type plugin configuration. + * + * @return $this + */ + public function setPluginConfiguration(array $configuration); + } diff --git a/modules/tax/src/Form/TaxTypeForm.php b/modules/tax/src/Form/TaxTypeForm.php index 3bee6479f3..edc2b12ce1 100644 --- a/modules/tax/src/Form/TaxTypeForm.php +++ b/modules/tax/src/Form/TaxTypeForm.php @@ -92,9 +92,11 @@ public function form(array $form, FormStateInterface $form_state) { ], ]; $form['configuration'] = [ - '#parents' => ['configuration'], + '#type' => 'commerce_plugin_configuration', + '#plugin_type' => 'commerce_tax_type', + '#plugin_id' => $plugin, + '#default_value' => $type->getPluginConfiguration(), ]; - $form['configuration'] = $type->getPlugin()->buildConfigurationForm($form['configuration'], $form_state); $form['status'] = [ '#type' => 'checkbox', '#title' => $this->t('Enabled'), @@ -111,17 +113,6 @@ public static function ajaxRefresh(array $form, FormStateInterface $form_state) return $form; } - /** - * {@inheritdoc} - */ - public function validateForm(array &$form, FormStateInterface $form_state) { - parent::validateForm($form, $form_state); - - /** @var \Drupal\commerce_tax\Entity\TaxTypeInterface $type */ - $type = $this->entity; - $type->getPlugin()->validateConfigurationForm($form['configuration'], $form_state); - } - /** * {@inheritdoc} */ @@ -130,7 +121,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) { /** @var \Drupal\commerce_tax\Entity\TaxTypeInterface $type */ $type = $this->entity; - $type->getPlugin()->submitConfigurationForm($form['configuration'], $form_state); + $type->setPluginConfiguration($form_state->getValue(['configuration'])); } /** diff --git a/modules/tax/src/Plugin/Commerce/TaxType/Custom.php b/modules/tax/src/Plugin/Commerce/TaxType/Custom.php index b8d851959a..583aba192c 100644 --- a/modules/tax/src/Plugin/Commerce/TaxType/Custom.php +++ b/modules/tax/src/Plugin/Commerce/TaxType/Custom.php @@ -273,19 +273,16 @@ public function removeTerritorySubmit(array $form, FormStateInterface $form_stat * {@inheritdoc} */ public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { - if (!isset($form['territories'])) { - // The form was built by a different plugin, and is now in the process - // of being rebuilt. Temporary workaround for #2884870. - return; - } - $values = $form_state->getValue($form['#parents']); + // Filter out the button rows. $values['rates'] = array_filter($values['rates'], function ($rate) { return !empty($rate) && !isset($rate['add_rate']); }); $values['territories'] = array_filter($values['territories'], function ($territory) { return !empty($territory) && !isset($territory['add_territory']); }); + $form_state->setValue($form['#parents'], $values); + if (empty($values['rates'])) { $form_state->setError($form['rates'], $this->t('Please add at least one rate.')); } diff --git a/modules/tax/src/Plugin/Commerce/TaxType/TaxTypeBase.php b/modules/tax/src/Plugin/Commerce/TaxType/TaxTypeBase.php index a8a3e0d55f..45a0b39d52 100644 --- a/modules/tax/src/Plugin/Commerce/TaxType/TaxTypeBase.php +++ b/modules/tax/src/Plugin/Commerce/TaxType/TaxTypeBase.php @@ -39,6 +39,8 @@ abstract class TaxTypeBase extends PluginBase implements TaxTypeInterface, Conta /** * The ID of the parent config entity. * + * Not available while the plugin is being configured. + * * @var string */ protected $entityId; @@ -69,9 +71,10 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition $this->entityTypeManager = $entity_type_manager; $this->eventDispatcher = $event_dispatcher; - // The plugin most know the ID of its parent config entity. - $this->entityId = $configuration['_entity_id']; - unset($configuration['_entity_id']); + if (isset($configuration['_entity_id'])) { + $this->entityId = $configuration['_entity_id']; + unset($configuration['_entity_id']); + } $this->setConfiguration($configuration); } diff --git a/src/Element/PluginConfiguration.php b/src/Element/PluginConfiguration.php new file mode 100644 index 0000000000..ffa58e5eac --- /dev/null +++ b/src/Element/PluginConfiguration.php @@ -0,0 +1,135 @@ + 'commerce_plugin_configuration', + * '#plugin_type' => 'commerce_promotion', + * '#plugin_id' => 'order_total_price', + * '#default_value' => [ + * 'operator' => '<', + * 'amount' => [ + * 'number' => '10.00', + * 'currency_code' => 'USD', + * ], + * ], + * ]; + * @endcode + * + * @FormElement("commerce_plugin_configuration") + */ +class PluginConfiguration extends FormElement { + + use CommerceElementTrait; + + /** + * {@inheritdoc} + */ + public function getInfo() { + $class = get_class($this); + return [ + '#plugin_type' => NULL, + '#plugin_id' => NULL, + '#default_value' => [], + + '#process' => [ + [$class, 'attachElementSubmit'], + [$class, 'processPluginConfiguration'], + [$class, 'processAjaxForm'], + ], + '#element_validate' => [ + [$class, 'validateElementSubmit'], + [$class, 'validatePluginConfiguration'], + ], + '#commerce_element_submit' => [ + [$class, 'submitPluginConfiguration'], + ], + '#theme_wrappers' => ['container'], + ]; + } + + /** + * Processes the plugin configuration form element. + * + * @param array $element + * The form element to process. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The current state of the form. + * @param array $complete_form + * The complete form structure. + * + * @return array + * The processed element. + * + * @throws \InvalidArgumentException + * Thrown for missing #plugin_type or malformed #default_value properties. + */ + public static function processPluginConfiguration(array &$element, FormStateInterface $form_state, array &$complete_form) { + if (empty($element['#plugin_type'])) { + throw new \InvalidArgumentException('The commerce_plugin_configuration #plugin_type property is required.'); + } + if (!is_array($element['#default_value'])) { + throw new \InvalidArgumentException('The commerce_plugin_configuration #default_value must be an array.'); + } + + $element['#tree'] = TRUE; + /** @var \Drupal\Core\Executable\ExecutableManagerInterface $plugin_manager */ + $plugin_manager = \Drupal::service('plugin.manager.' . $element['#plugin_type']); + if (!empty($element['#plugin_id'])) { + /** @var \Drupal\Core\Plugin\PluginFormInterface $plugin */ + $plugin = $plugin_manager->createInstance($element['#plugin_id'], $element['#default_value']); + $element['form'] = [ + '#parents' => array_merge($element['#parents'], ['form']), + ]; + $element['form'] = $plugin->buildConfigurationForm($element['form'], $form_state); + } + + return $element; + } + + /** + * Validates the plugin configuration. + * + * @param array $element + * An associative array containing the properties of the element. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The current state of the form. + * @param array $complete_form + * The complete form structure. + */ + public static function validatePluginConfiguration(array &$element, FormStateInterface $form_state, array &$complete_form) { + if (!empty($element['#plugin_id'])) { + /** @var \Drupal\Component\Plugin\PluginManagerInterface $plugin_manager */ + $plugin_manager = \Drupal::service('plugin.manager.' . $element['#plugin_type']); + $plugin = $plugin_manager->createInstance($element['#plugin_id'], $element['#default_value']); + $plugin->validateConfigurationForm($element['form'], $form_state); + } + } + + /** + * Submits the plugin configuration. + * + * @param array $element + * An associative array containing the properties of the element. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The current state of the form. + */ + public static function submitPluginConfiguration(array &$element, FormStateInterface $form_state) { + if (!empty($element['#plugin_id'])) { + /** @var \Drupal\Component\Plugin\PluginManagerInterface $plugin_manager */ + $plugin_manager = \Drupal::service('plugin.manager.' . $element['#plugin_type']); + $plugin = $plugin_manager->createInstance($element['#plugin_id'], $element['#default_value']); + $plugin->submitConfigurationForm($element['form'], $form_state); + $form_state->setValueForElement($element, $plugin->getConfiguration()); + } + } + +} diff --git a/src/Element/PluginSelect.php b/src/Element/PluginSelect.php index 9fa3da1802..113268a2f4 100644 --- a/src/Element/PluginSelect.php +++ b/src/Element/PluginSelect.php @@ -5,18 +5,12 @@ use Drupal\Component\Utility\Html; use Drupal\Component\Utility\NestedArray; use Drupal\Core\Form\FormStateInterface; -use Drupal\Core\Plugin\PluginFormInterface; use Drupal\Core\Render\Element\FormElement; use Symfony\Component\HttpFoundation\Request; /** * Element for selecting plugins, and configuring them. * - * @FormElement("commerce_plugin_select") - * - * Properties: - * - #providers: Modules to restrict options to. - * * Usage example: * @code * $form['plugin_item'] = [ @@ -25,6 +19,8 @@ * '#categories' => ['user', 'system'], * ]; * @endcode + * + * @FormElement("commerce_plugin_select") */ class PluginSelect extends FormElement { @@ -63,7 +59,7 @@ public function getInfo() { /** * Process callback. */ - public static function processPluginSelect(&$element, FormStateInterface $form_state, &$complete_form) { + public static function processPluginSelect(array &$element, FormStateInterface $form_state, array &$complete_form) { if (!$element['#plugin_type']) { throw new \InvalidArgumentException('You must specify the plugin type ID.'); } @@ -91,9 +87,8 @@ public static function processPluginSelect(&$element, FormStateInterface $form_s '#default_value' => $target_plugin_id, '#required' => $element['#required'], ]; - // Add a "_none" option if the element is not required. if (!$element['#required']) { - $element['target_plugin_id']['#options']['_none'] = t('None'); + $element['target_plugin_id']['#empty_value'] = ''; } /** @var \Drupal\Core\Executable\ExecutableManagerInterface $plugin_manager */ @@ -116,27 +111,19 @@ public static function processPluginSelect(&$element, FormStateInterface $form_s } $definitions[] = $definition['id']; } - // If the element is required, set the default value to the first plugin. // definition available in the options array. - if ($element['#required'] && $target_plugin_id == '_none' && !empty($element['target_plugin_id']['#options'])) { + if ($element['#required'] && !$target_plugin_id && !empty($element['target_plugin_id']['#options'])) { $target_plugin_id = reset($definitions); $element['target_plugin_id']['#default_value'] = $target_plugin_id; } $element['target_plugin_configuration'] = [ - '#type' => 'container', + '#type' => 'commerce_plugin_configuration', + '#plugin_type' => $element['#plugin_type'], + '#plugin_id' => $target_plugin_id, + '#default_value' => $values['target_plugin_configuration'], ]; - if (!empty($target_plugin_id) && $target_plugin_id != '_none') { - /** @var \Drupal\Core\Executable\ExecutableInterface $plugin */ - $plugin = $plugin_manager->createInstance($target_plugin_id, $values['target_plugin_configuration']); - if ($plugin instanceof PluginFormInterface) { - $element['target_plugin_configuration'] = [ - '#tree' => TRUE, - ]; - $element['target_plugin_configuration'] = $plugin->buildConfigurationForm($element['target_plugin_configuration'], $form_state); - } - } return $element; } @@ -146,11 +133,10 @@ public static function processPluginSelect(&$element, FormStateInterface $form_s */ public static function ajaxRefresh(&$form, FormStateInterface $form_state, Request $request) { $target_plugin_id_element = $form_state->getTriggeringElement(); - // Radios are an extra parent deep compared to the select. $slice_length = ($target_plugin_id_element['#type'] == 'radio') ? -2 : -1; - $plugin_select_element = NestedArray::getValue($form, array_slice($target_plugin_id_element['#array_parents'], 0, $slice_length)); + return $plugin_select_element; } @@ -162,7 +148,7 @@ public static function valueCallback(&$element, $input, FormStateInterface $form $input = $element['#default_value']; } if (empty($input['target_plugin_id'])) { - $input['target_plugin_id'] = '_none'; + $input['target_plugin_id'] = ''; } if (empty($input['target_plugin_configuration'])) { $input['target_plugin_configuration'] = []; @@ -171,57 +157,6 @@ public static function valueCallback(&$element, $input, FormStateInterface $form return $input; } - /** - * Validates the plugin's configuration. - * - * @param array $element - * An associative array containing the properties of the element. - * @param \Drupal\Core\Form\FormStateInterface $form_state - * The current state of the form. - * @param array $complete_form - * The complete form structure. - */ - public static function validatePlugin(array &$element, FormStateInterface $form_state, array &$complete_form) { - $values = $form_state->getValue($element['#parents']); - $target_plugin_id = $values['target_plugin_id']; - // If a plugin was selected, create an instance and pass the configuration - // values to its configuration form validation method. - if ($target_plugin_id != '_none') { - /** @var \Drupal\Component\Plugin\PluginManagerInterface $plugin_manager */ - $plugin_manager = \Drupal::service('plugin.manager.' . $element['#plugin_type']); - $plugin = $plugin_manager->createInstance($target_plugin_id, $element['#default_value']['target_plugin_configuration']); - if ($plugin instanceof PluginFormInterface) { - $plugin->validateConfigurationForm($element['target_plugin_configuration'], $form_state); - } - } - } - - /** - * Submits the plugin's configuration. - * - * @param array $element - * An associative array containing the properties of the element. - * @param \Drupal\Core\Form\FormStateInterface $form_state - * The current state of the form. - */ - public static function submitPlugin(array &$element, FormStateInterface $form_state) { - $values = $form_state->getValue($element['#parents']); - $target_plugin_id = $values['target_plugin_id']; - // If a plugin was selected, create an instance and pass the configuration - // values to its configuration form submission method. - if ($target_plugin_id != '_none') { - /** @var \Drupal\Component\Plugin\PluginManagerInterface $plugin_manager */ - $plugin_manager = \Drupal::service('plugin.manager.' . $element['#plugin_type']); - $plugin = $plugin_manager->createInstance($target_plugin_id, $element['#default_value']['target_plugin_configuration']); - if ($plugin instanceof PluginFormInterface) { - /** @var \Drupal\Component\Plugin\ConfigurablePluginInterface $plugin */ - $plugin->submitConfigurationForm($element['target_plugin_configuration'], $form_state); - $values['target_plugin_configuration'] = $plugin->getConfiguration(); - $form_state->setValueForElement($element, $values); - } - } - } - /** * Clears the plugin-specific form values when the target plugin changes. * diff --git a/tests/src/FunctionalJavascript/PluginItemTest.php b/tests/src/FunctionalJavascript/PluginSelectTest.php similarity index 95% rename from tests/src/FunctionalJavascript/PluginItemTest.php rename to tests/src/FunctionalJavascript/PluginSelectTest.php index 9bafc1f994..20e1ae1212 100644 --- a/tests/src/FunctionalJavascript/PluginItemTest.php +++ b/tests/src/FunctionalJavascript/PluginSelectTest.php @@ -14,7 +14,7 @@ * * @group commerce */ -class PluginItemTest extends CommerceBrowserTestBase { +class PluginSelectTest extends CommerceBrowserTestBase { use JavascriptTestTrait; @@ -50,6 +50,7 @@ protected function getAdministratorPermissions() { */ protected function setUp() { parent::setUp(); + Role::create(['id' => 'test_role', 'label' => $this->randomString()])->save(); $field_storage = FieldStorageConfig::create([ 'field_name' => 'test_conditions', @@ -86,8 +87,8 @@ public function testPluginSelectSelectField() { $this->waitForAjaxToFinish(); $this->submitForm([ - 'test_conditions[0][plugin_select][target_plugin_configuration][roles][test_role]' => 1, - 'test_conditions[0][plugin_select][target_plugin_configuration][negate]' => 0, + 'test_conditions[0][plugin_select][target_plugin_configuration][form][roles][test_role]' => 1, + 'test_conditions[0][plugin_select][target_plugin_configuration][form][negate]' => 0, ], 'Save'); $this->assertSession()->pageTextContains('entity_test 1 has been updated.'); @@ -113,8 +114,8 @@ public function testPluginSelectSelectField() { // Set the condition to be negated. $this->drupalGet($entity->toUrl('edit-form')); $this->submitForm([ - 'test_conditions[0][plugin_select][target_plugin_configuration][roles][test_role]' => 1, - 'test_conditions[0][plugin_select][target_plugin_configuration][negate]' => 1, + 'test_conditions[0][plugin_select][target_plugin_configuration][form][roles][test_role]' => 1, + 'test_conditions[0][plugin_select][target_plugin_configuration][form][negate]' => 1, ], 'Save'); $this->assertSession()->pageTextContains('entity_test 1 has been updated.'); @@ -147,8 +148,8 @@ public function testPluginSelectRadiosField() { $this->waitForAjaxToFinish(); $this->submitForm([ - 'test_conditions[0][plugin_select][target_plugin_configuration][roles][test_role]' => 1, - 'test_conditions[0][plugin_select][target_plugin_configuration][negate]' => 0, + 'test_conditions[0][plugin_select][target_plugin_configuration][form][roles][test_role]' => 1, + 'test_conditions[0][plugin_select][target_plugin_configuration][form][negate]' => 0, ], 'Save'); $this->assertSession()->pageTextContains('entity_test 1 has been updated.'); @@ -174,8 +175,8 @@ public function testPluginSelectRadiosField() { // Set the condition to be negated. $this->drupalGet($entity->toUrl('edit-form')); $this->submitForm([ - 'test_conditions[0][plugin_select][target_plugin_configuration][roles][test_role]' => 1, - 'test_conditions[0][plugin_select][target_plugin_configuration][negate]' => 1, + 'test_conditions[0][plugin_select][target_plugin_configuration][form][roles][test_role]' => 1, + 'test_conditions[0][plugin_select][target_plugin_configuration][form][negate]' => 1, ], 'Save'); $this->assertSession()->pageTextContains('entity_test 1 has been updated.');