Showing Blocks while editing a Node in Drupal
Sun6thMay
No matter how well you think you've designed a form for someone to use, there are always questions. and they are commonly simple ones too. Simple problems deserve simple solutions though, and a help section with a few words is worth the time for any form that you want to get filled in. That being said, a picture is worth a thousand words, and better still, a short video is worth a thousand pictures.
So, the simplest way of adding help to a form is to add content into an <aside/>, kind of like a FAQ section; I like an accordions interface personally. But, within Drupal, adding blocks of information from the context of editing a node of a particular content type is a challenge. Consider the following <?php?> snippet for dealing with this issue.
<?php
/* Show this block only if there is a node of */
/* content type "Menu Page" that is loaded in edit mode */
$entity_type = "node";
$valid_content_type = "menu_page"; /*note the hyphen */
$valid_content_type_alt = "menu-page"; /* vs. the dash */
$mode = "edit";
$valid_new_node_type = ((arg(1)=="add") AND (arg(2)== $valid_content_type_alt));
if( arg(0)=="$entity_type"){
/* based on the 0th element of the URL's query string, */
/* This is a node so get the NID */
$nid = arg(1);
$node = node_load($nid);
/* AND vs && both perform a logical 'and' operation, */
/* but AND stops evaluating as soon as the first argument */
/* is evaluated */
return ($valid_new_node_type OR ((arg(2)==$mode) AND ($node->type==$valid_content_type)));
}
?>
Enabling the PHP filter module opens up the opportunity to use php evaluations to display a block and gives you an interface within the block's visibility context section that looks like the diagram below.
A few cautions when using this module.
- Back up your database frequently, and
- DO NOT EVER play with this code on a production server. You will not be popular during business hours of the day!
Misplacing a comma can send your entire Drupal instance into a tail-spin of Error 500 pages, including the admin pages too! Think about it, do you want anyone asking you, "what happened?", while you're trying to recover from this fatal error.
