function hook_block_view

You are here

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

Return a rendered or renderable view of a block.


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

Return value

Either an empty array so the block will not be shown or 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. If the content is empty the block will not be shown.

For a detailed usage example, see block_example.module.

See also




Related topics

17 functions implement hook_block_view()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

aggregator_block_view in modules/aggregator/aggregator.module
Implements hook_block_view().
block_block_view in modules/block/block.module
Implements hook_block_view().
block_test_block_view in modules/block/tests/block_test.module
Implements hook_block_view().
blog_block_view in modules/blog/blog.module
Implements hook_block_view().
book_block_view in modules/book/book.module
Implements hook_block_view().

... See full list

1 invocation of hook_block_view()
_block_render_blocks in modules/block/block.module
Render the content and subject for a set of blocks.


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


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'),

    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.');
  return $block;


For those who don't know what a Renderable Array is, here's the explanation:

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;

Like "4elove4ekFs" pointed out, to have JS/CSS be attached to a block when caching is on, you should use the #attached property.

Here is the code in how to do this:

$block['content'] = array(
  '#markup' => mymodule_testblock_content(),
  '#attached' => array(
      'css' => array(
        drupal_get_path('module', 'mymodule') . '/css/mymodule.css',
      'js' => array(
        drupal_get_path('module', 'mymodule') . '/js/mymodule.js',

Just use rendered array and '#attached' property

I have a module of type block.Now based on the user i want to display different blocks .
So i have a written the module but my dilemma is if its possible to call a block(default) inside our custom block?

If you want to pass an argument to a block, just put $arg = '' in the declaration and then you can access it. Not sure why it's not mentioned here but it does work in Drupal 7.

Can anybody comment on the "just use it" part?

I'm trying to add various block from external source to various pages (think "widget" from outside Drupal)

Where can I put what to have a page display the $x widget?


One solution to this problem can be to store a variable in $_SESSION and then retrieve it in hook_block_view. but i don't like too much this solution.
i would prefer to have a variable in the theme $vars, and read it in the block. but i think it's not possible.

This is very usefull.

If you run into an PHP fatal error:

PHP Fatal error: Unsupported operand types in .../modules/block/block.module on line 352

...while trying to access "admin/dashboard" path, it may be caused by your block view using string type as content instead of renderable array.

how to render the video from the hook_block_view.
How can i get it .
please help in detail

Can i use theme_image theme function to render a video?