Embed Panel Node View in code

By shane
Wed, 2014-08-06 20:48
2 comments

Share with Others

There are times when you want to programmatically embed a node display in a block or panel pane. You can do this by creating a block. You might try to use the node_view function to display the node. Doing so in a module might look something like this:
/**
 * Implements hook_block_info().
 */
function MYMODULE_block_info() {
  $blocks['MYMODULE_node_view'] = array(
    'info' => t('MYMODULE Node View Block'),
  );
 
  return $blocks;
}
 
/**
 * Implements hook_block_view().
 */
function MYMODULE_block_view($delta = '') {
  $block = array();
  if ($delta == 'MYMODULE_node_view') {
    // This is the node id of the node. This could be hard coded or pulled from something like a query string.
    $nid = 1;
    $output = "";
    $node = node_load($nid);
 
    // Replace this with your specific node type if necessary.
    if ($node->type == 'my_node_type') {
      $output = node_view($node);
    }
 
    $block['subject'] = t('MYMODULE Node View');
    $block['content'] = $output;
  }
  return $block;
}
If you are using Panels to override and design the node display of your node, you will notice that using node_view() does not display the node as you built it in panels. You can get the panels variant version by using the following code:
/**
 * Implements hook_block_info().
 */
function MYMODULE_block_info() {
  $blocks['MYMODULE_node_view'] = array(
    'info' => t('MYMODULE Node View Block'),
  );
 
  return $blocks;
}
 
/**
 * Implements hook_block_view().
 */
function MYMODULE_block_view($delta = '') {
  $block = array();
  if ($delta == 'MYMODULE_node_view') {
    // This is the node id of the node. This could be hard coded or pulled from something like a query string.
    $nid = 1;
    $output = "";
    $node = node_load($nid);
 
    // Replace this with your specific node type if necessary.
    if ($node->type == 'my_node_type') {
 
      // Get the node_view task.
      $task = page_manager_get_task('node_view');
 
      // Load the node into a context.
      ctools_include('context');
      ctools_include('context-task-handler');
 
      // Load the node context.
      $contexts = ctools_context_handler_get_task_contexts($task, '', array($node));
 
      // Render the node output.
      $output = ctools_context_handler_render($task, '', $contexts, array($node->nid));
    }
 
    $block['subject'] = t('MYMODULE Node View');
    $block['content'] = $output;
  }
  return $block;
}
You will need to replace the logic to get the node id, the node type, and the module name in the code above in order to get the embedded node view block to work.

Comments

Hi,

Good post, but am I wrong or in D7 the node_view function returns a renderable array ?
that is $output would be $output = drupal_render(node_view($node)); ?

Claudiu

You are exactly right. However, the returned content of the block_view hook can either be HTML, or a renderable array. This means you don't need to call drupal_render... the hook will know to do that for you if you return the render array.

Post new comment