You are here

function token_element_validate in Token 7

Same name and namespace in other branches
  1. 8 token.module \token_element_validate()
  2. 6 token.module \token_element_validate()

Validate a form element that should have tokens in it.

Form elements that want to add this validation should have the #token_types parameter defined.

For example:

$form['my_node_text_element'] = array(
  '#type' => 'textfield',
  '#title' => t('Some text to token-ize that has a node context.'),
  '#default_value' => 'The title of this node is [node:title].',
  '#element_validate' => array(
    'token_element_validate',
  ),
  '#token_types' => array(
    'node',
  ),
  '#min_tokens' => 1,
  '#max_tokens' => 10,
);
1 call to token_element_validate()
token_element_validate_token_context in ./token.module
Deprecated. Use token_element_validate() instead.
3 string references to 'token_element_validate'
token_form_block_admin_configure_alter in ./token.module
Implements hook_form_FORM_ID_alter().
token_form_field_ui_field_edit_form_alter in ./token.module
Implements hook_form_FORM_ID_alter().
token_form_user_admin_settings_alter in ./token.module
Implements hook_form_FORM_ID_alter().

File

./token.module, line 713
Enhances the token API in core: adds a browseable UI, missing tokens, etc.

Code

function token_element_validate(&$element, &$form_state) {
  $value = isset($element['#value']) ? $element['#value'] : $element['#default_value'];
  if (!drupal_strlen($value)) {

    // Empty value needs no further validation since the element should depend
    // on using the '#required' FAPI property.
    return $element;
  }
  $tokens = token_scan($value);
  $title = empty($element['#title']) ? $element['#parents'][0] : $element['#title'];

  // @todo Find old Drupal 6 style tokens and add them to invalid tokens.
  // Validate if an element must have a minimum number of tokens.
  if (isset($element['#min_tokens']) && count($tokens) < $element['#min_tokens']) {
    $error = format_plural($element['#min_tokens'], '%name must contain at least one token.', '%name must contain at least @count tokens.', array(
      '%name' => $title,
    ));
    form_error($element, $error);
  }

  // Validate if an element must have a maximum number of tokens.
  if (isset($element['#max_tokens']) && count($tokens) > $element['#max_tokens']) {
    $error = format_plural($element['#max_tokens'], '%name must contain at most one token.', '%name must contain at most @count tokens.', array(
      '%name' => $title,
    ));
    form_error($element, $error);
  }

  // Check if the field defines specific token types.
  if (isset($element['#token_types'])) {
    $invalid_tokens = token_get_invalid_tokens_by_context($tokens, $element['#token_types']);
    if ($invalid_tokens) {
      form_error($element, t('%name is using the following invalid tokens: @invalid-tokens.', array(
        '%name' => $title,
        '@invalid-tokens' => implode(', ', $invalid_tokens),
      )));
    }
  }
  return $element;
}