6

I have a multi language site made in Drupal 7. And I want to check if I am on the front page then if true to redirect the user to a to some specific node.

I have tried different methods:

  1. to create a page front--page.tpl.php and then to check language and then redirect
  2. I have put this code in page.tpl.php:

     global $language;
    
     if ($node->nid == 1) {
         if ($language->language == "en") {
             header('Location: en/home');
         } elseif ($language->language == "fr") {
             header('Location: fr/accueil');
         }
     }
    
  3. I am using i18n module for Drupal 7 so I have tried also to use $conf['i18n_variables'] in settings.php like that:

      $conf['i18n_variables'] = array(
         'site_frontpage',
         'site_name',
      );
    

But I have not succeed to do it. Have you any suggestions ?

7 Answers 7

7

page--front.tpl.php simply allows you to create a different page template than the default page.tpl.php template. When drupal loads the homepage and there is a page--front.tpl.php it will laod that template instead.

If you want to check if you are on the homepage you can use the is_front variable. It would be best to use hook_boot in a custom module.

hook_boot(&$vars) {
  if($vars['is_front']) {
    // do your redirect
  }
}

You can use drupal_goto() to handle the redirect.

6
  • I have did a mistake, I have used front--page.tpl.php instead of page--front.tpl.php. So I have tried as you said (using page--front.tpl.php), however it was too complicated so I have abandon it.
    – radu c
    Mar 18, 2011 at 8:03
  • Finally is working. I am using your idea to put in template.php the preprocess hook.
    – radu c
    Mar 18, 2011 at 8:06
  • The hook_preprocess_page is an early process ? In the perspective to not consume much time till loading. In practice is working fast, I will test it on a remote server.
    – radu c
    Mar 18, 2011 at 8:14
  • You should use hook_boot. hook_preprocess_page are a very late process.
    – gagarine
    Jul 3, 2014 at 12:35
  • 1
    note that your default front-page in admin/config/system/site-information can't be set to the same page or else you'll get an infinite redirect loop
    – bdanin
    Sep 11, 2015 at 21:18
4

Using a custom module that implements a menu handler for "frontpage", for example, it is possible to set the Default front page to "frontpage".

The module should then implement hook_menu() and a page handler, similarly to the following code:

/**
 * Implements hook_menu().
 */
function mymodule_menu() {
  $items = array();

  $items['frontpage'] = array(
    'page callback' => 'mymodule_view',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );

  return $items;
}

/**
 * Menu callback; shows the frontpage node.
 */
function mymodule_view() {
  $langcode = $GLOBALS['language']->language;

  if ($langcode == "en") {
    drupal_goto('en/home');
  }
  elseif ($langcode == "fr") {
    drupal_goto('fr/accueil');
  }
}
1

Why not set the value of Default front page (the site_frontpage variable) in Administration » Configuration » System » Site information to node/[NID]? If you want different node for the different languages, keep site_frontpagein i18n_variables in settings.php and visit the Site Information page in each language.

2
  • I have tried this, but is not working. For you is working in drupal 7 ?
    – radu c
    Mar 17, 2011 at 14:34
  • 1
    The problem with Pierre's proposed solution is that different content is served under the same URL. As the frontpage might get cached for anonymous users, you might run into problems: #339958, Cached pages returned in wrong language when browser language used. kiamlaluno's proposed solution should work, but should be completed with a "Vary: Accept-Language" header.
    – fietserwin
    Jun 6, 2012 at 15:44
1

I created a (sandbox) module to do exactly this, Front page redirect:

Drupal allows you to define what content to serve on the home page. However, Drupal does not redirect visitors to that page but instead just serves the content of that page on the front page.
...
To alleviate these problems, this module really redirects visitors that arrive at the front page to the page that is defined as front page. It does so using a 301 (Moved Permanently) status code.

1
  • I haven't tested the multilingual aspect, but this works nicely.
    – mpdonadio
    Feb 11, 2014 at 18:05
1

googletorp's solution will not work as is.

If you try to use drupal_is_front_page() in a hook_boot you will get errors because the function libraries haven't even been loaded until after the boot.

This is why EK Kosmos is getting a blank screen (why is anybody developing with error messaging turned off?).

A solution is to use hook_boot(), as suggested, but to evaluate directly from the url if it is the front page or not.

2
0

I would go for hook_boot:

module_boot() {
  global $language;
  if (drupal_is_front_page() && $language->language == 'XX') {
    drupal_goto('node/[NID]');
  }
}

It's early in the process, so should be quicker and use less resources, than doing it in a theme function.

6
  • Just now I have tried this hook, and is not working. I have checked with debugger and is ok but just he doesn't display the page I want, it display the default home page defined in admin/config/system/site-information
    – radu c
    Mar 17, 2011 at 14:48
  • @Ek Kosmos: Hook_boot is special, in that Drupal needs to know about it early on, so it caches the list of modules implementing this hook. All you need to do, is to clear the cache.
    – googletorp
    Mar 17, 2011 at 15:32
  • Yes, I have cleared the cache, still is not working.
    – radu c
    Mar 18, 2011 at 6:21
  • @EK Kosmos: hook_boot needs to be in a custom module. Have you tried it there? I like @googletorp's method better. He's catching it sooner than template_preprocess_page. Both work but the load will be less with hook_boot.
    – Jepedo
    Mar 18, 2011 at 11:21
  • Yes, I have created my custom module where I putted this hook. I have cleared the cache, then a blank page appeared, I have looked into the source code, and there are just header and body tags (which are empty).
    – radu c
    Mar 18, 2011 at 13:17
0

For a redirection you don't need to render the page therefor you should use hook_boot instead of hook_menu. In hook_boot you don't have access to all the Drupal function so I tried as much of possible stay with native PHP function.

/**
 * hook_boot
 */
function modulename_boot() {
  // Check if it's the front page
  // using hook_boot we avoid wasting resources
  // It's to early to use drupal_is_front_page and we should avoid loading unnecessary extra resource
  if ($_GET['q'] == '') {
    // http_negotiate_language() is a better option but require  pecl_http >= 0.1.0
    // Locale::acceptFromHttp() require intl extension
    $langBrowser = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,2);

    // Get the activated languages on your drupal site
    $langAvailable = language_list();

    // set a default in case the language is not avaible
    $lang = 'en';

    if(isset($langAvailable[$langBrowser]) ){
      $lang = $langAvailable[$langBrowser]->prefix;
    }

    $http_response_code = 302;
    header('Location: ' . $lang, TRUE, $http_response_code);

    drupal_exit();
  }
}
1
  • Must use exit() and not drupal_exit() has it is not available in hook_boot() like drupal_is_front_page() ;-)
    – anou
    Apr 22, 2021 at 13: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.