Community Documentation

Display public/non-public node status

Last updated August 26, 2009. Created by crischan on December 25, 2006.
Edited by bekasu, domesticat, Dublin Drupaller. Log in to edit this page.

Description

If you use some kind of Access Control like I do to make certain nodes only viewable by registered users, you may want to display some text or icon along with a non-public node. This way users can be easily made aware that they are looking at some non-public content. Using Drupal 5 and phptemplate this can easily be done using your template.php

Step 1 of 3

Make sure you have the

<?php
function _phptemplate_variables($hook, $vars) {
/* ... */
}
?>

in your template.php in your theme directory. If you do, build upon the code already in there, if not use the code from step 2 as a template.

Step 2 of 3

<?php
function _phptemplate_variables($hook, $vars) {
 
/* this is needed if you want to display the nonpublic-information in the page template */
 
static $public_node;
  switch (
$hook) {
    case
'page':
     
/* add public_node info to vars so it can be used in page.tpl.php */
     
$vars['public_node'] = $public_node;
      return
$vars;

    case
'node':
     
/* load current user object */
     
global $user;
     
/* backup current user object */
     
$my_account = $user;
     
/* set user object to anonymous user so that node_access() thinks we're anonymous */
     
$user = drupal_anonymous_user();
     
/* get info, if anonymous can view this node. */
      /* using node_access(), this takes into account any access control modules */
     
$public_node = node_access('view', $vars['node']);
     
/* add public_node info to vars so it can be used in node.tpl.php */
     
$vars['public_node'] = $public_node;
     
/* restore current user account */
     
$user = $my_account;
      return
$vars;
  }
  return array();
?>

Step 3 of 3

In your page.tpl.php and/or node.tpl.php use $public_node variable to display a info text. I display a little icon of a lock next to the title to show members of our foundation website (which are always members of the foundation itself) that the content they read is not publicly available.

Example

In your theme's node.tpl.php:

<?php
if ($public_node) {
echo
'<div id="public_node">public</div>';
}
?>

Then add the following CSS to your stylesheet:

#public_node {
float:right;
color:#bdd2be;
font-size:75%;
}

About this page

Drupal version
Drupal 5.x
Audience
Themers

Theming Guide

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.