You are here

function pathauto_create_alias in Pathauto 6

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. 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').

$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).

$source: An internal Drupal path to be aliased.

$entity_id: The entity ID (node ID, user ID, etc.).

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

11 calls to pathauto_create_alias()
blog_pathauto_bulkupdate in ./pathauto_user.inc
Bulk generate aliases for all blogs without aliases.
contact_pathauto_bulkupdate in ./pathauto_user.inc
Bulk generate aliases for all users without aliases
node_pathauto_bulkupdate in ./pathauto_node.inc
Generate aliases for all nodes without aliases.
pathauto_form_alter in ./pathauto.module
Implements hook_form_alter().
pathauto_nodeapi in ./pathauto.module
Implements hook_nodeapi().

... See full list

File

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

Code

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

  // Retrieve and apply the pattern for this content type
  if (!empty($type)) {
    $pattern = trim(variable_get("pathauto_{$module}_{$type}_{$language}_pattern", ''));
    if (empty($pattern)) {
      $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 != $source) {

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

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

  // 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', 2)) {
        case 0:

          // 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,
    'entity_id' => $entity_id,
    '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
  if (_pathauto_alias_exists($alias, $source, $language)) {
    $maxlength = min(variable_get('pathauto_max_length', 100), _pathauto_get_schema_alias_maxlength());
    $separator = variable_get('pathauto_separator', '-');
    $original_alias = $alias;
    $i = 0;
    do {

      // Append an incrementing numeric suffix until we find a unique alias.
      $unique_suffix = $separator . $i;
      $alias = drupal_substr($original_alias, 0, $maxlength - drupal_strlen($unique_suffix, TRUE)) . $unique_suffix;
      $i++;
    } while (_pathauto_alias_exists($alias, $source, $language));

    // 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);

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

    // For forums and taxonomies, the source doesn't always form the base of the RSS feed (i.e. image galleries)
    if ($module == 'taxonomy' || $module == 'forum' && !empty($entity_id)) {
      $source = "taxonomy/term/{$entity_id}";
    }

    // Build the feed path alias array and send it off to be created.
    $path = array(
      'source' => "{$source}/{$feedappend}",
      'alias' => "{$alias}/feed",
      'language' => $language,
    );
    $existing_alias = _pathauto_existing_alias_data($path['source'], $path['language']);
    _pathauto_set_alias($path, $existing_alias, $op);
  }
  return $success ? $alias : NULL;
}