Drupal block render

Here's a handy function that returns markup for a block, with or without contextual links.

<?php
 
/**
 * Renders a block with or without contextual links.
 *
 * @param string $id
 *   Block id.
 * @param string $module
 *   Module name.
 * @param boolean $show_contextual_links
 *   Whether to include contextual links.
 *
 * @return string
 *   Block markup.
 */
function loopduplicate_block_render($id, $module = 'block', $show_contextual_links = TRUE) {
  $block = loopduplicate_block_get_renderable_array($id, $module, $show_contextual_links);
  $markup = render($block);
 
  return $markup;
}
 
/**
 * Gets a block render array with or without contextual links.
 *
 * @param string $id
 *   Block id.
 * @param string $module
 *   Module name.
 * @param boolean $show_contextual_links
 *   Whether to include contextual links.
 *
 * @return array
 *   A block render array.
 */
function loopduplicate_block_get_renderable_array($id, $module = 'block', $show_contextual_links = TRUE) {
  if ($show_contextual_links) {
    $block = block_load($module, $id);
    $block = _block_get_renderable_array(_block_render_blocks(array($block)));
  } else {
    $block = module_invoke($module, 'block_view', $id);
  }
 
  return $block;
}
 
?>

Tags

Article Type

General