How to disable and enable Drupal 7 blocks programmatically

Sometimes you need to enable and place a Drupal 7 block in a seen region like header, content, footer, etc., and other times you need to disable it programmatically in a custom module. So to do it you just need to create two functions:

function _start_refreshing() {

db_update('block')
->fields(array(
'status' => 1,
'weight' => -10,
'region' => 'footer',
))
->condition('module', 'mymodule')
->condition('delta', 'mymodule_delta')
->execute();

}

and

function _stop_refreshing() {

db_update('block')
->fields(array(
'status' => 0,
'weight' => -10,
'region' => 'disabled',
))
->condition('module', 'mymodule')
->condition('delta', 'mymodule_delta')
->execute();

}

and call one of the functions as needed:

_start_refreshing();

Tags:

Add new comment

Filtered HTML

  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <pre>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.