You are here

function _twitterfield_field_widget_validate in TwitterField 7

An #element_validate callback for the twitterfield field widget.

1 string reference to '_twitterfield_field_widget_validate'
twitterfield_field_widget_form in ./twitterfield.module
Implements hook_field_widget_info().

File

./twitterfield.module, line 101
The Twitter Field Widget provides a custom field formatter to display Twitter Id's as Twitter Profile Widgets.

Code

function _twitterfield_field_widget_validate($element, &$form_state) {
  if (empty($element['#value'])) {
    return;
  }
  $instance = field_widget_instance($element, $form_state);
  $allowed_types = $instance['widget']['settings']['allowed_types'];
  $allowed_types_count = count(array_filter($allowed_types));
  $error = '';
  if ($allowed_types_count == 0) {

    // We must be on the settings page, checking the default value,
    // and the allowed types failed validation.
    return;
  }
  elseif ($allowed_types_count == 1) {
    if ($allowed_types['username'] && !twitterfield_is_username($element['#value'])) {
      $error = t('Only Usernames are allowed, and must be prepended with "@".');
    }
    elseif ($allowed_types['list'] && !twitterfield_is_list($element['#value'])) {
      $error = t('Only Lists are allowed, and must be in the format "@username/list".');
    }
    elseif ($allowed_types['hashtag'] && !twitterfield_is_hashtag($element['#value'])) {
      $error = t('Only Hashtags are allowed, and must be prepended with "#".');
    }
    elseif ($allowed_types['search'] && !twitterfield_is_search($element['#value'])) {
      $error = t('Only search strings are allowed.');
    }
  }
  else {
    if (!$allowed_types['username'] && twitterfield_is_username($element['#value'])) {
      $error = t('Usernames are not allowed.');
    }
    elseif (!$allowed_types['list'] && twitterfield_is_list($element['#value'])) {
      $error = t('Lists are not allowed.');
    }
    elseif (!$allowed_types['hashtag'] && twitterfield_is_hashtag($element['#value'])) {
      $error = t('Hashtags are not allowed.');
    }
    elseif (!$allowed_types['search'] && twitterfield_is_search($element['#value'])) {
      $error = t('Search strings are not allowed.');
    }
  }
  if ($error) {
    form_error($element, $error);
  }
}