Display public/non-public node status

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.

This looks great!

wallan - February 10, 2007 - 11:59

Thanks for this! Would it be possible to show some examples of how you might use $public_node? I'm still trying to learn this whole PHP business!

Allan

An example

Kimbecca - January 23, 2008 - 22:34

In your theme's node.tpl.php: (I have this above the title)

<?php if ($public_node):?><div id="public_node"><?php print "public" ?></div><?php endif; ?>

Then in style.css as an example I have:

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

 
 

Drupal is a registered trademark of Dries Buytaert.