hook_block_view

7 block.api.php hook_block_view($delta = '')
8 block.api.php hook_block_view($delta = '')

Return a rendered or renderable view of a block.

Parameters

$delta: Which block to render. This is a unique identifier for the block within the module, defined in hook_block_info().

Return value

An array containing the following elements:

  • subject: The default localized title of the block. If the block does not have a default title, this should be set to NULL.
  • content: The content of the block's body. This may be a renderable array (preferable) or a string containing rendered HTML content.

For a detailed usage example, see block_example.module.

See also

hook_block_info()

hook_block_view_alter()

hook_block_view_MODULE_DELTA_alter()

Related topics

17 functions implement hook_block_view()

1 invocation of hook_block_view()

File

modules/block/block.api.php, line 214
Hooks provided by the Block module.

Code

function hook_block_view($delta = '') {
  // This example is adapted from node.module.
  $block = array();

  switch ($delta) {
    case 'syndicate':
      $block['subject'] = t('Syndicate');
      $block['content'] = array(
        '#theme' => 'feed_icon', 
        '#url' => 'rss.xml', 
        '#title' => t('Syndicate'),
      );
      break;

    case 'recent':
      if (user_access('access content')) {
        $block['subject'] = t('Recent content');
        if ($nodes = node_get_recent(variable_get('node_recent_block_count', 10))) {
          $block['content'] = array(
            '#theme' => 'node_recent_block', 
            '#nodes' => $nodes,
          );
        }
        else {
          $block['content'] = t('No content available.');
        }
      }
      break;
  }
  return $block;
}

Comments

content Renderable Array

For those who don't know what a Renderable Array is, here's the explanation: http://randyfay.com/node/79

Caching and JavaScript

If you call drupal_add_js() inside a block and have caching enabled for that block, that JS code will not be included after the block gets cached!

It took me a bit of time to figure this out. There is a way to include the JS you need and still enable caching for the html. To do this, following these instructions.

#1: Turn off full caching for the block under hook_block_info() by setting 'cache' to: DRUPAL_NO_CACHE

#2: In hook_block_view() cache the output of the html after adding the JS. Below is a sample function that I call that does this and returns the cached block after it has been generated. In this example, I'm making a cached block per user, but that isn't required.

/**
* The dashboard tabs block.
*/
function _tbf_dashboard_tabs_block() {
  global $user;
 
  // Add the JS to make the tabs work.
  drupal_add_library('system', 'ui.tabs'); 
  drupal_add_js('jQuery(function() { jQuery("#tabs").tabs(); });', 'inline');
   
  $cache_name = 'dashboard_tabs_' . $user->uid;
  $cache = cache_get($cache_name, 'cache_block');
 
  // Is the tab html cached already?
  if (!empty($cache) && isset($cache->data) && !empty($cache->data)) {
    // It is cached. Return the cached content.
    return $cache->data;
  }
  else {
    // Grab the html from the tabs template file.
    $tab_html = theme('dashboard_tabs');
   
    // Cache for 24 hours.
    cache_set($cache_name, $tab_html, 'cache_block', time() + 86400);
    return $tab_html;
  }
}

Login or register to post comments