Learning Drupal. I know now that I can create block through the backend, but I would rather precreate them programmatically, if it's possible. In fact I'm pretty sure that it's possible. But what's the easiest way?


For Drupal 7:

Use hook_block_info and hook_block_view hooks in your custom module. hook_block_info defines the block. It will show up in admin > structure > blocks. *hook_block_view* displays the content. See examples below from the Drupal API.

Example of hook_block_info, where two blocks are defined (titled Syndicate and Recent content):

function hook_block_info() {
  // This example comes from node.module.
  $blocks['syndicate'] = array(
    'info' => t('Syndicate'), 
    'cache' => DRUPAL_NO_CACHE,

  $blocks['recent'] = array(
    'info' => t('Recent content'),
    // DRUPAL_CACHE_PER_ROLE will be assumed.

  return $blocks;

Example of hook_block_view:

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;

Refer to the Blocks API page on Drupal.org for full list of hooks. Drupal 6 is a little different. There is no hook_block_view hook; instead use hook_block to declare blocks.


For Drupal 6, the short answer is that you create a custom module that implements hook_block.

For Drupal 7, you use the Block API.

If this is your first foray into module development, I highly recommend Pro Drupal Development or Pro Drupal 7 Development. Pretty much everything you need to know is in there.

  • It seems that hook_block can only be used in separate module :| I still do not get why this complexity... Is it a usual practice to have modules for everything in Drupal? – jayarjo Jun 22 '11 at 6:45
  • 2
    Yes, modules are a key concept of Drupal. In most ways, Drupal itself is a set of modules and everything gets done via module hooks. – mpdonadio Jun 22 '11 at 13:22

You might be looking for what I was looking for and found it here:

block_custom_block_save($edit, $delta).

But when you look at this function you'll see that you can just do the db_update yourself.

  'body' => $edit['body']['value'],
  'info' => $edit['info'],
  'format' => $edit['body']['format'],
->condition('bid', $delta)

In Drupal 7, take a look at block_add_block_form_submit in modules/block/block.admin.inc.

This is the regular block creation form; it appears to just insert the records directly rather than calling another function. Note that each record in the block table is linked to a single theme (you can just use the active one but it's safer to insert records for all themes).

The block_custom table holds the content.


You can use the Features Extra module to export blocks.

Alternatively, just create records directly in the blocks table.

INSERT INTO blocks (
  module, delta, theme, status, weight, region, custom,
  throttle, visibility, pages, title, cache)
  • 1
    Extra module or direct sql query... there should be something cleaner around I think. – jayarjo Jun 21 '11 at 17:44

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