3

In Drupal 7, can I use hook_block_info() in a theme?

I would like my theme to create a block (which would contain dynamic content). This will allow the user to move it around where he wants to, or simply hide it.

Is this the way to go?

/**
 * Implements hook_block_info() 
 */
function mytemplate_block_info()
{
  $blocks['mytemplate-block'] = array(
    'info' => t('TemplateBlock'),
    'weight' => -100,
    'status' => 1,
    'region' => 'content',
    'visibility' => BLOCK_VISIBILITY_LISTED,
    'pages' => '<front>',
  );

  return $blocks;
}

/**
 * Implements hook_block_view() 
 */
function mytemplate_block_view($delta = '')
{
  $block = array();

  switch ($delta) {
    case 'mytemplate-block':
      $block['content'] = 'some custom html';
    break;

  }

  return $block;
}
7

I'm afraid you can't, no, it has to be in a module.

The array of blocks is built up in _block_render_blocks() using the module_invoke() function which, as its name suggests, only invoke hooks for modules and not themes.

If you haven't written a custom module before don't be afraid to get stuck in its pretty easy. I'd highly recommend the Module Developer's Guide which will tell you everything you need to know.

  • Thanks I'll food another way then!I am familiar with modules but not themes and was wondering if I could also build blocks from themes. I appreciate your help! – Weboide Mar 3 '12 at 23:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.