Drupal blocks are typically meant to store content to be displayed across several pages. For that use case it makes sense to have a separate admin interface for blocks. But often there's a desire to include additional single-use content in a region.

For example, you may wish to show a menu on every page in a section, but add a PDF download link that varies across pages. You certainly don't want to create a block for every link on every page. Furthermore, views, panels, and other modules may be overkill for what will amount to a couple lines of code.

Enter drupal_add_region_content() for Drupal 7 and 8 or drupal_set_content() for Drupal 5 or 6. Not only will these functions allow you to provide simple on-click editing of all content relevant to a single page, you can also use them to override existing content.

Example

Let's build our example above and assume we have a page with a primary content area on the right, a left sidebar, and the following elements:

  • Title (primary content area)
  • Body (primary content area)
  • Automated section menu  (block displayed in the upper-left of the left sidebar)
  • Related PDF file links (displayed below the section menu on the left) ; let's call this field_related_pdf and place it in the "left-sidebar" region.

The automated section menu functionality is defined as a block and could use the popular Menu Block module to provide a context-sensitive menu. The other elements are defined on a node add/edit form.

Drupal 6

For Drupal 6, to get the related PDF links to display in that region, add the code in one of the following sections to your node-[type].tpl.php file

For Drupal 6, we make use of a handy function called drupal_set_content() and the premade $yourfield_rendered, which contains the HTML code to display your field exactly as you set it up using CCK field display settings.

drupal_set_content('left_sidebar', $field_related_pdf_rendered);

Drupal 7

With Drupal 7, things get a bit more complex as you must add the extra content using a hook (hook_page_build() to be exact).

<?php
function yourmodule_page_build(&$page) {
 
// Load the node object
 
$node = node_load(arg(1));
 
// Grab the field_related_pdf in the default language
 
$related_pdf = field_get_items('node', $node, 'field_related_pdf');
 
// This example assumes only one PDF
 
$related_pdf_safe = $related_pdf[0]['safe_value']; 
 
// If there's nothing in the left sidebar
 
$page['left_sidebar'] = array(
   
'#markup' =>$related_pdf_safe, );
}
?>

Coming soon, we'll build on this example to show how a page hierarchy can be built.