문제

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 ?

도움이 되었습니까?

해결책

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.

다른 팁

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');
  }
}

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.

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.

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.

There is no need to code. Go to Configuration-Site Information There, you can find 'Default front page', just insert the link of the node you want to get.

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.

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();
  }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 drupal.stackexchange
scroll top