Implementing dynamic menu items with Menu Token for Drupal

18 March 2013

 
Dubai: We need to show different links to our country-specific content views, based on the context of a country that the user is navigating. So imagine that our site has a navigation tree like:
 

  1. Home - /home/1 (México Home), /home/2 (Argentina Home)
  2. News - /news/1 (México News), /news/2 (Argentina News)
  3. Articles - /articles (All Countries news)

 
With the custom menus of Drupal we can only associate a single link with every menu item, so it is not possible to create a Home entry point that dynamically changes the ID parameter of the country. With Menu Tokens, however, we can link Home in our example with /news/[node:nid], where [node:nid] is the dynamic part that will be replaced by the ID of a node.
 
Don’t rejoice yet, though! Menu Tokens don’t offer, out of the box, a replacement that will work for us. We only have: random node (pick randomly a node), user specific (static node configured), and Node from context (the viewed node). As we want to display our menu in a sidebar of a page that is actually a view, we don’t have the required node information in context and none of the available options will work for us.
 
Fortunately, we can implement our own Menu Token plug-in that has its own logic to provide the node that will be used for the replacement. So in our case we allow the user to choose a Country when accessing our multi-country site, and we store that selected country in a $_SESSION variable. We implement a Menu Token plug-in, as so:
 
What we are doing here is providing, as a Menu Token, the Country ID that the user selected at the start. We get the value from the session, load the node, and return the replacements in our menus! Next, we are going to delve a little deeper into how we can better keep the context of the country and what some of the complexities are around that.
 
Preserving the context of the country the user is browsing. When a user comes to your site you need a way to remember which country the user is interested in, so as to be able to build the dynamic menus and other country-specific items on the page. This poses some challenges. While the user could come directly to the home page allowing you to display a select country page, as discussed above, a lot of traffic is actually expected to arrive from search engines and social networks.
 
For example, if a user is arriving from mysite . com/news/mexico, it would be counter-intuitive to take them to a Country select page. Instead, we need to set and preserve the context to México. Alternatively, we may want to auto-detect the country based on the IP location from the user. This could be done via Smart IP.
 
In order to flexibly deal with a number of different heuristics, we use Rules and the _multi_country_set_selected_country function (shown below) is called from Rules actions. This allows us via the UI to configure different options on how to set the country context.

Drupal Web Development Dubai
 

News Archive

  • WordPress Development in Dubai
  • Drupal Development in Dubai
  • Joomla Development in Dubai
  • eCommerce Magento Dubai
  • Web Development Dubai
  • Web Design Dubai
  • jQuery development Dubai
  • Zend framework development Dubai
  • Airline IBE GDS Integration Navitaire Dubai
  • Airline IBE GDS Integration Aamadeus Dubai