2

I am trying to figure out how to change the content of a region/block depending on the node being viewed.

Lets say I was creating a website for a grocery store. The home page will have several links for the various departments ... Meat, Seafood, Dairy, etc. When the user clicks on one of these departments they will go to a node where there is a menu that lists all the items in that department ...

  • Meat => Steak, Hamburger, etc.
  • Dairy => Milk, Eggs, Cheese, etc.
  • Seafood => Shrimp, Tuna, Salmon, etc.

What I am trying to figure out is how to display the correct item menu for the selected department. How to I display the menu of all the dairy products when the user selected the "Dairy" node?

Ideally I would like to accomplish this without hard coding changes into the template as I would like the site owner to be able change things themselves.

Thanks for any suggestions!

3

An easy way is to:

  1. Make a site section taxonomy, and add as a field to your nodes (eg, field_site_section). Probably want to make it a required field that only allows one value.

  2. Set pathauto settings for the content types to include the term in the URL, eg [node:field_site_section]/[node:title]

  3. There should be a block for each menu you have defined. Set the visibility to only appear on paths matching a wildcard based on the paths you defined above, eg section/*.

If you need a landing page for the section, just make a node, assign the site section, and set the alias manually to just be the section name. Then, add in this to the list of path for the menu block to appear on.

One more thing that can help with this approach is to add an entityref field to the site section taxonomy. Then for each term, set the field to the root node of the section. This can come in handy for some uses.

  • That makes a lot of sense. Perfect since pathauto and taxonomy are already being used. – webworm Nov 8 '12 at 13:36
  • This is a great simple way to do it. The only thing is that your sections won't actually be a node. Therefore Meat, Dairy, Seafood won't be able to have their own body text and fields or am I missing something? – James Nov 8 '12 at 22:08
  • 2
    @James, in that case, I make a node for the section itself and use a manual alias that is just the section name. You then add that path to block config. In some cases, you need to have a few simple hooks to get the active trail and breadcrumb to work out the way you need. But, that really depends on how you built out the rest of your site. – mpdonadio Nov 8 '12 at 22:13
  • @MPD, That makes perfect sense thankyou very much. – James Nov 8 '12 at 22:27
  • Just wanted to report back that this solution worked perfectly for me. No need to add custom PHP to the theme as everything was managed through the Drupal admin screens including block visibility. I just needed to add Pathauto and Token modules beyond standard Drupal 7 install. – webworm Nov 21 '12 at 3:46
3

Try the context module.

Context allows you to manage contextual conditions and reactions for different portions of your site. You can think of each context as representing a "section" of your site. For each context, you can choose the conditions that trigger this context to be active and choose different aspects of Drupal that should react to this active context.

Think of conditions as a set of rules that are checked during page load to see what context is active. Any reactions that are associated with active contexts are then fired.

Refer the module page for documentation links.

0

You could use entity references http://drupal.org/project/entityreference on your nodes (make the name 'Parent Node' and machine name 'field_parent_node' to follow my full example). This will allow you to assign the nodes a parent node. Once done you can use views with a contextual filter

Contextual Filter Setup: Content: Parent Node, WHEN THE FILTER VALUE IS NOT AVAILABLE - Provide default value - Content ID from URL

This will then allow you to display a block of titles to make a menu or even display some teasers of child nodes for the parent.

It is also possible (although a little more complicated) to create a view of parents-children i.e. on the eggs page you could display milk, eggs, cheese etc. to do this create a view as before but the contextual filter needs some php.

Contextual Filter Setup: Content: Parent Node, WHEN THE FILTER VALUE IS NOT AVAILABLE - Provide default value - PHP Code

if ($node = menu_get_object()) {
  $query = &$view->build_info['query'];
  $parent_node = field_get_items('node', $node, 'field_parent_node');
  return($parent_node[0]['target_id']);
}

If you also want to exclude the current node from the view then you can add another contextual filter to this view:

Contextual Filter Setup: Content:nid, WHEN THE FILTER VALUE IS NOT AVAILABLE - Provide default value - Content ID from URL. Then open the more tab at the bottom and check exclude.

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.