You are here

function pathauto_create_alias in Pathauto 5.2

Same name and namespace in other branches
  1. 5 pathauto.module \pathauto_create_alias()
  2. 6.2 pathauto.inc \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', or 'bulkupdate').

$placeholders: An array whose keys consist of the translated placeholders which appear in patterns (e.g., t('[title]')) and values are the actual values to be substituted into the pattern (e.g., $node->title).

$src: The "real" URI of the content to be aliased (e.g., "node/$node->nid").

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

Return value

The alias that was created.

8 calls to pathauto_create_alias()
blog_pathauto_bulkupdate in ./pathauto_user.inc
Bulk generate aliases for all blogs without aliases.
node_pathauto_bulkupdate in ./pathauto_node.inc
Generate aliases for all nodes without aliases.
pathauto_nodeapi in ./pathauto.module
Implementation of hook_nodeapi().
pathauto_node_operations_update in ./pathauto.module
Callback function for updating node aliases.
pathauto_user in ./pathauto.module
Implementation of hook_user() for users, trackers, and blogs.

... See full list

File

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

Code

function pathauto_create_alias($module, $op, $placeholders, $src, $entity_id, $type = NULL) {
  if ($op != 'bulkupdate' and variable_get('pathauto_verbose', FALSE) && user_access('notify of path changes')) {
    $verbose = TRUE;
  }
  else {
    $verbose = FALSE;
  }

  // Retrieve and apply the pattern for this content type
  if (!empty($type)) {
    $pattern = trim(variable_get('pathauto_' . $module . '_' . $type . '_pattern', ''));
  }
  if (empty($pattern)) {
    $pattern = trim(variable_get('pathauto_' . $module . '_pattern', ''));
  }

  // No pattern? Do nothing (otherwise we may blow away existing aliases...)
  if (empty($pattern)) {
    return '';
  }
  if ($module == 'taxonomy') {

    // Get proper path for term.
    $term_path = taxonomy_term_path(taxonomy_get_term($entity_id));
    if ($term_path != $src) {

      // Quietly alias 'taxonomy/term/[tid]' with proper path for term.
      $update_data = _pathauto_existing_alias_data($src);
      _pathauto_set_alias($src, $term_path, $module, $entity_id, $update_data['pid'], FALSE, $update_data['old_alias']);

      // Set $src as proper path.
      $src = $term_path;
    }
  }

  // Special handling when updating an item which is already aliased.
  $pid = NULL;
  $old_alias = NULL;
  if ($op == 'update' or $op == 'bulkupdate') {
    if (variable_get('pathauto_update_action', 2) == 0) {

      // Do nothing
      return '';
    }
    $update_data = _pathauto_existing_alias_data($src);
    $pid = $update_data['pid'];
    $old_alias = $update_data['old_alias'];
  }

  // Replace the placeholders with the values provided by the module,
  // and optionally lower-case the result
  $alias = str_replace($placeholders['tokens'], $placeholders['values'], $pattern);
  if (variable_get('pathauto_case', 1)) {
    $alias = drupal_strtolower($alias);
  }

  // Two or more slashes should be collapsed into one
  $alias = preg_replace('/\\/+/', '/', $alias);

  // Trim any leading or trailing slashes
  $alias = preg_replace('/^\\/|\\/+$/', '', $alias);
  $maxlength = min(variable_get('pathauto_max_length', 100), 128);
  $alias = drupal_substr($alias, 0, $maxlength);

  // If the alias already exists, generate a new, hopefully unique, variant
  $separator = variable_get('pathauto_separator', '-');
  if (_pathauto_alias_exists($alias, $src)) {
    $original_alias = $alias;
    for ($i = 0; _pathauto_alias_exists(drupal_substr($alias, 0, $maxlength - strlen($i)) . $separator . $i, $src); $i++) {
    }

    // Make room for the sequence number
    $alias = drupal_substr($alias, 0, $maxlength - drupal_strlen($i));
    $alias = $alias . $separator . $i;

    // If verbose is on, alert the user why this happened
    if ($verbose) {
      drupal_set_message(t('The automatically generated alias %original_alias conflicted with an existing alias. Alias changed to %alias.', array(
        '%original_alias' => $original_alias,
        '%alias' => $alias,
      )));
    }
  }

  // If $pid is NULL, a new alias is created - otherwise, the existing
  // alias for the designated src is replaced
  _pathauto_set_alias($src, $alias, $module, $entity_id, $pid, $verbose, $old_alias);

  // Also create a related feed alias if requested, and if supported
  // by the module
  if (drupal_strlen(variable_get('pathauto_' . $module . '_applytofeeds', ''))) {
    $feedappend = variable_get('pathauto_' . $module . '_applytofeeds', '');

    // For forums and taxonomies, the src doesn't always form the base of the rss feed (ie. image galleries)
    if ($module == 'taxonomy' || $module == 'forum') {
      $update_data = _pathauto_existing_alias_data("taxonomy/term/{$entity_id}/{$feedappend}");
      _pathauto_set_alias("taxonomy/term/{$entity_id}/{$feedappend}", "{$alias}/feed", $module, $entity_id, $update_data['pid'], $verbose, $update_data['old_alias']);
    }
    else {
      $update_data = _pathauto_existing_alias_data("{$src}/{$feedappend}");
      _pathauto_set_alias("{$src}/{$feedappend}", "{$alias}/feed", $module, $entity_id, $update_data['pid'], $verbose, $update_data['old_alias']);
    }
  }
  return $alias;
}