1

Is it possible to change my block template suggestion based on a particular context?

E.g. On certain parent landing pages (I could manually set; because there are only 3 conditions no determinate pattern), I need the block title to go inside the content <div>.

I could do a workaround and add the title in the body field. Although, I don't want to modify the block content (to add title in the body area/field) because they maybe used on other taxonomy pages; and the clients will have a hard time managing this (calls asking why is the title shown twice in the block management).

Would or could this be best in terms of most efficient solution achieved by preprocessing?

Current output on pages (Default drupal templates in psuedocode):

div.block-block-{delta}
  h2.block-title
  div.content

Desired output (on set parent landing pages)

   div.block-block-{delta}
     div.content
       h2.block-title

Desired output (on all other taxonomy pages)

div.block-block-{delta}
  h2.block-title
  div.content

Update:

These landing pages are just three nodes that the client wants. Theres not really a rhyme or reason to what determines if it is a landing page. I just need to set three node id's to trigger the block template change.

2 Answers 2

3

You mention the word 'context' in the first line of your question. Take a look at the Context module. I just came from this page

http://www.metachunk.com/blog/context-based-template-suggestions-drupal-7

Edit: elaborating .. here's the context module http://drupal.org/project/context Install it and create a context 'mycontext' for your 3 pages.

Based on this context, provide a template suggestion as mentioned in Topsitemakers' answer. In your themes template.php, it should be something like (not tested)

mytheme_preprocess_block($&vars) {
  $context = context_get();
  if(isset($context['context']['mycontext'])) {
    $vars['theme_hook_suggestions'][] = "block__mycontext";
  }
}

You can do this more generic as described in the post linked above. Now, your block will render using block--mycontext.tpl.php, if it exists, when shown in the 'mycontext' context.

$2c, *-pike

2
  • Could you please provide an example?
    – chrisjlee
    Oct 24, 2012 at 21:47
  • Not a working one, but I can try to elaborate a bit. I was looking for a similar solution, tried the Context way of things and found the module was misbehaving on my setup. I eventually decided to go with Themekey and subthemes.
    – commonpike
    Oct 27, 2012 at 11:22
1

Yes, the easiest way is to use hook_preprocess_block() function and add your templates to the list in specific contexts.

Example:

/**
 * Implements hook_preprocess_block().
 */
function THEME_preprocess_block(&$variables) {
  // Replace 1, 2, 3 with real node IDs
  if (arg(0) == 'node' && in_array(arg(1), array(1, 2, 3)) && !arg(2)) {
    $variables['theme_hook_suggestions'][] = 'block__custom_context';
  }
}
4
  • That approach seems hack-ish. Is there another way of avoiding the arg() function?
    – chrisjlee
    Oct 22, 2012 at 15:45
  • The arg() is just an example. The point of the post was how you add the template suggestion. What are exactly your contexts? Are landing pages you are mentioning views pages, nodes or something else? Oct 22, 2012 at 15:51
  • These landing pages are just three nodes that the client wants. Theres not really a rhyme or reason to what determines if it is a landing page. I just need to set three node id's to trigger the block template change.
    – chrisjlee
    Oct 22, 2012 at 15:57
  • In that case I believe this is the easiest and best way to determine if the user is on those pages. I updated my answer to check the node ID. I don't think this is hackish at all and furthermore this should be the correct way of handling exceptions of only 3 nodes that have nothing in common. If you can relate them somehow else (field value etc.) and if you will have more of them later on - then the logic should be different. Oct 22, 2012 at 16:03

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.