Tuesday, July 2, 2013

How to Create Block Programmatically in Drupal 7

How to Create Block Programmatically in Drupal 7:

I try to create with block with update hook but failed. So have to check out with another method which is hook_block. We need to use the following hook:

  • hook_block_info : To define custom block and display block at administration page
  • hook_block_configure : To define custom block configuration block (e.g. title, body, text format)
  • hook_block_save : To save configuration option from hook_block_configure()
  • hook_block_view : To build the contents of the block, can include functionality code

The following link explain how to use the hook
References: http://fourkitchens.com/blog/2012/07/18/building-custom-blocks-drupal-7

The following is the sample how to set text format:

function MODULE_block_configure($delta=''){
  $form = array();

  switch ($delta) {

    case 'blockname':
      # Text field form element
    $form['text_body'] = array(
      '#type' => 'text_format',
      '#title' => t('Enter your html code'),
      '#default_value' => variable_get('text_variable',''),
      '#format' => 'full_html',
  return $form;

No comments:

Post a Comment