1 link.module | link_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) |
Implements hook_field_validate().
Most validations are done at the element level, which runs before this.
File
- core/
modules/ link/ link.module, line 285 - Defines simple link field types.
Code
function link_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
// When setting a default value on the field instance settings form, $entity
// will be NULL. Do not validate the default provided values, which allows
// for default titles to be provided but not a URL (or vice-versa). Invalid
// or partial URLs are also acceptable for default values.
if (is_null($entity)) {
return;
}
// In a totally bizarre case, where URLs and titles are optional but the field
// is required, ensure there is at least one link. Populate the
// $optional_field_found variable if any "required" values have been provided.
$optional_field_found = FALSE;
foreach ($items as $delta => $value) {
if ($instance['settings']['url'] === 'optional' && $instance['settings']['title'] === 'optional' && (strlen(trim($value['url'])) !== 0 || strlen(trim($value['title'])) !== 0)) {
$optional_field_found = TRUE;
}
}
// Require entire field.
if ($instance['settings']['url'] === 'optional' && $instance['settings']['title'] === 'optional' && $instance['required'] == 1 && !$optional_field_found && isset($instance['id'])) {
$errors[$field['field_name']][$langcode][$delta][] = array(
'error' => 'link_required',
'message' => t('At least one title or URL must be entered.'),
'error_element' => array('url' => FALSE, 'title' => TRUE),
);
}
// If no values were provided across all values but the field itself is
// required, throw a validation error on the first field.
if ($instance['settings']['url'] === 'optional' && $instance['settings']['title'] === 'optional' && $instance['required'] && !$optional_field_found) {
$errors[$field['field_name']][$langcode][0][] = array(
'error' => 'link_required',
'message' => t('At least one title or URL must be entered.'),
'error_element' => array('url' => FALSE, 'title' => TRUE),
);
}
}