function _link_validate in Link 7
Same name and namespace in other branches
- 6.2 link.inc \_link_validate()
- 6 link.module \_link_validate()
Validates that the link field has been entered properly.
1 call to _link_validate()
- link_field_validate in ./
link.module - Implements hook_field_validate().
File
- ./
link.module, line 559 - Defines simple link field types.
Code
function _link_validate(&$item, $delta, $field, $entity, $instance, $langcode, &$optional_field_found, &$errors) {
if ($item['url'] && !(isset($instance['default_value'][$delta]['url']) && $item['url'] === $instance['default_value'][$delta]['url'] && !$instance['required'])) {
// Validate the link.
if (!link_validate_url(trim($item['url']), $langcode)) {
$errors[$field['field_name']][$langcode][$delta][] = array(
'error' => 'link_required',
'message' => t('The value %value provided for %field is not a valid URL.', array(
'%value' => trim($item['url']),
'%field' => $instance['label'],
)),
'error_element' => array(
'url' => TRUE,
'title' => FALSE,
),
);
}
// Require a title for the link if necessary.
if ($instance['settings']['title'] == 'required' && strlen(trim($item['title'])) == 0) {
$errors[$field['field_name']][$langcode][$delta][] = array(
'error' => 'link_required',
'message' => t('Titles are required for all links.'),
'error_element' => array(
'url' => FALSE,
'title' => TRUE,
),
);
}
}
// Require a link if we have a title.
if ($instance['settings']['url'] !== 'optional' && strlen(isset($item['title']) ? $item['title'] : NULL) > 0 && strlen(trim($item['url'])) == 0) {
$errors[$field['field_name']][$langcode][$delta][] = array(
'error' => 'link_required',
'message' => t('You cannot enter a title without a link url.'),
'error_element' => array(
'url' => TRUE,
'title' => FALSE,
),
);
}
// In a totally bizzaro case, where URLs and titles are optional but the field
// is required, ensure there is at least one link.
if ($instance['settings']['url'] === 'optional' && $instance['settings']['title'] === 'optional' && (strlen(trim($item['url'])) !== 0 || strlen(trim($item['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,
),
);
}
// Specific logic for when using the 'select' option on the title field.
if ($instance['settings']['title'] == 'select') {
// The title is required.
if (!empty($item['title']) && !empty($item['url'])) {
$errors[$field['field_name']][$langcode][$delta][] = array(
'error' => 'link_required',
'message' => t('The title field is required when a URL is provided.'),
'error_element' => array(
'url' => FALSE,
'title' => TRUE,
),
);
}
}
}