Skip to main content
2 of 5
added 13 characters in body
leymannx
  • 18.8k
  • 6
  • 72
  • 126

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 registred 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.

/**
 * 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 (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>';
    }
}
leymannx
  • 18.8k
  • 6
  • 72
  • 126