You are here

function pathauto_create_alias in Pathauto 6.2

Same name and namespace in other branches
  1. 5.2 pathauto.inc \pathauto_create_alias()
  2. 5 pathauto.module \pathauto_create_alias()
  3. 6 pathauto.inc \pathauto_create_alias()
  4. 7 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. Only one key/value pair is supported, otherwise you may pass the results from pathauto_get_placeholders() here.

$entity_id: (deprecated) The entity ID (node ID, user ID, etc.). This parameter is deprecated and is not actually used.

$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

The alias that was created.

See also

_pathauto_set_alias()

pathauto_get_placeholders()

5 calls to pathauto_create_alias()
pathauto_blog_update_alias in ./pathauto.module
Update the blog URL aliases for an individual user account.
pathauto_form_alter in ./pathauto.module
Implements hook_form_alter().
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 430
Miscellaneous functions for Pathauto.

Code

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

  // 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 '';
  }

  // Support for when $source and $placeholders were swapped.
  if (is_array($source) && is_string($data)) {
    $placeholders = $source;
    $source = $data;
  }
  elseif (is_array($data) && !isset($data['tokens']) && !isset($data['values'])) {
    $placeholders = pathauto_get_placeholders(key($data), current($data), $pattern, array(
      'language' => (object) array(
        'language' => $language,
      ),
    ));
  }
  else {
    $placeholders = $data;
  }

  // Special handling when updating an item which is already aliased.
  $existing_alias = NULL;
  if ($op == 'update' || $op == 'bulkupdate') {
    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 '';
      }
    }
  }

  // Replace the placeholders with the values provided by the module.
  $alias = str_replace($placeholders['tokens'], $placeholders['values'], $pattern);

  // 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]+?)\\]/', '', $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,
  );
  $success = _pathauto_set_alias($path, $existing_alias, $op);
  return $success ? $alias : NULL;
}