0

I've been creating my own module that consist an array of blocks. I plan to print them using context as oppose programatically as I already have the module on use (no need to reinvent the wheel). However, whenever I use context to display a block on a region, it always display it on the highest part of html body tag. e.g.

<body>
  block content rendered here...

as oppose to my custom region,

<div class="l-info-bar">
  I want my block content rendered here...

I cleared the cache several times. Made sure that the context is set to display the block on info bar region.

Here's my code for reference.

    function custom_module_block_info() {
      $blocks = array();
      $blocks['my_block_top_navigation'] = array(
        'info' => t('Top Bar Navigation Menu'),
        'cache' => DRUPAL_CACHE_GLOBAL,
      );
      $blocks['my_block_avatar'] = array(
        'info' => t('Avatar'),
        'cache' => DRUPAL_CACHE_GLOBAL,
      );  

      return $blocks;
    }
    /**
    * Implements hook_block_view().
    */
    function custom_module_block_view($delta = '') {
      $block = array();
      switch ($delta) {
        case 'my_block_top_navigation':
          $block['subject'] = '';
          $block['content'] = _custom_module_block_top_navigation_content();
          break;
        case 'my_block_avatar':
          $block['subject'] = '';
          $block['content'] = _custom_module_block_avatar_content();
          break;     
      }
      return $block;
    }

function _custom_module_block_top_navigation_content() {
    lots of code here...
}

function _custom_module_block_avatar_content() {


   lots of code here...
}

I'm guessing you have to prep your block so context can actually place it to certain region?

Update1: I forgot to mentioned that normal blocks works on context and normal block display (only my custom block doesn't work on normal way and context—which is why I though problem lies in my coding—not context module). I also tried printing the block through code and works perfectly but I don't want extra code I don't need because I have context on use.

Update 2: I also replaced the block content with print t('TEST') and still printing above body through normal means and context. Works well printing using code.

2 Answers 2

0

Firstly, You can try putting a regular block at a regular region created by drupal core in context. That should work.

Then compare the different results you got for your custom block and custom region. Try putting your custom block at regular region, or putting regular block at your custom region.

Also, You may need to double check the 'custom region' you mentioned at question does exist at page.tpl.php. I.e the code does print the 'custom region' in page template, like

<?php print render($page['your_custom_reagion']); ?>
2
  • Yes. I created the region myself and it is in theme.info file and printed on the page.tpl. I have checked the following: made sure normal blocks like search block can be displayed on the custom region through the normal block display and context. I also tried printing it through code and it's printing in the right region—but I want to shy away from code this time because context is already in use—no need to bloat extra code.
    – Ian
    Sep 14, 2014 at 13:09
  • thanks for your answer as it help me solved my problem through testing as you suggested!
    – Ian
    Sep 14, 2014 at 13:38
0

Ok it seems the problem is that when creating a custom block through code, YOU MUST "RETURN" the variable, not just print or echo them—or else it won't print on the right region.

e.g.

function _custom_module_block_top_navigation_content() {

$test1 = t('test1');

return $test1;

}

NOT

function _custom_nixket_block_top_navigation_content() {

print t('test1');

}

Your Answer

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

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