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..c1efeb6 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. @@ -57,27 +60,27 @@ protected function setUp() { function testURLValidation() { $field_name = Unicode::strtolower($this->randomMachineName()); // Create a field with settings to validate. - $this->fieldStorage = entity_create('field_storage_config', array( + $this->fieldStorage = FieldStorageConfig::create([ 'field_name' => $field_name, 'entity_type' => 'entity_test', 'type' => 'link', - )); + ]); $this->fieldStorage->save(); - $this->field = entity_create('field_config', array( + $this->field = FieldConfig::create([ 'field_storage' => $this->fieldStorage, 'bundle' => 'entity_test', - 'settings' => array( + 'settings' => [ 'title' => DRUPAL_DISABLED, 'link_type' => LinkItemInterface::LINK_GENERIC, - ), - )); + ], + ]); $this->field->save(); - entity_get_form_display('entity_test', 'entity_test', 'default') + EntityFormDisplay::load('entity_test.entity_test.default') ->setComponent($field_name, array( 'type' => 'link_default', - 'settings' => array( + 'settings' => [ 'placeholder_url' => 'http://example.com', - ), + ], )) ->save(); entity_get_display('entity_test', 'entity_test', 'full') @@ -218,29 +221,29 @@ protected function assertInvalidEntries($field_name, array $invalid_entries) { function testLinkTitle() { $field_name = Unicode::strtolower($this->randomMachineName()); // Create a field with settings to validate. - $this->fieldStorage = entity_create('field_storage_config', array( + $this->fieldStorage = FieldStorageConfig::create([ 'field_name' => $field_name, 'entity_type' => 'entity_test', 'type' => 'link', - )); + ]); $this->fieldStorage->save(); - $this->field = entity_create('field_config', array( + $this->field = FieldConfig::create([ 'field_storage' => $this->fieldStorage, 'bundle' => 'entity_test', 'label' => 'Read more about this entity', - 'settings' => array( + 'settings' => [ 'title' => DRUPAL_OPTIONAL, 'link_type' => LinkItemInterface::LINK_GENERIC, - ), - )); + ], + ]); $this->field->save(); - entity_get_form_display('entity_test', 'entity_test', 'default') + EntityFormDisplay::load('entity_test.entity_test.default') ->setComponent($field_name, array( 'type' => 'link_default', - 'settings' => array( + 'settings' => [ 'placeholder_url' => 'http://example.com', 'placeholder_title' => 'Enter the text for this link', - ), + ], )) ->save(); entity_get_display('entity_test', 'entity_test', 'full') @@ -332,23 +335,23 @@ function testLinkTitle() { function testLinkFormatter() { $field_name = Unicode::strtolower($this->randomMachineName()); // Create a field with settings to validate. - $this->fieldStorage = entity_create('field_storage_config', array( + $this->fieldStorage = FieldStorageConfig::create([ 'field_name' => $field_name, 'entity_type' => 'entity_test', 'type' => 'link', 'cardinality' => 2, - )); + ]); $this->fieldStorage->save(); - entity_create('field_config', array( + FieldConfig::create([ 'field_storage' => $this->fieldStorage, 'label' => 'Read more about this entity', 'bundle' => 'entity_test', - 'settings' => array( + 'settings' => [ 'title' => DRUPAL_OPTIONAL, 'link_type' => LinkItemInterface::LINK_GENERIC, - ), - ))->save(); - entity_get_form_display('entity_test', 'entity_test', 'default') + ], + ])->save(); + EntityFormDisplay::load('entity_test.entity_test.default') ->setComponent($field_name, array( 'type' => 'link_default', )) @@ -472,26 +475,26 @@ function testLinkFormatter() { function testLinkSeparateFormatter() { $field_name = Unicode::strtolower($this->randomMachineName()); // Create a field with settings to validate. - $this->fieldStorage = entity_create('field_storage_config', array( + $this->fieldStorage = FieldStorageConfig::create([ 'field_name' => $field_name, 'entity_type' => 'entity_test', 'type' => 'link', 'cardinality' => 2, - )); + ]); $this->fieldStorage->save(); - entity_create('field_config', array( + FieldConfig::create([ 'field_storage' => $this->fieldStorage, 'bundle' => 'entity_test', - 'settings' => array( + 'settings' => [ 'title' => DRUPAL_OPTIONAL, 'link_type' => LinkItemInterface::LINK_GENERIC, - ), - ))->save(); + ], + ])->save(); $display_options = array( 'type' => 'link_separate', 'label' => 'hidden', ); - entity_get_form_display('entity_test', 'entity_test', 'default') + EntityFormDisplay::load('entity_test.entity_test.default') ->setComponent($field_name, array( 'type' => 'link_default', )) @@ -593,4 +596,46 @@ protected function renderTestEntity($id, $view_mode = 'full', $reset = TRUE) { $this->verbose($output); } + /** + * Check link text is required. + */ + function testRequiredLinkText() { + $field_name = Unicode::strtolower($this->randomMachineName()); + // Create a field with settings to validate. + $this->fieldStorage = FieldStorageConfig::create([ + 'field_name' => $field_name, + 'entity_type' => 'entity_test', + 'type' => 'link', + ]); + $this->fieldStorage->save(); + $this->field = FieldConfig::create([ + 'field_storage' => $this->fieldStorage, + 'bundle' => 'entity_test', + 'label' => 'Read more about this entity', + 'required' => TRUE, + 'settings' => [ + 'title' => DRUPAL_REQUIRED, + 'link_type' => LinkItemInterface::LINK_GENERIC, + ], + ]); + $this->field->save(); + EntityFormDisplay::load('entity_test.entity_test.default') + ->setComponent($field_name, array( + 'type' => 'link_default', + 'settings' => [ + 'placeholder_url' => 'http://example.com', + 'placeholder_title' => 'Enter the text for this link', + ], + )) + ->save(); + + // Display creation form. + $this->drupalGet('entity_test/add'); + $this->drupalPostForm(NULL, [], t('Save')); + + $result = $this->xpath('//label[contains(@class, :class) and contains(text(), :text)]', array(':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.', array('@url' => t('URL')))); + $this->assertText(t('@name field is required.', array('@name' => t('Link text')))); + } }