You are here

function _pathauto_check_pattern in Pathauto 5.2

Helper function for pathauto_admin_settings().

See if they are using all -raw tokens available and if all the tokens are valid in the given context.

For more information on the $type parameter.

Parameters

$pattern: A string containing the pattern to be tested.

$type: A flag indicating the class of substitution tokens to use.

Return value

A status flag, telling whether there is an error or not.

See also

token_get_list()

1 call to _pathauto_check_pattern()
pathauto_admin_settings in ./pathauto.module
Form builder; Configure the Pathauto system.

File

./pathauto.module, line 714
Main file for the Pathauto module, which automatically generates aliases for content.

Code

function _pathauto_check_pattern($pattern, $type) {

  // Hold items we've warned about so we only warn once per token
  static $warned;
  $return = FALSE;

  // Build up a set of all tokens in a format that's easy to search
  $all_tokens = array();
  $tokens = token_get_list($type);
  foreach ($tokens as $actual_tokens) {
    foreach (array_keys($actual_tokens) as $token) {
      $all_tokens[$token] = $token;
    }
  }

  // Now, search the pattern and evaluate its contents.
  $matches = array();
  if (preg_match_all('/\\[[a-zA-z_\\-]+?\\]/i', $pattern, $matches)) {

    // Loop over each found token.
    foreach ($matches[0] as $id => $token) {
      $token = str_replace('[', '', str_replace(']', '', $token));

      // Check if the token is even valid in this context.
      if (!array_key_exists($token, $all_tokens)) {
        drupal_set_message(t('You are using the token [%token] which is not valid within the scope of tokens where you are using it.', array(
          '%token' => $token,
        )), 'error');
        $return[] = $token;
      }
      elseif (!preg_match('/\\-raw/i', $token)) {
        $raw_token = $token . '-raw';
        if (array_key_exists($raw_token, $all_tokens)) {
          if (!isset($warned) || !array_key_exists($token, $warned)) {
            drupal_set_message(t('You are using the token [%token] which has a -raw companion available [%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' => $raw_token,
              '@pathauto-help' => url('admin/help/pathauto'),
            )), 'error');
            $warned[$token] = $token;
          }
          $return[] = $token;
        }
      }
    }
  }
  if ($return) {
    $description = t('NOTE: This field contains potentially incorrect patterns. ');
    $description .= format_plural(count($return), 'Problem token: ', 'Problem tokens: ');
    $description .= t('%problems', array(
      '%problems' => implode(', ', $return),
    ));
    $return = $description;
  }
  return $return;
}