Ever wanted to include a Node in a Block? This can be great for having a webform in a sidebar, or a piece of easily CCK-editable content in addition to your node content. Includes tabs for admin.

Not thoroughly tested, but works on my sites.

DRUPAL 6 VERSION


<?php

$nid = '5';  // the Node ID to display

if (user_access('administer nodes')) {
  print '<div class="tabs"><ul class="tabs primary clear-block">';
}
$bnode = node_load($nid);
if (!is_object($bnode)) {
  drupal_set_message('Error: Cannot find node \''.$bnode.'\' for inclusion in block.', 'error');
}
if ($bnode->title && false): print '<h3 class="title">'. $bnode->title .'</h3>'; endif;
if (user_access('administer nodes')){
  print "<li class='active'>".l("<span class='tab'>".t('View')."</span>", $_GET['q'], array('html' => true, 'attributes' => array('class' => 'active')))."</li>";
  print "<li>".l("<span class='tab'>".t('Edit')."</span>", 'node/'.$nid.'/edit', array('html' => true, 'query' => 'destination='.$_GET['q']))."</li>";
  print "<li>".l("<span class='tab'>".t('Go to page')."</span>", 'node/'.$nid, array('html' => true))."</li>";
  // **UNCOMMENT THIS LINE FOR WEBFORMS** print "<li>".l("<span class='tab'>".t('Results')."</span>", 'node/'.$nid.'/webform-results', array('html' => true, 'query' => 'destination='.$_GET['q']))."</li>";
  print '</ul></div>';
}
print node_view($bnode, FALSE, TRUE);
?>

DRUPAL 5 VERSION

<?php

$nid = '26';  // the Node ID to display

if (user_access('administer nodes')) {
  print '<div id="tabs-wrapper" class="clear-block"><ul class="tabs primary">';
}
$bnode = node_load($nid);
if (!is_object($bnode)) {
  drupal_set_message('Error: Cannot find node \''.$bnode.'\' for inclusion in block.', 'error');
}
if ($bnode->title): print '<h3'. ($tabs ? ' class="with-tabs title"' : ' class="title"') .'>'. $bnode->title .'</h3>'; endif;
if (user_access('administer nodes')){
  print "<li class='active'>".l(t('View'), $_GET['q'], array('class' => 'active'), NULL, NULL, FALSE, FALSE)."</li>";
  print "<li>".l(t('Edit'), 'node/'.$nid.'/edit', array(), 'destination='.$_GET['q'], NULL, FALSE, FALSE)."</li>";
  print "<li>".l(t('Go To Page'), 'node/'.$nid, array(), NULL, NULL, FALSE, FALSE)."</li>";
  // **UNCOMMENT FOR WEBFORMS** print "<li>".l(t('Results'), 'node/'.$nid.'/results', array(), 'destination='.$_GET['q'], NULL, FALSE, FALSE)."</li>";
  print '</ul></div>';
}
print node_view($bnode, FALSE, TRUE);
?>

might want to lose the line with the h3 stuff in it if there is a block on the title itself.

Comments

ultimateboy’s picture

For D5, there is also a module, Node as Block which appears to have the same functionality.

--matt
http://www.monarchdigital.com

-- matt tucker

thedawn’s picture

Thanks danielb, just what I needed on Drupal 6 ...

xynjav’s picture

Hello, can you give me a some quick instructions regarding how to integrate this code into Drupal 6.

Thanks a bunch!

ultimateboy’s picture

Pretty simple, actually. First of all, you have to make sure that you have the PHP Filter module installed. Then, create a new block via Admin > Site Building > Blocks, make sure that the input format is set to PHP, and then copy and paste the above code into the block body, remembering to change the node id (nid) to the id of the node you would like to display.

--matt
http://www.monarchdigital.com

-- matt tucker

xynjav’s picture

Seems easy enough. Thanks a million!

alaneKilauea’s picture

Thank you danielb! Wow, I spent all of yesterday looking for this - awesome, works great!

Alane

WorldFallz’s picture

can't you just create a full node block view with views and set the number of items to display to 1? what am i missing?

===
"Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime."
-- Lao Tzu
"God helps those who help themselves." -- Benjamin Franklin
"Search is your best friend." -- Worldfallz

NiklasM’s picture

I have placed my webform in a block with the snippet. It works, but there's a problem.

If the user doesn't fill in all mandatory fields the whole webform is loaded in the main content area and the empty mandatory fields are highlighted. I would prefer that the current page was reloaded and that the fields where highlighted in the webform block. Does anyone have an idea of how to accomplish this?

thezaib’s picture

Can the above mentioned code make teaser inside a block?