pathauto_create_alias

5 pathauto.inc pathauto_create_alias($module, $op, $placeholders, $src, $entity_id, $type = NULL)
6 pathauto.inc pathauto_create_alias($module, $op, $source, $data, $entity_id = NULL, $type = NULL, $language = '')
7 pathauto.inc pathauto_create_alias($module, $op, $source, $data, $type = NULL, $language = LANGUAGE_NONE)

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()

10 calls to pathauto_create_alias()

File

contributions/pathauto/pathauto.inc, line 415
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);
  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);
  }
  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 = array(
    'module' => $module, 
    'op' => $op, 
    'source' => &$source, 
    'data' => $data, 
    'type' => $type, 
    'language' => &$language, 
    '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;
}