Background

URL aliasing gives users the ability to have control over all Drupal paths. This functionality will integrate seamlessly into node forms and also provide the administrator an interface to view all aliases that have been created.

Aliases have a 1 to 1 relationship with their original Drupal URLs. In otherwards you cannot have an alias map to more than one path. Likewise, a Drupal URL can't be mapped to more than one alias.

"; $output .= "

Permissions

Two new permissions are introduced for aliasing URLs: create url aliases and administer url aliases.

"; $output .= "
    "; $output .= "
  1. create url aliases - Allows users to create aliases for nodes. Enabling this permission will display a new path field to the user in any node form, allowing them to enter an alias for that node. They will be able to edit/delete the alias after it is created using the same form.
  2. "; $output .= "
  3. administer url aliases - Allows users to access the alias administration interface. They must also have the access administration pages permission set as well. This interface displays all aliases and provides a way to create and modify them as well. This is also the location to build aliases for things other than nodes. For example, you can create an alias for a taxonomy URL or even re-map the admin path (although the original admin path will still be accessible since aliases do not cancel out original paths).
  4. "; $output .= "
"; $output = t($output); break; } return $output; } function path_link($type, $node = NULL) { if ($type == "system" && user_access("administer url aliases")) { menu("admin/path", t("url aliasing"), "path_admin", 4); menu("admin/path/add", t("new alias"), "path_admin"); menu("admin/path/help", t("help"), "path_admin", 9); } } function path_nodeapi(&$node, $op, $arg) { if (user_access("create url aliases") || user_access("administer url aliases")) { switch ($op) { case "validate": // is_null provides a mechanism for us to determine if this is the first // viewing of the form. If it is the first time, load the alias, if it isn't // (i.e., user has clicked preview) let them work with their current form alias. if (is_null($node->path)) { $node->path = drupal_get_path_alias("node/view/$node->nid"); } else { $node->path = trim($node->path); if ($node->path && !valid_url($node->path)) { $error["path"] = t("The path is invalid."); return $error; } else if (db_result(db_query("SELECT COUNT(dst) FROM {path} WHERE dst = '%s' AND src != '%s'", $node->path, "node/view/$node->nid"))) { $error["path"] = t("The path is already in use."); return $error; } } break; case "form pre": return form_textfield(t("Path alias"), "path", $node->path, 60, 250, t("Optionally specify an alternative URL by which this node can be accessed. For example, type 'about' when writing an about page. Use a relative path and don't add a trailing slash or the URL alias won't work.") . theme_error($arg["path"])); case "insert": /* ** Don't try to insert if path is NULL. We may have already set ** the alias ahead of time. */ if ($node->path) { path_set_alias("node/view/$node->nid", $node->path); } break; case "update": path_set_alias("node/view/$node->nid", $node->path); break; case "delete": if ($alias = drupal_get_path_alias("node/view/$node->nid")) { path_set_alias("node/view/$node->nid"); } break; } } } function path_perm() { return array("create url aliases", "administer url aliases"); } function path_overview() { $sql = "SELECT * FROM {path}"; $header = array( array ("data" => t("alias"), "field" => "dst", "sort" => "asc"), array ("data" => t("normal"), "field" => "src"), array ("data" => t("operations"), "colspan" => 2) ); $sql .= tablesort_sql($header); $result = pager_query($sql, 50); while ($data = db_fetch_object($result)) { $rows[] = array($data->dst, $data->src, l(t("edit"), "admin/path/edit/$data->pid"), l(t("delete"), "admin/path/delete/$data->pid")); } if ($pager = pager_display(NULL, 50, 0, "admin", tablesort_pager())) { $rows[] = array(array("data" => $pager, "colspan" => "4")); } if (!$rows) { $rows[] = array(array("data" => t("No URL aliases available."), "colspan" => "4")); } return table($header, $rows); } function path_load($pid) { return db_fetch_array(db_query("SELECT * FROM {path} WHERE pid = '%d'", $pid)); } function path_delete($pid) { db_query("DELETE FROM path WHERE pid = '%d'", $pid); return t("the alias has been deleted."); } function path_save($edit) { $src = $edit["src"]; $dst = $edit["dst"]; $pid = $edit["pid"]; if (!valid_url($src)) { $error[] = t("the normal path '%src' is invalid.", array("%src" => $src)); } if (db_result(db_query("SELECT COUNT(src) FROM {path} WHERE pid != '%d' AND src = '%s'", $pid, $src))) { $error[] = t("the normal path '%src' is already aliased.", array("%src" => $src)); } if (!valid_url($dst)) { $error[] = t("the alias '%dst' is invalid.", array("%dst" => $dst)); } if (db_result(db_query("SELECT COUNT(dst) FROM {path} WHERE pid != '%d' AND dst = '%s'", $pid, $dst))) { $error[] = t("the alias '%dst' is already in use.", array("%dst" => $dst)); } if ($error) { return path_form($edit, $error); } else { /* ** Normally, you would use path_set_alias to update the paths table, ** but this is a special case. We want to modify a specific row and the only ** way to do that is with pid. */ if ($pid) { db_query("UPDATE {path} SET src = '%s', dst = '%s' WHERE pid = '%d'", $src, $dst, $pid); } else { path_set_alias($src, $dst); } } return t("the alias has been saved.") . path_overview(); } ?>