Node Alias模块

/**
* @file
* Allows module to be nodealiasd giving it an alias.
*
*/
/**
* Implementation of hook_help().
*/
function nodealias_help($section) {
switch ($section) {
case 'admin/modules#description':
return t('Enables users to make a node alias');
case 'admin/help#nodealias':
return t("Need something here");
case 'node/add/nodealias':
return variable_get('nodealias_help', '');
case 'node/add#nodealias':
return t("A node aliasd node provides an alias to a node");
}
}
/**
* Implementation of hook_node_name().
*/
function nodealias_node_name($node) {
return t('node alias');
}
/**
* Implementation of hook_perm().
*/
function nodealias_perm() {
return array('create node alias', 'edit own node alias');
}
/**
* Implementation of hook_access().
*/
function nodealias_access($op, $node) {
global $user;
if ($op == 'create') {
return user_access('create node alias');
}
if ($op == 'update' || $op == 'delete') {
if (user_access('edit own node alias') && ($user->uid == $node->uid)) {
return TRUE;
}
}
}
/**
* Implementation of hook_link().
*/
function nodealias_link($type, $node = 0, $main) {
$links = array();
if ($type == 'node' && $node->type == 'nodealias') {
// Don't display a redundant edit link if they are node administrators.
if (nodealias_access('update', $node) && !user_access('administer nodes')) {
$links[] = l(t('edit this node alias'), "node/$node->nid/edit");
}
}
else if ($type == 'node' && $node->orig_type == 'nodealias')
{
$func = $node->type . '_access';
if ( function_exists($func) )
{
if ( $func('update', $node) )
{
$links[] = l(t('edit orginal'), "node/" . nodealias_get_nid($node->node_source) . "/edit");
}
}
}
return $links;
}
/**
* Implementation of hook_menu().
*/
function nodealias_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array('path' => 'node/add/nodealias', 'title' => t('alert'),
'access' => nodealias_access('create', NULL));
}
return $items;
}
/**
* Loads all of the nodealias specific information when an event node is viewed.
*
* @ingroup nodealias_node
* @param &$node The node being viewed.
* @return An array of event-specific information.
*/
function nodealias_load(&$node)
{
$nodealias = db_fetch_object(db_query("SELECT * FROM {nodealias} WHERE nid = %d", $node->nid));
return $nodealias;
}
/**
* Implementation of hook_form().
*/
function nodealias_form(&$node)
{
$output = '';
$output .= form_textfield(t('Node Source'), 'node_source', $node->node_source, 32, 32, 'Node id (ex; 6) or node path if path module is enabled', NULL, TRUE);
return $output;
}
/**
* Updates the nodealias database table when event nodes are inserted.
*
* @ingroup event_node
* @param @$node The node that is being inserted.
*/
function nodealias_insert(&$node)
{
db_query("INSERT INTO {nodealias} (nid, node_source) VALUES (%d, '%s')", $node->nid, $node->node_source);
}
/**
* Updates the nodealias database table when event nodes are updated.
*
* @ingroup nodealias_node
* @param &$node The node that is being updated.
*/
function nodealias_update(&$node)
{
db_query("UPDATE {nodealias} SET node_source = '%s'", $node->node_source);
}
/**
* Deletes rows from the event database table when event nodes are deleted.
*
* @ingroup event_node
* @param &$node The node that is being deleted.
*/
function nodealias_delete(&$node) {
db_query("DELETE FROM {nodealias} WHERE nid = %d", $node->nid);
}
/**
* Prepares a nodealias for viewing.
*
* @ingroup nodealias_node
* @param &$node the event to be prepared
* @param $main whether or not the main page is requesting the node
* @param $page whether or not the event is being viewed by itself
* @return a string containing the event after it has been passed through the theme subsystem
*/
function nodealias_view(&$node, $main = 0, $page = 0)
{
$nid = nodealias_get_nid($node->node_source);
$orig_type = $node->type;
if ( $nid > 0 )
{
$nodealias = node_load(array('nid' => $nid));
if ( $nodealias )
{
nodealias_node_view($nodealias, $main, $page);
if ( $node->title == '=' )
{
$node->title = $nodealias->title;
}
foreach ( $nodealias as $field => $value )
{
if ( $field != 'nid' )
{
$node->$field = $nodealias->$field;
}
}
$node->orig_type = $orig_type;
}
else
{
$node->title = "Page Not Found";
$node->body = "";
$node->teaser = "";
return "";
}
}
else
{
$node->title = "Page Not Found";
$node->body = "";
$node->teaser = "";
return "";
}
}
function nodealias_node_view(&$node, $teaser = FALSE, $page = FALSE) {
$node = array2object($node);
// Remove the delimiter (if any) that separates the teaser from the body.
// TODO: this strips legitimate uses of '' also.
$node->body = str_replace('', '', $node->body);
// The 'view' hook can be implemented to overwrite the default function
// to display nodes.
if (node_hook($node, 'view')) {
node_invoke($node, 'view', $teaser, $page);
}
else {
$node = node_prepare($node, $teaser);
}
// Allow modules to change $node->body before viewing.
node_invoke_nodeapi($node, 'view', $teaser, $page);
return theme('node', $node, $teaser, $page);
}
function nodealias_get_nid($node_source)
{
if ( is_numeric($node_source) )
{
$nid = $node_source;
}
else
{
$path = drupal_get_normal_path($node_source);
if ( strpos($path, 'node/') != 0 )
{
return 0;
}
$nid = substr($path, 5);
}
return $nid;
}
?>

它的SQL文件的源码如下:

CREATE TABLE nodealias (
nid int(10) unsigned NOT NULL default '0',
node_source text NOT NULL default '',
PRIMARY KEY (nid)
);

Taxonomy upgrade extras: