2

I am trying to set my hreflang links with this code:

$langcode= $GLOBALS['language']-> language;
//Get enabled languages
$langcode= array_pop(language_list('enabled'));
unset ($languages[$langcode->language]);

// Get path
$path1 = drupal_is_front_page() ? '<front>' : $_GET['q'];
//Get Urls 
$links = array();
foreach ($languages as $lang) {
  $links[$lang->language] = array(
    'href'=> $path1,
    'language'=>lang,
    'absolute'=> TRUE,
  );
}
drupal_alter('translation_link', $links, $path1);

//Set head tags
foreach($links as $link) {
  drupal_set_html_head('<link rel="alternate" hreflang="'.   $link['language']->language .'" href="'. url($link['href'], $link) .'" />');
}
//Update head
$vars['head'] = drupal_get_html_head();

with no luck.

While researching I have found this module Internationalization contributions but it doesn't do anything. Or I don't know how to use it.

  • What part of the code doesn't do what you want? You can't just dump code and say "It doesn't work". Where is this code? Have you even checked it's actually running? If what Clive says is true, this code should give a fatal error. – Letharion Aug 3 '12 at 12:42
  • I am using this code inside my function mytheme_preprocess_page in the template.php file. No fatal error at all. Where i can see at least. – paranod Aug 3 '12 at 12:50
2

You tagged your question as Drupal 7 so I'm guessing the only problem is that you're using drupal_set_html_head() which doesn't exist in Drupal 7.

It's been replaced with drupal_add_html_head(), which you can use like so:

$element = array(
  '#tag' => 'link', 
  '#attributes' => array( 
    'hreflang' => 'some_language', 
    'rel' => 'alternate',
  ),
);
drupal_add_html_head($element, 'mymodule');
  • So i have to create it as a module to be able to include it? What about the module i have already mentioned? Are you familiar with this? – paranod Aug 3 '12 at 12:51
  • It should work fine in a theme as well, the 'mymodule' bit is just a key to be able to identify the tag; 'mytheme' would work just as well, as would 'my_pet_dog' or any other string. I haven't used the contributions module before so I can't help with that I'm afraid – Clive Aug 3 '12 at 12:53
  • so is the path! Ok i see! So if i understand correctly it should be something like this? – paranod Aug 3 '12 at 13:02
  • // Get path $path1 = drupal_is_front_page() ? '<front>' : $_GET['q']; //Get Urls $links = array(); foreach ($languages as $lang) { $links[$lang->language] = array( '#tag'=> 'link', 'hreflang'=>lang, 'rel'=> 'alternate', ); } drupal_alter('translation_link', $links, $path1); //Set head tags foreach($links as $link) { drupal_set_html_head('<link rel="alternate" hreflang="'. $link['language']->language .'" href="'. url($link['href'], $link) .'" />'); } //Update head $vars['head'] = drupal_add_html_head(); – paranod Aug 3 '12 at 13:02
  • I'm not sure if you need to set $vars['head'] (definitely not if your code is in hook_preprocess_page and not hook_preprocess_html). Best thing to do would be to test it out and see if you run into any problems – Clive Aug 3 '12 at 13:11
2

Just use the mentioned module http://drupal.org/project/i18n_contrib and enable the sub-module i18n_hreflang, this will provide the hreflang in the head section of your website automatically across your translated pages. (clear cache!) Use the latest state (dev-release) to have some improvements already in. Sure I will create soon an new stable release too.

2

Another option for adding the hreflang tag is this module: Alternate hreflang

Alternate hreflang is a simple module that automatically adds these tags to your pages. It has no dependencies, but works well with Entity Translation.

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.