1 link.module _link_validate(&$item, $delta, $field, $entity, $instance, $langcode, &$optional_field_found, &$errors)

Validates that the link field has been entered properly.

File

core/modules/link/link.module, line 414
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 ($instance['settings']['validate_url']) {
      $url_valid = link_validate_url(trim($item['url']));
      if ($url_valid === FALSE) {
        $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),
      );
    }
    // Validate the URL type.
    if ($instance['settings']['type'] != LINK_BOTH) {
      $url_type = link_validate_url(trim($item['url']));
      if (($instance['settings']['type'] == LINK_INTERNAL && $url_type === LINK_EXTERNAL) || ($instance['settings']['type'] == LINK_EXTERNAL && in_array($url_type, array(LINK_INTERNAL, LINK_FRONT), TRUE))) {
        $field_label = array('%field' => $instance['label']);
        $errors[$field['field_name']][$langcode][$delta][] = array(
          'error' => 'link_required',
          'message' => ($instance['settings']['type'] == LINK_INTERNAL) ? t('Only internal URLs are allowed in %field.', $field_label) : t('Only external URLs are allowed in %field.', $field_label),
          'error_element' => array('url' => TRUE, 'title' => FALSE),
        );
      }
    }
  }
  // Require a link if we have a title.
  if ($instance['settings']['url'] !== 'optional' && strlen(isset($item['title']) ? $item['title'] : '') > 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 bizarre 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),
    );
  }

  // Prevent unexpected data from being placed in the attributes column.
  if (isset($item['attributes']) && is_string($item['attributes'])) {
    $errors[$field['field_name']][$langcode][$delta][] = array(
      'error' => 'link_required',
      'message' => t('String values are not acceptable for attributes.'),
      'error_element' => array('url' => TRUE, 'title' => FALSE),
    );
  }
}