function linked_field_element_validate_destination in Linked Field 8
Form element validation handler for destination field in settings form.
Parameters
array $element: The structured array whose children shall be rendered.
\Drupal\Core\Form\FormStateInterface $form_state: The form state.
1 string reference to 'linked_field_element_validate_destination'
File
- ./
linked_field.module, line 451 - Main file of Linked Field module.
Code
function linked_field_element_validate_destination(array $element, FormStateInterface &$form_state) {
$fields = $form_state
->getValue('fields');
// We check whether 'fields' exists in the form_state values.
if ($fields) {
$field_name = $element['#array_parents'][1];
$settings = $fields[$field_name]['settings_edit_form']['third_party_settings']['linked_field'];
$linked = $settings['linked'];
$type = $settings['type'];
$destination = $settings['destination'][$type];
// If this field should be linked, the destination field is required.
if ($linked) {
if (!$destination) {
$form_state
->setErrorByName($element[$type], t('!name field is required.', [
'!name' => $element[$type]['#title'],
]));
}
else {
$field =& $fields[$field_name]['settings_edit_form']['third_party_settings']['linked_field'];
$field['destination'] = $field['destination'][$type];
if ($type == 'custom') {
$destination = $field['destination'];
// Prevent validating URL with tokens.
if (strpos($destination, '[') === FALSE || strpos($destination, ']') === FALSE) {
try {
Url::fromUri($destination);
} catch (\Exception $e) {
$form_state
->setError($element, t('Destination is invalid.'));
return;
}
}
}
$form_state
->setValue('fields', $fields);
}
}
}
}