Commit 7a4a8c97 authored by catch's avatar catch

Issue #2923496 by mark_fullmer, dpfitzsi, krina.addweb: [Link module]...

Issue #2923496 by mark_fullmer, dpfitzsi, krina.addweb: [Link module] Validation for title but no link, when title optional
parent 13768e80
......@@ -157,6 +157,17 @@ public static function validateTitleElement(&$element, FormStateInterface $form_
}
}
/**
* Form element validation handler for the 'title' element.
*
* Requires the URL value if a link title was filled in.
*/
public static function validateTitleNoLink(&$element, FormStateInterface $form_state, $form) {
if ($element['uri']['#value'] === '' && $element['title']['#value'] !== '') {
$form_state->setError($element['uri'], t('The @uri field is required when the @title field is specified.', ['@title' => $element['title']['#title'], '@uri' => $element['uri']['#title']]));
}
}
/**
* {@inheritdoc}
*/
......@@ -222,8 +233,12 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
// Post-process the title field to make it conditionally required if URL is
// non-empty. Omit the validation on the field edit form, since the field
// settings cannot be saved otherwise.
//
// Validate that title field is filled out (regardless of uri) when it is a
// required field.
if (!$this->isDefaultValueWidget($form_state) && $this->getFieldSetting('title') === DRUPAL_REQUIRED) {
$element['#element_validate'][] = [get_called_class(), 'validateTitleElement'];
$element['#element_validate'][] = [get_called_class(), 'validateTitleNoLink'];
if (!$element['title']['#required']) {
// Make title required on the front-end when URI filled-in.
......@@ -242,6 +257,12 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
}
}
// Ensure that a URI is always entered when an optional title field is
// submitted.
if (!$this->isDefaultValueWidget($form_state) && $this->getFieldSetting('title') == DRUPAL_OPTIONAL) {
$element['#element_validate'][] = [get_called_class(), 'validateTitleNoLink'];
}
// Exposing the attributes array in the widget is left for alternate and more
// advanced field widgets.
$element['attributes'] = [
......
......@@ -281,6 +281,14 @@ public function testLinkTitle() {
$this->assertRaw('placeholder="Enter the text for this link"');
$this->assertFieldByName("{$field_name}[0][title]", '', 'Link text field found.');
if ($title_setting === DRUPAL_OPTIONAL) {
// Verify that the URL is required, if the link text is non-empty.
$edit = [
"{$field_name}[0][title]" => 'Example',
];
$this->drupalPostForm(NULL, $edit, t('Save'));
$this->assertText(t('The URL field is required when the @title field is specified.', ['@title' => t('Link text')]));
}
if ($title_setting === DRUPAL_REQUIRED) {
// Verify that the link text is required, if the URL is non-empty.
$edit = [
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment