Loading core/lib/Drupal/Core/Field/WidgetBase.php +16 −5 Original line number Diff line number Diff line Loading @@ -176,10 +176,21 @@ protected function formMultipleElements(FieldItemListInterface $items, array &$f // For multiple fields, title and description are handled by the wrapping // table. $element = array( '#title' => $is_multiple ? '' : $title, '#description' => $is_multiple ? '' : $description, ); if ($is_multiple) { $element = [ '#title' => $title . ' ' . $this->t('(value @number)', ['@number' => $delta + 1]), '#title_display' => 'invisible', '#description' => '', ]; } else { $element = [ '#title' => $title, '#title_display' => 'before', '#description' => $description, ]; } $element = $this->formSingleElement($items, $delta, $element, $form, $form_state); if ($element) { Loading @@ -189,7 +200,7 @@ protected function formMultipleElements(FieldItemListInterface $items, array &$f // defined by widget. $element['_weight'] = array( '#type' => 'weight', '#title' => t('Weight for row @number', array('@number' => $delta + 1)), '#title' => $this->t('Weight for row @number', array('@number' => $delta + 1)), '#title_display' => 'invisible', // Note: this 'delta' is the FAPI #type 'weight' element's property. '#delta' => $max, Loading core/modules/field/src/Tests/FormTest.php +26 −0 Original line number Diff line number Diff line Loading @@ -10,6 +10,8 @@ use Drupal\Component\Utility\SafeMarkup; use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\Form\FormState; use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldStorageConfig; /** * Tests field form handling. Loading Loading @@ -316,6 +318,30 @@ function testFieldFormUnlimited() { // Test with several multiple fields in a form } /** * Tests the position of the required label. */ public function testFieldFormUnlimitedRequired() { $field_name = $this->fieldStorageUnlimited['field_name']; $this->field['field_name'] = $field_name; $this->field['required'] = TRUE; FieldStorageConfig::create($this->fieldStorageUnlimited)->save(); FieldConfig::create($this->field)->save(); entity_get_form_display($this->field['entity_type'], $this->field['bundle'], 'default') ->setComponent($field_name) ->save(); // Display creation form -> 1 widget. $this->drupalGet('entity_test/add'); // Check that the Required symbol is present for the multifield label. $this->assertRaw(SafeMarkup::format('<h4 class="label form-required">@label</h4>', array('@label' => $this->field['label'])), 'Required symbol added field label.'); // Check that the label of the field input is visually hidden and contains // the field title and an indication of the delta for a11y. $this->assertRaw(SafeMarkup::format('<label for="edit-field-unlimited-0-value" class="visually-hidden form-required">@label (value 1)</label>', array('@label' => $this->field['label'])), 'Required symbol not added for field input.'); } /** * Tests widget handling of multiple required radios. */ Loading Loading
core/lib/Drupal/Core/Field/WidgetBase.php +16 −5 Original line number Diff line number Diff line Loading @@ -176,10 +176,21 @@ protected function formMultipleElements(FieldItemListInterface $items, array &$f // For multiple fields, title and description are handled by the wrapping // table. $element = array( '#title' => $is_multiple ? '' : $title, '#description' => $is_multiple ? '' : $description, ); if ($is_multiple) { $element = [ '#title' => $title . ' ' . $this->t('(value @number)', ['@number' => $delta + 1]), '#title_display' => 'invisible', '#description' => '', ]; } else { $element = [ '#title' => $title, '#title_display' => 'before', '#description' => $description, ]; } $element = $this->formSingleElement($items, $delta, $element, $form, $form_state); if ($element) { Loading @@ -189,7 +200,7 @@ protected function formMultipleElements(FieldItemListInterface $items, array &$f // defined by widget. $element['_weight'] = array( '#type' => 'weight', '#title' => t('Weight for row @number', array('@number' => $delta + 1)), '#title' => $this->t('Weight for row @number', array('@number' => $delta + 1)), '#title_display' => 'invisible', // Note: this 'delta' is the FAPI #type 'weight' element's property. '#delta' => $max, Loading
core/modules/field/src/Tests/FormTest.php +26 −0 Original line number Diff line number Diff line Loading @@ -10,6 +10,8 @@ use Drupal\Component\Utility\SafeMarkup; use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\Form\FormState; use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldStorageConfig; /** * Tests field form handling. Loading Loading @@ -316,6 +318,30 @@ function testFieldFormUnlimited() { // Test with several multiple fields in a form } /** * Tests the position of the required label. */ public function testFieldFormUnlimitedRequired() { $field_name = $this->fieldStorageUnlimited['field_name']; $this->field['field_name'] = $field_name; $this->field['required'] = TRUE; FieldStorageConfig::create($this->fieldStorageUnlimited)->save(); FieldConfig::create($this->field)->save(); entity_get_form_display($this->field['entity_type'], $this->field['bundle'], 'default') ->setComponent($field_name) ->save(); // Display creation form -> 1 widget. $this->drupalGet('entity_test/add'); // Check that the Required symbol is present for the multifield label. $this->assertRaw(SafeMarkup::format('<h4 class="label form-required">@label</h4>', array('@label' => $this->field['label'])), 'Required symbol added field label.'); // Check that the label of the field input is visually hidden and contains // the field title and an indication of the delta for a11y. $this->assertRaw(SafeMarkup::format('<label for="edit-field-unlimited-0-value" class="visually-hidden form-required">@label (value 1)</label>', array('@label' => $this->field['label'])), 'Required symbol not added for field input.'); } /** * Tests widget handling of multiple required radios. */ Loading