Tev
Tev

Reputation: 690

Programmatically assign a View to a Region in Drupal 7

During the install of a module, I need to assign a View to a Region, and unassign a Block from that region. It's something that would take 3 seconds in the UI, but this has to be done programmatically.

The view is called 'legal-footer' and it needs to be assigned to the region 'footer'. Similarly, I have a block called 'footer-logos' that's currently in the 'footer' region but needs to be removed.

I think I want hook_block_info_alter, but I'm not sure if it works on a View, and there's a note in the API docs that it can't be used to unassign a block...

I'm new to Drupal, and I can whatever I want in the UI pretty easily, but I'm having a hard time with the API.

Upvotes: 0

Views: 1655

Answers (1)

maged adel
maged adel

Reputation: 780

to assign block to a region use 'region' key in the array returned info that contain the name of the region that this block should be assigned to

E.g

function module_block_info() {
    $blocks = array();
    $blocks[0] = array(
        'info' => t('Block Title'),
        'region' => 'name-of-the-region', // here is the name of the region
        'status' => 1, // 1 if you want the block to be enabled by default
    );
    return $blocks;
}

and you can Disable exist block by using such query

db_update('block')->fields(array('region' => '', 'status' => 0))->condition('bid', $block_id)->execute();

replace $block_id with the id of the block that you want to disable

UPDATE: you can use hook_block_info_alter to disable exist block

function hook_block_info_alter(&$blocks, $theme, $code_blocks) {
  // Disable the login block.
  $blocks['defining_module']['delta']['status'] = 0;
}

good luck

Upvotes: 1

Related Questions