Automatic help block for nodes

Last modified: July 25, 2007 - 23:59

I wanted an easy way to add a help block to some pages on my site, preferably so users could do it. Creating custom blocks and assigning to paths was cumbersome to say the least.

To do this create a new content type using CCK called 'helpfile' with a single additional text field 'path'. Then add a custom block with the code below. Users can then create help nodes pointing to the parent node. For example to add help to node/23 create a new node of type helpfile with the path set to "node/23". It will automagically appear when node/23 is visited. For multiple page webforms add the page number as a third part of the path for pages after the first, eg. setting path to "node/23/2" will create a help block for page 2 of the webform in node 23. I hope this is useful for someone.

<?php
//display a helpfile node (body only) in a block if the path matches

$path = $_GET['q'];

// for multi-page webforms
if ($_POST['details']['page_num']) {
 
$path .= '/'. $_POST['details']['page_num'];
}

// now look for a helpfile for this path
$result = db_query("SELECT nid FROM {content_type_helpfile} WHERE field_path_value = '%s' LIMIT 5", $path);  
  if (
db_num_rows($result)) {
   
$output = '';
    while (
$row = db_fetch_array($result)) {
     
$node = node_load(array('nid' => $row['nid']));
     
$output .= '<h2 class="title">'. check_plain($node->title) .'</h2>';
     
$output .= '<div class="content">'. check_markup($node->body) .'</div>';
    }
      return
$output;
    }
    else {
      return;
    }
?>

Thanks a lot for this.. To

lelizondob - November 17, 2008 - 07:03

Thanks a lot for this..

To show the block in multiple paths you'll need to change the Path Field to allow multiple values, then you'll have to change the query because the field_path_value will no longer be part of the content_type_whatever table, but it will be in his own new table, 'content_field_path'..

$result = db_query("SELECT nid FROM {content_field_path} WHERE field_path_value = '%s' LIMIT 5", $path);  

Notice the "content_field_path" changed.

Now you can use the help node or whatever in more than one path. I'm using this to show a banner image in some nodes.

To show an image field you have to create an imagefield and then add to the $output:

$output .= content_format('field_image', $node->field_image[0]);

Luis

 
 

Drupal is a registered trademark of Dries Buytaert.