Drupal: add administrative links to nodes

Drupal

If you want to be able to quickly add, edit, or track a node, you can add some links to each node. I did this by creating a variable in phptemplate.php that can then simply be used in node.tpl.php.

Administrative links for nodes
Example of links you can add to a node.

Code for Drupal 6.x

I added an improved code snippet below this one.

I finally figured out how to use the phptemplate_preprocess() function. It's similar to Drupal 5.x _phptemplate_variables() but just a little different.

You could use the function phptemplate_preprocess() and test &hook for "node" so it's only executed for nodes. It is easier to use the function phptemplate_preprocess_node(). The difference is that you then omit the parameter &hook. In either case, you then create a variable $admin_links that contains everything for our links, including all the HTML.

In node.tpl.php you then use a simple <?php print $admin_links; ?> to display the links at the bottom of the node. Just add some styles to make it line up with any existing links under the node body and it's a done deal.

<?php
/* Preprocess function for nodes */
function phptemplate_preprocess_node(&$vars) {

 
// Create admin links for posts
 
$vars['admin_links'] = '';
  if (
user_access('administer nodes')) {

   
$vars['admin_links'] .= '<ul class="links inline">';
   
$vars['admin_links'] .= '<li class="node-add">' .
     
l(t('New'), 'node/add/' . $vars['node']->type,
        array(
'attributes' => array('title'=>'Add new post'))) .
     
'</li>';
   
$vars['admin_links'] .= '<li class="node-edit">' .
     
l(t('Edit'), 'node/' . $vars['node']->nid . '/edit',
        array(
'attributes' => array('title'=>'Edit this post'))) .
     
'</li>';
   
$vars['admin_links'] .= '<li class="node-track">' .
     
l(t('Track'), 'node/' . $vars['node']->nid . '/track',
        array(
'attributes' => array('title'=>'Track this post'))) .
     
'</li>';
   
$vars['admin_links'] .= '</ul>';

  }

}

?>

(In template.php you omit the closing php tag ?> but I need it here in order to render the code block.)

In node.tpl.php you can then use a print statement:

<?php
print $admin_links;
print
$links;
?>

And in the style sheet I added something like:

/* Admin links */
.node .node-add {
  background: transparent url(img/node-add.gif) no-repeat left center;
  padding-left: 20px;
}
.node .node-edit {
  background: transparent url(img/node-edit.gif) no-repeat left center;
  padding-left: 20px;
}
.node .node-track {
  background: transparent url(img/node-track.gif) no-repeat left center;
  padding-left: 20px;
}

In Drupal 5.x I had created a function show_admin_links($node_type, $node_id) which I then called in the node template. This method will still work in Drupal 6.x but using the above example is a much neater solution to keep the template file as clean as possible. It also eliminates the need to pass parameters, such as the node type and ID.

Drupal 6.x — better solution

Added August 8, 2010

It would be preferable to use the following code in template.php instead of the previous snippet, using Drupal link theming.

<?php

 
// --- Create admin links for posts ---
 
if (user_access('administer nodes')) {

   
$admin_links = array();
   
$admin_links_attr = array('class' => 'links inline');

   
$admin_links['node-add'] = array(
     
'title' => t('New'),
     
'href' => 'node/add/' . $vars['node']->type,
     
'attributes' => array('title' => t('Add new node of type !type', array('!type' => $vars['node']->type)))
    );
   
$admin_links['node-edit'] = array(
     
'title' => t('Edit'),
     
'href' => 'node/' . $vars['node']->nid . '/edit',
     
'attributes' => array('title' => t('Edit this node'))
    );
   
$admin_links['node-track'] = array(
     
'title' => t('Track'),
     
'href' => 'node/' . $vars['node']->nid . '/track',
     
'attributes' => array('title' => t('Track this node'))
    );

   
$vars['admin_links'] = theme_links($admin_links, $admin_links_attr);

  }

?>

The variable $admin_links remains the same in the node template(s), and the CSS remains unchanged compared to the previous example.

Code for Drupal 5.x

In Drupal 5.x this whole procedure is very similar, although the l() function is different.

In template.php add this to the function _phptemplate_variables():

<?php
function _phptemplate_variables($hook, $vars) {

 
// Create admin links for posts
 
if ($hook == 'node') {

    if (
user_access('administer nodes')) {

     
$vars['admin_links'] = '<ul class="links inline">';
     
$vars['admin_links'] .= '<li class="node-add">' .
       
l(t('Add'), 'node/add/' . $vars['node']->type,
          array(
'title'=>'Add new post')) . '</li>';
     
$vars['admin_links'] .= '<li class="node-edit">' .
       
l(t('Edit'), 'node/' . $vars['node']->nid . '/edit',
          array(
'title'=>'Edit this post')) . '</li>';
     
$vars['admin_links'] .= '<li class="node-track">' .
       
l(t('Track'), 'node/' . $vars['node']->nid . '/track',
          array(
'title'=>'Track this post')) .
       
'</li>';
     
$vars['admin_links'] .= '</ul>';

    }

  }

}

?>

P.S. Here I create a new list with the links. If you prefer to modify the existing $links for the node, take a look at the tutorial at 11 heavens.

Comments are closed