You are here

function _pathauto_validate_pattern_element in Pathauto 6

Same name and namespace in other branches
  1. 6.2 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_admin_settings in ./pathauto.admin.inc
Form builder; Configure the Pathauto system.

File

./pathauto.admin.inc, line 307
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_token_context')) {
    drupal_set_message(t('Please make sure you are using the latest version of the Token module.'), 'warning', FALSE);
    return $element;
  }

  // Check for at least one token.
  $tokens = token_scan($value);
  if (empty($tokens)) {
    form_error($element, t('The %name should contain at least one token to ensure unique URL aliases are created.', array(
      '%name' => $element['#title'],
    )));
  }
  else {
    token_element_validate_token_context($element, $form_state);
  }

  // Find any non-raw tokens that do have a raw companion token and warn.
  module_load_include('inc', 'pathauto');
  $not_raw_tokens = array();
  $raw_tokens = _pathauto_get_raw_tokens();
  foreach ($tokens 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;
}