3

I am on Drupal 7. My website will have a country select form on the first page where users will pick one country from a list. All these countries speak the same language (Spanish), so they are basically under the same language pack. My idea is to create a Vocabulary with all the countries I need. Then, in order to guarantee consistency accross pages (allowing me to filter content when necessary), I want to append a parameter to the URL like this:

path/to/my/page?country=ur

I want this parameter to be added to all the links on the menu in my website. Drupal will read the parameter currently in the URL and will append that same parameter to all URLs on the menu.

My idea is to use Rules to catch the current country code and append that same parameter to all links on the menu, both on top of the page AND on the footer (yes, there are two menu bars). What would be the code to add those parameters to each menu link? Do I have to run the code twice in order to change URLs from both top and bottom menus, or is it possible to inject the querystring into a single resource that will be pulled to build both menus? For the menus, I am using Menu Block module.

If you have a better idea to tackle this, please, suggest. Thank you!

2 Answers 2

3

The best hook for this is hook_url_outbound_alter. This affects links generated by url(), which is used by most/all code generating urls and links in Drupal.

function mymodule_url_outbound_alter(&$path, &$options, $original_path) {
  // If the param of interest is present and the url being generated is internal.
  if (!empty($_GET['country']) && !$options['external']) {
    $options['query']['country'] = $_GET['country'];
  }
}
1

I have used this idea to maintain COUNTRY CODE without URL parameter. These are the procedure to implement it and use in _init function

  1. Based on front page country selection pass one URL parameter like ?country=ur to set COUNTRY CODE IN SESSION.
  2. Use the same SESSION value until changed from user(through front page or direct URL parameter).
  3. If the user is directly typing the URL (no SESSION value and no URL parameter) better to set default country value otherwise redirect to front page to country selection.
2
  • Hi, is it the hook_init function? Have you used Rules, or did you write your own custom module? I was also thinking of using sessions, but I am not sure what would be the best to deal with search engines and avoid duplicated content. Thank you. Commented Oct 25, 2012 at 13:31
  • 1
    Yes, I have used custom module for this. Here we are not using query string in each page, so there is no duplication in search engine. Commented Oct 29, 2012 at 10:33

Your Answer

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

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