You are here

function _pathauto_validate_pattern_element in Pathauto 6.2

Same name and namespace in other branches
  1. 6 pathauto.admin.inc \_pathauto_validate_pattern_element()

Element validation callback for URL alias patterns.

This function performs the following validations:

  • Checks if the pattern has at least one token.
  • Checks if any tokens with raw companions are being used and recommends use of the raw tokens.
1 string reference to '_pathauto_validate_pattern_element'
pathauto_patterns_form in ./pathauto.admin.inc
Form builder; Configure the URL alias patterns.

File

./pathauto.admin.inc, line 231
Admin page callbacks for the Pathauto module.

Code

function _pathauto_validate_pattern_element(&$element, &$form_state) {

  // Get the current value of the element (since this can be used during both
  // form display and validation).
  $value = isset($element['#value']) ? $element['#value'] : $element['#default_value'];

  // Empty patterns need no further validation.
  if (!drupal_strlen($value)) {
    return $element;
  }

  // Check to see if the required token functions are available.
  if (!function_exists('token_scan') || !function_exists('token_element_validate')) {
    drupal_set_message(t('Please make sure you are using the latest version of the Token module.'), 'error', FALSE);
    return $element;
  }

  // Run token validation.
  $element = token_element_validate($element, $form_state);

  // Find any non-raw tokens that do have a raw companion token and warn.
  if (!form_get_errors($element)) {

    // Skip if there are already errors on this field.
    module_load_include('inc', 'pathauto');
    $not_raw_tokens = array();
    $raw_tokens = _pathauto_get_raw_tokens();
    foreach (token_scan($value) as $token) {
      if (substr($token, -4) === '-raw') {

        // Skip raw tokens.
        continue;
      }
      elseif (in_array($token . '-raw', $raw_tokens)) {
        drupal_set_message(t('You are using the token [%token] which has a raw companion token [%raw_token]. For Pathauto patterns you should use the -raw version of tokens unless you really know what you are doing. See the <a href="@pathauto-help">Pathauto help</a> for more details.', array(
          '%token' => $token,
          '%raw_token' => $token . '-raw',
          '@pathauto-help' => url('admin/help/pathauto'),
        )), 'error', FALSE);
      }
    }
  }
  return $element;
}