0

I have sidebar block which has id "block-block-11--2". After including this statement in the node template

<div class="top-menu wrapper">
 <div class='search-wrapper'>";
 $search_block  = module_invoke("views","block_view","-exp-category_wise_search-page");
 print render($search_block['content']);
 </div>
</div>

all css of tat id is messed up, because id of that block has been changed to block-block-11. If I comment that code in node template the previous block id comes properly and all css works fine with that.Those two are entirely different blocks.

NOTE I am not trying to place same blocks at two place.

What is the issue over here? Thanks in advance.

2

The issue is you're not using things correctly. You should never call blocks directly from templates. Use regions in templates and add blocks to them from GUI, or you can expect IDs to get messed up. Block IDs are based on the order blocks are prepared for display (see implementation of drupal_html_id function), and each time you mess up with the block display mechanism, and go around it, their numbers may change.

If you call block directly because you need to place a block in two places, see answers to Place a block in multiple regions question. Or, if it's a Views block, simply clone block display.

To reliably style a block, you can use Block Class module to assign a class that will not change the way ID does. Alternatively, you can use answers to How to add id to a block to manually override IDs - just be careful to keep them unique if you want JavaScript to continue to work.


Note that not only IDs will get messed up when you call block directly, but also things like permissions and cache settings may get compromised.

  • 2
    I think the OP is not asking idea to place blocks in two places. He's looking for the working solution to prevent the id getting changed everytime when new block is invoked in template – pown Nov 6 '13 at 8:55
  • Those two are entirely different blocks and what ever i am trying to do in template using module_invoke is causing that issue of messing up with already available block-id. I dont know what can be the issue. – Ram Nov 6 '13 at 9:03
  • @pown but invoking blocks in templates is the core of the problem, as it messes up the order blocks are created and enumerated. The solution is simply to do things the way they was designed to be done. Answer updated. Also, have you read the last paragraph? – Mołot Nov 6 '13 at 9:06
  • I meant last paragraph before footnote, of course. – Mołot Nov 6 '13 at 9:17

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.