Loading src/Entity/Fee.php +19 −1 Original line number Diff line number Diff line Loading @@ -466,8 +466,9 @@ class Fee extends CommerceContentEntityBase implements FeeInterface { ->setLabel(t('Fee type')) ->setCardinality(1) ->setRequired(TRUE) ->setSetting('allowed_values_function', [static::class, 'getPluginOptions']) ->setDisplayOptions('form', [ 'type' => 'commerce_plugin_radios', 'type' => 'commerce_plugin_select', 'weight' => 3, ]); Loading Loading @@ -550,4 +551,21 @@ class Fee extends CommerceContentEntityBase implements FeeInterface { return gmdate(DateTimeItemInterface::DATETIME_STORAGE_FORMAT, $timestamp); } /** * Gets the allowed values for the 'plugin' base field. * * @return array * The allowed values. */ public static function getPluginOptions() { /** @var \Drupal\commerce_fee\FeeManager $fee_manager */ $fee_manager = \Drupal::getContainer()->get('plugin.manager.commerce_fee'); $plugins = array_map(static function ($definition) { return $definition['label']; }, $fee_manager->getDefinitions()); asort($plugins); return $plugins; } } src/Form/FeeForm.php +13 −0 Original line number Diff line number Diff line Loading @@ -38,6 +38,19 @@ class FeeForm extends ContentEntityForm { $form = parent::form($form, $form_state); $form['#tree'] = TRUE; // By default a fee type is preselected on the add form because the field // is required. Select an empty value instead, to force the user to choose. $user_input = $form_state->getUserInput(); if ($this->operation == 'add' && $this->entity->get('plugin')->isEmpty()) { if (!empty($form['plugin']['widget'][0]['target_plugin_id'])) { $form['plugin']['widget'][0]['target_plugin_id']['#empty_value'] = ''; if (empty($user_input['plugin'][0]['target_plugin_id'])) { $form['plugin']['widget'][0]['target_plugin_id']['#default_value'] = ''; unset($form['plugin']['widget'][0]['target_plugin_configuration']); } } } $translating = !$this->isDefaultFormLangcode($form_state); $hide_non_translatable_fields = $this->entity->isDefaultTranslationAffectedOnly(); Loading tests/src/FunctionalJavascript/FeeTest.php +4 −5 Original line number Diff line number Diff line Loading @@ -110,7 +110,6 @@ class FeeTest extends CommerceWebDriverTestBase { // Set an end date. $end_date = new DrupalDateTime('now', 'UTC'); $end_date = $end_date->modify('+1 month'); $name = $this->randomMachineName(8); $this->getSession()->getPage()->checkField('end_date[0][has_value]'); $this->setRawFieldValue('end_date[0][container][value][date]', $end_date->format('Y-m-d')); $this->setRawFieldValue('end_date[0][container][value][time]', $end_date->format('H:i:s')); Loading @@ -126,22 +125,22 @@ class FeeTest extends CommerceWebDriverTestBase { } /** * Tests updating the fee type when creating a promotion. * Tests updating the fee type when creating a fee. */ public function testCreateFeeTypeSelection() { $this->drupalGet('admin/commerce/fees'); $this->clickLink('Add fee'); $fee_config_xpath = '//div[@data-drupal-selector="edit-fee-0-target-plugin-configuration"]'; $fee_config_xpath = '//div[@data-drupal-selector="edit-plugin-0-target-plugin-configuration"]'; $fee_config_container = $this->xpath($fee_config_xpath); $this->assertEmpty($fee_config_container); $this->getSession()->getPage()->selectFieldOption('fee[0][target_plugin_id]', 'order_item_percentage'); $this->getSession()->getPage()->selectFieldOption('plugin[0][target_plugin_id]', 'order_item_percentage'); $this->assertSession()->assertWaitOnAjaxRequest(); $fee_config_container = $this->xpath($fee_config_xpath); $this->assertNotEmpty($fee_config_container); $this->getSession()->getPage()->selectFieldOption('fee[0][target_plugin_id]', ''); $this->getSession()->getPage()->selectFieldOption('plugin[0][target_plugin_id]', ''); $this->assertSession()->assertWaitOnAjaxRequest(); $fee_config_container = $this->xpath($fee_config_xpath); $this->assertEmpty($fee_config_container); Loading Loading
src/Entity/Fee.php +19 −1 Original line number Diff line number Diff line Loading @@ -466,8 +466,9 @@ class Fee extends CommerceContentEntityBase implements FeeInterface { ->setLabel(t('Fee type')) ->setCardinality(1) ->setRequired(TRUE) ->setSetting('allowed_values_function', [static::class, 'getPluginOptions']) ->setDisplayOptions('form', [ 'type' => 'commerce_plugin_radios', 'type' => 'commerce_plugin_select', 'weight' => 3, ]); Loading Loading @@ -550,4 +551,21 @@ class Fee extends CommerceContentEntityBase implements FeeInterface { return gmdate(DateTimeItemInterface::DATETIME_STORAGE_FORMAT, $timestamp); } /** * Gets the allowed values for the 'plugin' base field. * * @return array * The allowed values. */ public static function getPluginOptions() { /** @var \Drupal\commerce_fee\FeeManager $fee_manager */ $fee_manager = \Drupal::getContainer()->get('plugin.manager.commerce_fee'); $plugins = array_map(static function ($definition) { return $definition['label']; }, $fee_manager->getDefinitions()); asort($plugins); return $plugins; } }
src/Form/FeeForm.php +13 −0 Original line number Diff line number Diff line Loading @@ -38,6 +38,19 @@ class FeeForm extends ContentEntityForm { $form = parent::form($form, $form_state); $form['#tree'] = TRUE; // By default a fee type is preselected on the add form because the field // is required. Select an empty value instead, to force the user to choose. $user_input = $form_state->getUserInput(); if ($this->operation == 'add' && $this->entity->get('plugin')->isEmpty()) { if (!empty($form['plugin']['widget'][0]['target_plugin_id'])) { $form['plugin']['widget'][0]['target_plugin_id']['#empty_value'] = ''; if (empty($user_input['plugin'][0]['target_plugin_id'])) { $form['plugin']['widget'][0]['target_plugin_id']['#default_value'] = ''; unset($form['plugin']['widget'][0]['target_plugin_configuration']); } } } $translating = !$this->isDefaultFormLangcode($form_state); $hide_non_translatable_fields = $this->entity->isDefaultTranslationAffectedOnly(); Loading
tests/src/FunctionalJavascript/FeeTest.php +4 −5 Original line number Diff line number Diff line Loading @@ -110,7 +110,6 @@ class FeeTest extends CommerceWebDriverTestBase { // Set an end date. $end_date = new DrupalDateTime('now', 'UTC'); $end_date = $end_date->modify('+1 month'); $name = $this->randomMachineName(8); $this->getSession()->getPage()->checkField('end_date[0][has_value]'); $this->setRawFieldValue('end_date[0][container][value][date]', $end_date->format('Y-m-d')); $this->setRawFieldValue('end_date[0][container][value][time]', $end_date->format('H:i:s')); Loading @@ -126,22 +125,22 @@ class FeeTest extends CommerceWebDriverTestBase { } /** * Tests updating the fee type when creating a promotion. * Tests updating the fee type when creating a fee. */ public function testCreateFeeTypeSelection() { $this->drupalGet('admin/commerce/fees'); $this->clickLink('Add fee'); $fee_config_xpath = '//div[@data-drupal-selector="edit-fee-0-target-plugin-configuration"]'; $fee_config_xpath = '//div[@data-drupal-selector="edit-plugin-0-target-plugin-configuration"]'; $fee_config_container = $this->xpath($fee_config_xpath); $this->assertEmpty($fee_config_container); $this->getSession()->getPage()->selectFieldOption('fee[0][target_plugin_id]', 'order_item_percentage'); $this->getSession()->getPage()->selectFieldOption('plugin[0][target_plugin_id]', 'order_item_percentage'); $this->assertSession()->assertWaitOnAjaxRequest(); $fee_config_container = $this->xpath($fee_config_xpath); $this->assertNotEmpty($fee_config_container); $this->getSession()->getPage()->selectFieldOption('fee[0][target_plugin_id]', ''); $this->getSession()->getPage()->selectFieldOption('plugin[0][target_plugin_id]', ''); $this->assertSession()->assertWaitOnAjaxRequest(); $fee_config_container = $this->xpath($fee_config_xpath); $this->assertEmpty($fee_config_container); Loading