You are here

function pathauto_create_alias in Pathauto 7

Same name and namespace in other branches
  1. 5.2 pathauto.inc \pathauto_create_alias()
  2. 5 pathauto.module \pathauto_create_alias()
  3. 6.2 pathauto.inc \pathauto_create_alias()
  4. 6 pathauto.inc \pathauto_create_alias()

Apply patterns to create an alias.

Parameters

$module: The name of your module (e.g., 'node').

$op: Operation being performed on the content being aliased ('insert', 'update', 'return', or 'bulkupdate').

$source: An internal Drupal path to be aliased.

$data: An array of keyed objects to pass to token_replace(). For simple replacement scenarios 'node', 'user', and others are common keys, with an accompanying node or user object being the value. Some token types, like 'site', do not require any explicit information from $data and can be replaced even if it is empty.

$type: For modules which provided pattern items in hook_pathauto(), the relevant identifier for the specific item to be aliased (e.g., $node->type).

$language: A string specify the path's language.

Return value

array|null|false The alias array that was created, NULL if an empty alias was generated, or FALSE if the alias generation was not possible.

See also

_pathauto_set_alias()

token_replace()

5 calls to pathauto_create_alias()
pathauto_blog_update_alias in ./pathauto.module
Update the blog URL aliases for an individual user account.
pathauto_field_attach_form in ./pathauto.module
Implements hook_field_attach_form().
pathauto_node_update_alias in ./pathauto.module
Update the URL aliases for an individual node.
pathauto_taxonomy_term_update_alias in ./pathauto.module
Update the URL aliases for an individual taxonomy term.
pathauto_user_update_alias in ./pathauto.module
Update the URL aliases for an individual user account.

File

./pathauto.inc, line 357
Miscellaneous functions for Pathauto.

Code

function pathauto_create_alias($module, $op, $source, $data, $type = NULL, $language = LANGUAGE_NONE) {

  // Retrieve and apply the pattern for this content type.
  $pattern = pathauto_pattern_load_by_entity($module, $type, $language);

  // Allow other modules to alter the pattern.
  $context = array(
    'module' => $module,
    'op' => $op,
    'source' => $source,
    'data' => $data,
    'type' => $type,
    'language' => &$language,
  );
  drupal_alter('pathauto_pattern', $pattern, $context);
  if (empty($pattern)) {

    // No pattern? Do nothing (otherwise we may blow away existing aliases...)
    return FALSE;
  }

  // Special handling when updating an item which is already aliased.
  $existing_alias = NULL;
  if ($op != 'insert') {
    if ($existing_alias = _pathauto_existing_alias_data($source, $language)) {
      switch (variable_get('pathauto_update_action', PATHAUTO_UPDATE_ACTION_DELETE)) {
        case PATHAUTO_UPDATE_ACTION_NO_NEW:

          // If an alias already exists, and the update action is set to do nothing,
          // then gosh-darn it, do nothing.
          return FALSE;
      }
    }
  }

  // Replace any tokens in the pattern. Uses callback option to clean replacements. No sanitization.
  $alias = token_replace($pattern, $data, array(
    'sanitize' => FALSE,
    'clear' => TRUE,
    'callback' => 'pathauto_clean_token_values',
    'language' => (object) array(
      'language' => $language,
    ),
    'pathauto' => TRUE,
  ));

  // Check if the token replacement has not actually replaced any values. If
  // that is the case, then stop because we should not generate an alias.
  // @see token_scan()
  $pattern_tokens_removed = preg_replace('/\\[[^\\s\\]:]*:[^\\s\\]]*\\]/', '', $pattern);
  if ($alias === $pattern_tokens_removed) {
    return;
  }
  $alias = pathauto_clean_alias($alias);

  // Allow other modules to alter the alias.
  $context['source'] =& $source;
  $context['pattern'] = $pattern;
  drupal_alter('pathauto_alias', $alias, $context);

  // If we have arrived at an empty string, discontinue.
  if (!drupal_strlen($alias)) {
    return;
  }

  // If the alias already exists, generate a new, hopefully unique, variant.
  $original_alias = $alias;
  pathauto_alias_uniquify($alias, $source, $language);
  if ($original_alias != $alias) {

    // Alert the user why this happened.
    _pathauto_verbose(t('The automatically generated alias %original_alias conflicted with an existing alias. Alias changed to %alias.', array(
      '%original_alias' => $original_alias,
      '%alias' => $alias,
    )), $op);
  }

  // Return the generated alias if requested.
  if ($op == 'return') {
    return $alias;
  }

  // Build the new path alias array and send it off to be created.
  $path = array(
    'source' => $source,
    'alias' => $alias,
    'language' => $language,
  );
  $path = _pathauto_set_alias($path, $existing_alias, $op);
  return $path;
}