0

I am attempting to attach a block to another block via hook_block_view_alter. The source block is a menu_block and the block to be attached is a view block, but the type of block shouldn't matter to the solution.

Here is the code used to attach the block. Assume that block_attached_block is populated with the bid of the block and at the point where I am altering the render array I have a fully rendered block.

function MODULE_block_view_alter(&$data, $block) {
  if (!empty($block->block_attached_block)) {

    // load the attached block from the database
    $attached_block = db_select('block', 'b')
      ->fields('b')
      ->condition('b.bid', $block->block_attached_block)
      ->execute()
      ->fetchObject();

    // render the block
    $block_content  = _block_render_blocks(array($attached_block));
    $build          = _block_get_renderable_array($block_content);
    $block_rendered = drupal_render($build);

    // attach the block
    $data['block_attached_block'] = array(
      '#type' => 'container',
      '#attributes' => array(
        'class' => array('block_attached_block'),
      ),
      'block' => array(
        '#markup' => $block_rendered,
      ),
    );
  }
}

If I render $data at this point I see the attached block rendered perfectly.

drupal_render($data);

However, when I load the page the attached block is missing. The documentation is pretty light.

My question is how do I get the rendered block to output on the page.

4
  • 1
    Maybe you mean $data['content']['block_attached_block'] = ...? A $block as returned from hook_block_info isn't a render array, confusingly, the render array is actually in the inner content element
    – Clive
    Dec 17 '15 at 15:59
  • Hey Clive, that does not work unfortunately. Dec 17 '15 at 16:54
  • Worth a shot, will try to take a closer look later
    – Clive
    Dec 17 '15 at 16:57
  • For menu blocks, I need to add it to $data['content']['#content']['block_attached_block']...so that's one down. Now I need to figure out view blocks, webform blocks and custom blocks. Dec 17 '15 at 17:06
0

Here is an updated function taking into account some of the various types of blocks that we can attach blocks to.

function MODULE_block_view_alter(&$data, $block) {
  if (!empty($block->block_attached_block)) {

    // load the attached block from the database
    $attached_block = db_select('block', 'b')
      ->fields('b')
      ->condition('b.bid', $block->block_attached_block)
      ->execute()
      ->fetchObject();

    // render the block
    $block_content = _block_render_blocks(array($attached_block));
    $build         = _block_get_renderable_array($block_content);

    // create attached block render array
    $block_attached_block = array(
      '#type' => 'container',
      '#attributes' => array(
        'class' => array('block_attached_block'),
      ),
      'block' => $build,
    );

    // attach the block
    switch ($block->module) {

      // menu block module blocks
      case 'menu_block':
        $data['content']['#content']['block_attached_block'] = $block_attached_block;
        break;

      // views module blocks
      case 'views':
        $data['content']['#markup'] .= drupal_render($block_attached_block);
        break;

      // block module blocks
      // webform module blocks
      case 'block':
      case 'webform':
        $data['content'] .= drupal_render($block_attached_block);
        break;

      // all other module blocks
      default:
        $data['content']['block_attached_block'] = $block_attached_block;
        break;
    }
  }
}

Thanks Clive for getting me started on the right path.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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