You are here

function link_field_validate in Link 7

Implements hook_field_validate().

File

./link.module, line 329
Defines simple link field types.

Code

function link_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
  $optional_field_found = FALSE;
  if ($instance['settings']['validate_url'] !== 0 || is_null($instance['settings']['validate_url']) || !isset($instance['settings']['validate_url'])) {
    foreach ($items as $delta => $value) {
      _link_validate($items[$delta], $delta, $field, $entity, $instance, $langcode, $optional_field_found, $errors);
    }
  }
  foreach ($items as $delta => $value) {
    if (isset($value['attributes']) && is_string($value['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,
        ),
      );
    }
  }
  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,
      ),
    );
  }

  // 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,
        ),
      );
    }
  }
}