26

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?

closed as primarily opinion-based by Pierre.Vriens, Shawn Conn, Neograph734, DRUPWAY, Krishna Mohan Jan 23 '17 at 6:58

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

34

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):

    <?php
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:

<?php
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;
}
?>

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.

6

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
3

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

/modules/block/block.module:
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.

db_update('block_custom')
->fields(array(
  'body' => $edit['body']['value'],
  'info' => $edit['info'],
  'format' => $edit['body']['format'],
))
->condition('bid', $delta)
->execute();
2

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.

1

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)
VALUES (
  'views','blog-block_1','mytheme',1,10,'sidebar_first',0,0,1,'blog\r\nblog/*','',-1)
  • 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.