4

I need to enable block from my custom module, to show block when the module is enabled, I set the status 1, but nothing happens until I manually change in Structure > Blocks .

function test_block_info() {
   $blocks['my-block-id'] = array(
    'info'   => t('My block'),
    'status' => 1,
    'region' => 'sidebar_first',
    'cache'  => DRUPAL_NO_CACHE,
      );
    return $blocks;
 }
1
  • Best way would be to add your elements using hook_page_alter()
    – AKS
    Commented Feb 21, 2013 at 17:06

5 Answers 5

5

For drupal 7 the best way will be write hook_enable function in *.install file of your custom module:

<?php

function YOUR_MODULE_NAME_enable() {
          $blocks = array(
            array(
              'status' => 1,
              'weight' => 20,
              'region' => 'sidebar_second',
              'visibility' => 1,
              'pages' => "instruments/*",
              'module' => 'instruments',
              'delta' => 'instruments_sidebar',
              'theme' => 'markets',
            ),
            array(
              'status' => 1,
              'weight' => 19,
              'region' => 'sidebar_second',
              'visibility' => 1,
              'pages' => "instruments/*\n<front>",
              'module' => 'instruments',
              'delta' => 'instruments_search',
              'theme' => 'markets',
            )
          );

          foreach($blocks as $block){
            db_merge('block')
              ->key(array(
                'module'=>$block['module'],
                'delta'=> $block['delta'],
                'theme'=> $block['theme']
              ))
              ->fields(array(
                'status' => $block['status'],
                'weight' => $block['weight'],
                'region' => $block['region'],
                'visibility' => $block['visibility'],
                'pages' => $block['pages'],
              ))
              ->execute();
          }
    }

This is code from my custom module, and it works :) Here I enable my custom blocks, so I modify 'block' table. For custom blocks you should update 'block_custom' table too.

If you don't know how drupal store settings, just make changes manually and then check DB.

NOTE: db_merge function is very useful - it inserts data if they absent (if you just installed your module) or updates it (if you disabled and then enabled module)

3

You have to update database table block like Drupal core does:

  db_update('block')
    ->fields(array(
      'status' => $block['status'],
      'weight' => $block['weight'],
      'region' => $block['region'],
    ))
    ->condition('module', $block['module'])
    ->condition('delta', $block['delta'])
    ->condition('theme', $block['theme'])
    ->execute();
  // Flush cache
  cache_clear_all();

I didn't test it. Maybe you also need to flush block cache before query.

1
  • Nice, i looked around for this so long! For me the only function which could activate a custom block in a custom region, but is there a little fault in your script? 'status' => $block['status'], should be 'status' => 'your_custom_block_status', and the same with other lines.
    – Bruno
    Commented Oct 31, 2015 at 17:02
3

you also can utilize Context API. Below is what I did to enable/place a block programmatically.

function MYMODULE_context_load_alter(&$context) {

  $your_custom_requirement = ... Whatever you define ...;

  if ($your_custom_requirement === TRUE) {
    
    $context['reactions']['block']['blocks']['block-machine-name-here'] = array(
      'module' => 'module_that_defined_the_block',
      'delta'  => 'block_delta_name',
      'region' => 'sidebar_first', // place to sidebar
      'weight' => '-10', // block weight in the region
    );
  }
  
}
0

Managing blocks through code can be very valuable, especially if you are running clean deployments of just the codebase. You can use hook_block_info_alter() to configure the default blocks disable/move/change visibility status.

<?php
/**
 * Implements hook_block_info_alter().
 */
function mymodule_block_info_alter(&$blocks, $theme, $code_blocks) {
  // move navigation to footer
  $blocks['system']['navigation']['region'] = 'footer';
  // disable the powered by drupal block and login block
  $blocks['system']['powered-by']['status'] = 0;
  $blocks['user']['login']['status'] = 0;
  // change the visibility to front page only
  $blocks['search']['form']['visibility'] = BLOCK_VISIBILITY_LISTED;
  $blocks['search']['form']['pages'] = '';
}

The same options can be used to manage your own blocks:

/**
 * Implements hook_block_info().
 */
function mymodule_block_info() {
  $blocks = array();
  $blocks['mymodule_block_example'] = array(
    'info' => t('MyModule: Block example'),
    'status' => 1,
    'region' => 'header',
    'visibility' => BLOCK_VISIBILITY_LISTED,
    'pages' => '',
  );
  return $blocks;
}
0

You just have to additionally call hook_block_info_alter() like in the following example. Don't know why. Seems the block needs to be registered first with hook_block_info(). And only after that you can alter its settings to enable and place it programmatically. Please feel free to add any better explanation to this answer in the comments.

The downside of this approach is, that this block ain't configurable through the back-end. I mean, indeed it is, but the new values always will always get overridden by the code below.

/**
 * Implements hook_block_info().
 */
function mymodule_block_info() {
    $blocks['mymodule_block'] = array(
        'info' => t('A Super Duper Custom Block (ASDCB)'),
        'cache' => DRUPAL_CACHE_PER_ROLE,
    );
    return $blocks;
}
/**
 * Implements hook_block_info_alter().
 */
function mymodule_block_info_alter(&$blocks, $theme, $code_blocks) {
    if (!path_is_admin(current_path())) {
        if (isset($blocks['mymodule']['mymodule_block'])) {
            $blocks['mymodule']['mymodule_block']['status'] = 1;
            $blocks['mymodule']['mymodule_block']['region'] = 'Header';
            $blocks['mymodule']['mymodule_block']['weight'] = 2;
            $blocks['mymodule']['mymodule_block']['visibility'] = BLOCK_VISIBILITY_LISTED;
            $blocks['mymodule']['mymodule_block']['pages'] = '<front>';
        }
    }
}
/**
 * Implements hook_block_view().
 */
function mymodule_block_view($delta = '') {
    $block['subject'] = t('ASCDB');
    $block['content'] = _mymodule_block_content();
    return $block;
}

function _mymodule_block_content() {
    return t('Hello World');
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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