diff --git a/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php b/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php index f5a5353b47f380d69257638e14fa1a748715d8ee..c402f766b1b4a9bebb964fb232f5962b77b32fff 100644 --- a/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php +++ b/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php @@ -133,6 +133,8 @@ protected static function getUserEnteredStringAsUri($string) { } /** + * Form element validation handler for the 'uri' element. + * * Disallows saving inaccessible or untrusted URLs. */ public static function validateUriElement($element, FormStateInterface $form_state, $form) { @@ -149,6 +151,18 @@ public static function validateUriElement($element, FormStateInterface $form_sta } } + /** + * Form element validation handler for the 'title' element. + * + * Conditionally requires the link title if a URL value was filled in. + */ + public static function validateTitleElement(&$element, FormStateInterface $form_state, $form) { + if ($element['uri']['#value'] !== '' && $element['title']['#value'] === '') { + $element['title']['#required'] = TRUE; + $form_state->setError($element['title'], t('!name field is required.', array('!name' => $element['title']['#title']))); + } + } + /** * {@inheritdoc} */ @@ -207,7 +221,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen // non-empty. Omit the validation on the field edit form, since the field // settings cannot be saved otherwise. if (!$this->isDefaultValueWidget($form_state) && $this->getFieldSetting('title') == DRUPAL_REQUIRED) { - $element['#element_validate'][] = array($this, 'validateTitle'); + $element['#element_validate'][] = array(get_called_class(), 'validateTitleElement'); } // Exposing the attributes array in the widget is left for alternate and more @@ -311,18 +325,6 @@ public function settingsSummary() { return $summary; } - /** - * Form element validation handler; Validates the title property. - * - * Conditionally requires the link title if a URL value was filled in. - */ - public function validateTitle(&$element, FormStateInterface $form_state, $form) { - if ($element['uri']['#value'] !== '' && $element['title']['#value'] === '') { - $element['title']['#required'] = TRUE; - $form_state->setError($element['title'], $this->t('!name field is required.', array('!name' => $element['title']['#title']))); - } - } - /** * {@inheritdoc} */