From 04db444cd2a4d0043f511ed7f7dbc4557b1f4aa5 Mon Sep 17 00:00:00 2001 From: heykarthikwithu Date: Wed, 9 Dec 2015 12:11:39 -0200 Subject: [PATCH] Issue #2613924 by heykarthikwithu, attiks, aerozeppelin, claudiu.cristea, Mac_Weber: Link text isn't marked as required --- .../src/Plugin/Field/FieldWidget/LinkWidget.php | 2 + core/modules/link/src/Tests/LinkFieldTest.php | 82 ++++++++++++++++++++++ 2 files changed, 84 insertions(+) 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..c369716 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,83 @@ protected function renderTestEntity($id, $view_mode = 'full', $reset = TRUE) { $this->verbose($output); } + /** + * Tests if the URL and the link text are both required. + */ + function testRequiredURLAndLinkText() { + $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->drupalGet('entity_test/add'); + + $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."); + } + + /** + * Tests if the URL is not required, but link text is required. + */ + function testRequiredLinkTextOnly() { + $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' => FALSE, + '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. + $edit = ["{$field_name}[0][uri]" => 'http://www.example.com']; + $this->drupalPostForm('entity_test/add', $edit, 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."); + } + } -- 2.1.4