You are here

function _pathauto_set_alias in Pathauto 6

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

TRUE if the path was saved, or NULL otherwise.

See also

path_set_alias()

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

File

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

Code

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

  // Alert users that an existing callback cannot be overridden automatically
  if (_pathauto_path_is_callback($path['alias'])) {
    if ($verbose) {
      _pathauto_verbose(t('Ignoring alias %alias due to existing path conflict.', array(
        '%alias' => $path['alias'],
      )));
    }
    return;
  }

  // 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;
  }
  $path += array(
    'pid' => NULL,
    'language' => '',
  );

  // Skip replacing the current alias with an identical alias
  if (empty($existing_alias) || $existing_alias['alias'] != $path['alias']) {

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

          // Do not create the alias.
          return;
        case 1:

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

          // Create a redirect
          if (module_exists('path_redirect') && function_exists('path_redirect_save')) {
            $redirect = array(
              'source' => $existing_alias['alias'],
              'language' => $existing_alias['language'],
              'redirect' => $path['source'],
            );
            path_redirect_save($redirect);
          }

        // Intentionally fall through to the next condition since we still
        // want to replace the existing alias.
        case 2:

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

    // Save the path array.
    path_set_alias($path['source'], $path['alias'], $path['pid'], $path['language']);
    if ($verbose) {
      if (!empty($redirect)) {
        _pathauto_verbose(t('Created new alias %alias for %source, replacing %old_alias. %old_alias now redirects to %alias.', array(
          '%alias' => $path['alias'],
          '%source' => $path['source'],
          '%old_alias' => $existing_alias['alias'],
        )));
      }
      elseif (!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 TRUE;
  }
}