Skip to content

Commit 9bc560f

Browse files
mglamanbojanz
authored andcommitted
Issue #2863805 by mglaman: Plugin select widgets show weight field exposed when AJAX, and broken (#693)
1 parent d82e7e9 commit 9bc560f

File tree

4 files changed

+46
-42
lines changed

4 files changed

+46
-42
lines changed

modules/promotion/tests/src/FunctionalJavascript/PromotionTest.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,20 @@ public function testCreatePromotion() {
4444
$name = $this->randomMachineName(8);
4545
$this->getSession()->getPage()->fillField('name[0][value]', $name);
4646

47-
$this->getSession()->getPage()->fillField('offer[0][target_plugin_id]', 'commerce_promotion_product_percentage_off');
47+
$this->getSession()->getPage()->fillField('offer[0][plugin_select][target_plugin_id]', 'commerce_promotion_product_percentage_off');
4848
$this->waitForAjaxToFinish();
49-
$this->getSession()->getPage()->fillField('offer[0][target_plugin_configuration][amount]', '10.0');
49+
$this->getSession()->getPage()->fillField('offer[0][plugin_select][target_plugin_configuration][amount]', '10.0');
5050

5151
// Change, assert any values reset.
52-
$this->getSession()->getPage()->fillField('offer[0][target_plugin_id]', 'commerce_promotion_product_percentage_off');
52+
$this->getSession()->getPage()->fillField('offer[0][plugin_select][target_plugin_id]', 'commerce_promotion_product_percentage_off');
5353
$this->waitForAjaxToFinish();
54-
$this->assertSession()->fieldValueNotEquals('offer[0][target_plugin_configuration][amount]', '10.0');
55-
$this->getSession()->getPage()->fillField('offer[0][target_plugin_configuration][amount]', '10.0');
54+
$this->assertSession()->fieldValueNotEquals('offer[0][plugin_select][target_plugin_configuration][amount]', '10.0');
55+
$this->getSession()->getPage()->fillField('offer[0][plugin_select][target_plugin_configuration][amount]', '10.0');
5656

57-
$this->getSession()->getPage()->fillField('conditions[0][target_plugin_id]', 'commerce_promotion_order_total_price');
57+
$this->getSession()->getPage()->fillField('conditions[0][plugin_select][target_plugin_id]', 'commerce_promotion_order_total_price');
5858
$this->waitForAjaxToFinish();
59-
$this->getSession()->getPage()->fillField('conditions[0][target_plugin_configuration][amount][number]', '50.00');
60-
$this->getSession()->getPage()->checkField('conditions[0][target_plugin_configuration][negate]');
59+
$this->getSession()->getPage()->fillField('conditions[0][plugin_select][target_plugin_configuration][amount][number]', '50.00');
60+
$this->getSession()->getPage()->checkField('conditions[0][plugin_select][target_plugin_configuration][negate]');
6161

6262
$this->submitForm([], t('Save'));
6363
$this->assertSession()->pageTextContains("Saved the $name promotion.");
@@ -85,19 +85,19 @@ public function testCreatePromotionWithEndDate() {
8585
// Check the integrity of the form.
8686
$this->assertSession()->fieldExists('name[0][value]');
8787

88-
$this->getSession()->getPage()->fillField('offer[0][target_plugin_id]', 'commerce_promotion_product_percentage_off');
89-
$this->getSession()->wait(2000, "jQuery('.ajax-progress').length === 0");
88+
$this->getSession()->getPage()->fillField('offer[0][plugin_select][target_plugin_id]', 'commerce_promotion_product_percentage_off');
89+
$this->waitForAjaxToFinish();
9090

9191
$name = $this->randomMachineName(8);
9292
$edit = [
9393
'name[0][value]' => $name,
94-
'offer[0][target_plugin_configuration][amount]' => '10.0',
94+
'offer[0][plugin_select][target_plugin_configuration][amount]' => '10.0',
9595
];
9696

97-
$this->getSession()->getPage()->fillField('conditions[0][target_plugin_id]', 'commerce_promotion_order_total_price');
98-
$this->getSession()->wait(2000, "jQuery('.ajax-progress').length === 0");
97+
$this->getSession()->getPage()->fillField('conditions[0][plugin_select][target_plugin_id]', 'commerce_promotion_order_total_price');
98+
$this->waitForAjaxToFinish();
9999

100-
$edit['conditions[0][target_plugin_configuration][amount][number]'] = '50.00';
100+
$edit['conditions[0][plugin_select][target_plugin_configuration][amount][number]'] = '50.00';
101101

102102
// Set an end date.
103103
$this->getSession()->getPage()->checkField('end_date[0][has_value]');
@@ -136,7 +136,7 @@ public function testEditPromotion() {
136136
$new_promotion_name = $this->randomMachineName(8);
137137
$edit = [
138138
'name[0][value]' => $new_promotion_name,
139-
'offer[0][target_plugin_configuration][amount]' => '20',
139+
'offer[0][plugin_select][target_plugin_configuration][amount]' => '20',
140140
];
141141
$this->submitForm($edit, 'Save');
142142

src/Plugin/Field/FieldWidget/PluginRadiosWidget.php

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Drupal\commerce\Plugin\Field\FieldWidget;
44

55
use Drupal\Core\Field\FieldItemListInterface;
6-
use Drupal\Core\Field\WidgetBase;
76
use Drupal\Core\Form\FormStateInterface;
87

98
/**
@@ -17,25 +16,15 @@
1716
* },
1817
* )
1918
*/
20-
class PluginRadiosWidget extends WidgetBase {
19+
class PluginRadiosWidget extends PluginSelectWidget {
2120

2221
/**
2322
* {@inheritdoc}
2423
*/
2524
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
26-
list($field_type, $plugin_type) = explode(':', $this->fieldDefinition->getType());
27-
return [
28-
'#type' => 'commerce_plugin_select',
29-
'#plugin_element_type' => 'radios',
30-
'#plugin_type' => $plugin_type,
31-
'#categories' => $this->fieldDefinition->getSetting('categories'),
32-
'#default_value' => [
33-
'target_plugin_id' => $items[$delta]->target_plugin_id,
34-
'target_plugin_configuration' => $items[$delta]->target_plugin_configuration ?: [],
35-
],
36-
'#required' => $this->fieldDefinition->isRequired(),
37-
'#title' => $this->fieldDefinition->getLabel(),
38-
];
25+
$element = parent::formElement($items, $delta, $element, $form, $form_state);
26+
$element['plugin_select']['#plugin_element_type'] = 'radios';
27+
return $element;
3928
}
4029

4130
}

src/Plugin/Field/FieldWidget/PluginSelectWidget.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ class PluginSelectWidget extends WidgetBase {
2525
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
2626
list($field_type, $derivative) = explode(':', $this->fieldDefinition->getType());
2727

28-
return [
28+
$element = [
29+
'#type' => 'container',
30+
] + $element;
31+
$element['plugin_select'] = [
2932
'#type' => 'commerce_plugin_select',
3033
'#plugin_type' => $derivative,
3134
'#categories' => $this->fieldDefinition->getSetting('categories'),
@@ -34,8 +37,20 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
3437
'target_plugin_configuration' => $items[$delta]->target_plugin_configuration ?: [],
3538
],
3639
'#required' => $this->fieldDefinition->isRequired(),
37-
'#title' => $this->fieldDefinition->getLabel(),
3840
];
41+
42+
return $element;
43+
}
44+
45+
/**
46+
* {@inheritdoc}
47+
*/
48+
public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
49+
// Remove the `plugin_select` container element key from values.
50+
foreach ($values as $delta => $value) {
51+
$values[$delta] = $value['plugin_select'];
52+
}
53+
return parent::massageFormValues($values, $form, $form_state);
3954
}
4055

4156
}

tests/src/FunctionalJavascript/PluginItemTest.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,12 @@ public function testPluginSelectSelectField() {
8282
'name' => 'Test',
8383
]);
8484
$this->drupalGet($entity->toUrl('edit-form'));
85-
$this->getSession()->getPage()->fillField('test_conditions[0][target_plugin_id]', 'commerce_test_user_role');
85+
$this->getSession()->getPage()->fillField('test_conditions[0][plugin_select][target_plugin_id]', 'commerce_test_user_role');
8686
$this->waitForAjaxToFinish();
8787

8888
$this->submitForm([
89-
'test_conditions[0][target_plugin_configuration][roles][test_role]' => 1,
90-
'test_conditions[0][target_plugin_configuration][negate]' => 0,
89+
'test_conditions[0][plugin_select][target_plugin_configuration][roles][test_role]' => 1,
90+
'test_conditions[0][plugin_select][target_plugin_configuration][negate]' => 0,
9191
], 'Save');
9292
$this->assertSession()->pageTextContains('entity_test 1 has been updated.');
9393

@@ -113,8 +113,8 @@ public function testPluginSelectSelectField() {
113113
// Set the condition to be negated.
114114
$this->drupalGet($entity->toUrl('edit-form'));
115115
$this->submitForm([
116-
'test_conditions[0][target_plugin_configuration][roles][test_role]' => 1,
117-
'test_conditions[0][target_plugin_configuration][negate]' => 1,
116+
'test_conditions[0][plugin_select][target_plugin_configuration][roles][test_role]' => 1,
117+
'test_conditions[0][plugin_select][target_plugin_configuration][negate]' => 1,
118118
], 'Save');
119119
$this->assertSession()->pageTextContains('entity_test 1 has been updated.');
120120

@@ -143,12 +143,12 @@ public function testPluginSelectRadiosField() {
143143
'name' => 'Test',
144144
]);
145145
$this->drupalGet($entity->toUrl('edit-form'));
146-
$this->getSession()->getPage()->fillField('test_conditions[0][target_plugin_id]', 'commerce_test_user_role');
146+
$this->getSession()->getPage()->fillField('test_conditions[0][plugin_select][target_plugin_id]', 'commerce_test_user_role');
147147
$this->waitForAjaxToFinish();
148148

149149
$this->submitForm([
150-
'test_conditions[0][target_plugin_configuration][roles][test_role]' => 1,
151-
'test_conditions[0][target_plugin_configuration][negate]' => 0,
150+
'test_conditions[0][plugin_select][target_plugin_configuration][roles][test_role]' => 1,
151+
'test_conditions[0][plugin_select][target_plugin_configuration][negate]' => 0,
152152
], 'Save');
153153
$this->assertSession()->pageTextContains('entity_test 1 has been updated.');
154154

@@ -174,8 +174,8 @@ public function testPluginSelectRadiosField() {
174174
// Set the condition to be negated.
175175
$this->drupalGet($entity->toUrl('edit-form'));
176176
$this->submitForm([
177-
'test_conditions[0][target_plugin_configuration][roles][test_role]' => 1,
178-
'test_conditions[0][target_plugin_configuration][negate]' => 1,
177+
'test_conditions[0][plugin_select][target_plugin_configuration][roles][test_role]' => 1,
178+
'test_conditions[0][plugin_select][target_plugin_configuration][negate]' => 1,
179179
], 'Save');
180180
$this->assertSession()->pageTextContains('entity_test 1 has been updated.');
181181

0 commit comments

Comments
 (0)