You are here

function _pathauto_set_alias in Pathauto 7

Same name and namespace in other branches
  1. 5.2 pathauto.inc \_pathauto_set_alias()
  2. 5 pathauto.module \_pathauto_set_alias()
  3. 6.2 pathauto.inc \_pathauto_set_alias()
  4. 6 pathauto.inc \_pathauto_set_alias()

Private function for Pathauto to create an alias.

Parameters

$path: An associative array containing the following keys:

  • source: The internal system path.
  • alias: The URL alias.
  • pid: (optional) Unique path alias identifier.
  • language: (optional) The language of the alias.

$existing_alias: (optional) An associative array of the existing path alias.

$op: An optional string with the operation being performed.

Return value

The saved path from path_save() or FALSE if the path was not saved.

See also

path_save()

1 call to _pathauto_set_alias()
pathauto_create_alias in ./pathauto.inc
Apply patterns to create an alias.

File

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

Code

function _pathauto_set_alias(array $path, $existing_alias = NULL, $op = NULL) {
  $verbose = _pathauto_verbose(NULL, $op);

  // Alert users if they are trying to create an alias that is the same as the internal path
  if ($path['source'] == $path['alias']) {
    if ($verbose) {
      _pathauto_verbose(t('Ignoring alias %alias because it is the same as the internal path.', array(
        '%alias' => $path['alias'],
      )));
    }
    return FALSE;
  }

  // Skip replacing the current alias with an identical alias
  if (empty($existing_alias) || $existing_alias['alias'] != $path['alias']) {
    $path += array(
      'pathauto' => TRUE,
      'original' => $existing_alias,
    );

    // If there is already an alias, respect some update actions.
    if (!empty($existing_alias)) {
      switch (variable_get('pathauto_update_action', PATHAUTO_UPDATE_ACTION_DELETE)) {
        case PATHAUTO_UPDATE_ACTION_NO_NEW:

          // Do not create the alias.
          return FALSE;
        case PATHAUTO_UPDATE_ACTION_LEAVE:

          // Create a new alias instead of overwriting the existing by leaving
          // $path['pid'] empty.
          break;
        case PATHAUTO_UPDATE_ACTION_DELETE:

          // The delete actions should overwrite the existing alias.
          $path['pid'] = $existing_alias['pid'];
          break;
      }
    }

    // Save the path array.
    path_save($path);
    if ($verbose) {
      if (!empty($existing_alias['pid'])) {
        _pathauto_verbose(t('Created new alias %alias for %source, replacing %old_alias.', array(
          '%alias' => $path['alias'],
          '%source' => $path['source'],
          '%old_alias' => $existing_alias['alias'],
        )));
      }
      else {
        _pathauto_verbose(t('Created new alias %alias for %source.', array(
          '%alias' => $path['alias'],
          '%source' => $path['source'],
        )));
      }
    }
    return $path;
  }
}