You are here

function pathauto_create_alias in Pathauto 5

Same name and namespace in other branches
  1. 5.2 pathauto.inc \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 patternitems in hook_autopath(), the relevant identifier for the specific item to be aliased (e.g., $node->type)

Return value

The alias that was created

9 calls to pathauto_create_alias()
blog_pathauto_bulkupdate in ./pathauto_user.inc
forum_pathauto_bulkupdate in ./pathauto_taxonomy.inc
Generate aliases for all forums and forum containers without aliases
node_pathauto_bulkupdate in ./pathauto_node.inc
pathauto_nodeapi in ./pathauto_node.inc
Implementation of hook_nodeapi().
pathauto_taxonomy in ./pathauto_taxonomy.inc
Implementation of hook_taxonomy().

... See full list

File

./pathauto.module, line 308

Code

function pathauto_create_alias($module, $op, $placeholders, $src, $type = NULL) {
  if ($op != 'bulkupdate' and variable_get('pathauto_verbose', FALSE)) {
    $verbose = TRUE;
  }
  else {
    $verbose = FALSE;
  }

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

  // No pattern? Do nothing (otherwise we may blow away existing aliases...)
  if (!trim($pattern)) {
    return '';
  }

  // Special handling when updating an item which is already aliased
  $pid = NULL;
  if ($op == 'update' or $op == 'bulkupdate') {
    $result = db_query("SELECT pid,dst FROM {url_alias} WHERE src='%s'", $src);
    if ($data = db_fetch_object($result)) {

      // The item is already aliased, check what to do...
      switch (variable_get('pathauto_update_action', 2)) {

        // Do nothing
        case 0:
          return '';

        // Add new alias in addition to old one
        case 1:
          $oldalias = $data->dst;
          break;

        // Replace old alias - remember the pid to update
        case 2:
          $pid = $data->pid;
          $oldalias = $data->dst;
          break;
        default:
          break;
      }
    }
  }

  // Replace the placeholders with the values provided by the module,
  // and lower-case the result
  $alias = str_replace(array_keys($placeholders), $placeholders, $pattern);
  $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 variant
  $separator = variable_get('pathauto_separator', '-');
  if (_pathauto_alias_exists($alias, $src)) {
    for ($i = 0; _pathauto_alias_exists($alias . $separator . $i, $src); $i++) {
    }

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

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

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

    // Handle replace case (get existing pid)
    _pathauto_set_alias("{$src}/{$feedappend}", "{$alias}/feed", NULL, $verbose);
  }

  // Create any relevant index aliases if requested
  if (variable_get('pathauto_indexaliases', FALSE)) {
    pathauto_create_index_alias($alias, $module);
  }
  return $alias;
}