1.20.x path.inc path_save_automatic_alias($source, $alias, $langcode)

Save an automatic alias; replacing or adding aliases based on site settings.

Parameters

string $source: The internal system path.

string $alias: The URL alias to be saved.

string $langcode: The language code for the alias being saved.

Return value

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

See also

path_save()

File

modules/path/path.inc, line 537
Miscellaneous functions for Path module.

Code

function path_save_automatic_alias($source, $alias, $langcode) {
  $verbose = path_verbose_message();
  $existing_path = _path_load_loosely_by_source($source, $langcode);

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

  // Assemble a path array to be saved.
  $path = array(
    'source' => $source,
    'alias' => $alias,
    'langcode' => $langcode,
    'auto' => TRUE,
    'original' => $existing_path,
  );

  // Skip replacing the current alias with an identical alias.
  if (empty($existing_path) || $existing_path['alias'] != $path['alias'] || empty($existing_path['auto'])) {
    // If there is already an alias, respect some update actions.
    if (!empty($existing_path)) {
      switch (config_get('path.settings', 'update_action')) {
        case PATH_UPDATE_ACTION_NO_NEW:
          // Do not create the alias.
          return FALSE;
        case PATH_UPDATE_ACTION_LEAVE:
          // Create a new alias instead of overwriting the existing by leaving
          // $path['pid'] empty.
          break;
        case PATH_UPDATE_ACTION_DELETE:
          // The delete actions should overwrite the existing alias.
          $path['pid'] = $existing_path['pid'];
          break;
      }
    }

    // Save the path array.
    path_save($path);

    if ($verbose) {
      if (!empty($existing_path['pid'])) {
        path_verbose_message(t('Created new alias %alias for %source, replacing %old_alias.', array('%alias' => $path['alias'], '%source' => $path['source'], '%old_alias' => $existing_path['alias'])));
      }
      else {
        path_verbose_message(t('Created new alias %alias for %source.', array('%alias' => $path['alias'], '%source' => $path['source'])));
      }
    }

    return $path;
  }

  return FALSE;
}