diff --git a/core/modules/field/src/Tests/FormTest.php b/core/modules/field/src/Tests/FormTest.php index 786e367d41..d90ed811d6 100644 --- a/core/modules/field/src/Tests/FormTest.php +++ b/core/modules/field/src/Tests/FormTest.php @@ -634,4 +634,35 @@ public function testLabelOnMultiValueFields() { $this->assertEscaped(""); } + /** + * Tests alter hooks on widgets that handle multiple values. + * + * @todo Do we want to add these tests to testFieldFormMultipleWidget() + * instead of creating a separate test here? Or do we want to test the alter + * hooks on single-value widgets and multivalue widgets together and move some + * code from testFieldFormSingle() here? + */ + public function testFieldFormMultipleWidgetAlter() { + // Create a field with fixed cardinality, configure the form to use a + // "multiple" widget. + $field_storage = $this->fieldStorageMultiple; + $field_name = $field_storage['field_name']; + $this->field['field_name'] = $field_name; + FieldStorageConfig::create($field_storage)->save(); + FieldConfig::create($this->field)->save(); + entity_get_form_display($this->field['entity_type'], $this->field['bundle'], 'default') + ->setComponent($field_name, [ + 'type' => 'test_field_widget_multiple', + ]) + ->save(); + + // Display creation form. The text added to the child elements should appear + // exactly 4 times, but we just test that it appears more than once. + $this->drupalGet('entity_test/add'); + $this->assertUniqueText(' From hook_field_widget_multiple_form_alter(): description on parent element.', 'Parent form element has text from hook_field_widget_multiple_form_alter()'); + $this->assertNoUniqueText(' From hook_field_widget_multiple_form_alter(): description on child element.', 'Child form elements have text from hook_field_widget_multiple_form_alter()'); + $this->assertUniqueText(' From hook_field_widget_multiple_WIDGET_TYPE_form_alter(): description on parent element.', 'Parent form element has text from hook_field_widget_multiple_WIDGET_TYPE_form_alter()'); + $this->assertNoUniqueText(' From hook_field_widget_multiple_WIDGET_TYPE_form_alter(): description on child element.', 'Child form elements have text from hook_field_widget_multiple_WIDGET_TYPE_form_alter()'); + } + } diff --git a/core/modules/field/tests/modules/field_test/field_test.module b/core/modules/field/tests/modules/field_test/field_test.module index 15f8708adf..6616fef047 100644 --- a/core/modules/field/tests/modules/field_test/field_test.module +++ b/core/modules/field/tests/modules/field_test/field_test.module @@ -108,6 +108,45 @@ function field_test_field_widget_form_alter(&$element, FormStateInterface $form_ } } +/** + * Implements hook_field_widget_multiple_form_alter(). + */ +function field_test_field_widget_multiple_form_alter(array &$elements, FormStateInterface $form_state, array $context) { + // Add a css class to widget form elements for all fields of type mytype. + $field_definition = $context['items']->getFieldDefinition(); + debug($field_definition->getType()); + if ($field_definition->getType() == 'mytype') { + // Be sure not to overwrite existing attributes. + $elements['#attributes']['class'][] = 'myclass'; + } + if (empty($elements['#description'])) { + $elements['#description'] = ''; + } + $elements['#description'] .= ' From hook_field_widget_multiple_form_alter(): description on parent element.'; + foreach ($Element::children($elements) as $delta => $element) { + if (empty($element['#description'])) { + $elements[$delta]['#description'] = ''; + } + $elements[$delta]['#description'] .= ' From hook_field_widget_multiple_form_alter(): description on child element.'; + } +} + +/** + * Implements hook_field_widget_multiple_WIDGET_TYPE_form_alter(). + */ +function field_test_field_widget_multiple_test_field_widget_multiple_form_alter(array &$elements, FormStateInterface $form_state, array $context) { + if (empty($elements['#description'])) { + $elements['#description'] = ''; + } + $elements['#description'] .= ' From hook_field_widget_multiple_WIDGET_TYPE_form_alter(): description on parent element.'; + foreach ($Element::children($elements) as $delta => $element) { + if (empty($element['#description'])) { + $elements[$delta]['#description'] = ''; + } + $elements[$delta]['#description'] .= ' From hook_field_widget_multiple_WIDGET_TYPE_form_alter(): description on child element.'; + } +} + /** * Implements hook_query_TAG_alter() for tag 'efq_table_prefixing_test'. *