diff --git a/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php b/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php index 9c97caa..f838540 100644 --- a/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php +++ b/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php @@ -226,6 +226,8 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen // settings cannot be saved otherwise. if (!$this->isDefaultValueWidget($form_state) && $this->getFieldSetting('title') == DRUPAL_REQUIRED) { $element['#element_validate'][] = array(get_called_class(), 'validateTitleElement'); + // Mark field as required, if parent is required. + $element['title']['#required'] = $element['#required']; } // Exposing the attributes array in the widget is left for alternate and more diff --git a/core/modules/link/src/Tests/LinkFieldTest.php b/core/modules/link/src/Tests/LinkFieldTest.php index eada494..a7e62b1 100644 --- a/core/modules/link/src/Tests/LinkFieldTest.php +++ b/core/modules/link/src/Tests/LinkFieldTest.php @@ -12,6 +12,9 @@ use Drupal\Core\Url; use Drupal\link\LinkItemInterface; use Drupal\simpletest\WebTestBase; +use Drupal\field\Entity\FieldStorageConfig; +use Drupal\field\Entity\FieldConfig; +use Drupal\Core\Entity\Entity\EntityFormDisplay; /** * Tests link field widgets and formatters. @@ -593,4 +596,44 @@ protected function renderTestEntity($id, $view_mode = 'full', $reset = TRUE) { $this->verbose($output); } + /** + * Tests if the link text is required. + */ + function testRequiredLinkText() { + $field_name = Unicode::strtolower($this->randomMachineName()); + // Create a field with settings to validate. + FieldStorageConfig::create([ + 'field_name' => $field_name, + 'entity_type' => 'entity_test', + 'type' => 'link', + ])->save(); + FieldConfig::create([ + 'field_name' => $field_name, + 'entity_type' => 'entity_test', + 'bundle' => 'entity_test', + 'label' => 'Read more about this entity', + 'required' => TRUE, + 'settings' => [ + 'title' => DRUPAL_REQUIRED, + 'link_type' => LinkItemInterface::LINK_GENERIC, + ], + ])->save(); + EntityFormDisplay::load('entity_test.entity_test.default') + ->setComponent($field_name, [ + 'type' => 'link_default', + 'settings' => [ + 'placeholder_url' => 'http://example.com', + 'placeholder_title' => 'Enter the text for this link', + ], + ]) + ->save(); + + // Display creation form. + $this->drupalPostForm('entity_test/add', [], t('Save')); + + $result = $this->xpath('//label[contains(@class, :class) and contains(text(), :text)]', [':class' => 'form-required', ':text' => 'Link text']); + $this->assertEqual(count($result), 1, "Link text is marked as required when the link field is set to required."); + $this->assertText(t('@url field is required.', ['@url' => t('URL')])); + $this->assertText(t('@name field is required.', ['@name' => t('Link text')])); + } }