I am working on a multilingual site. I have to add HREFLANG attribute so that search engines like Google provide the proper response according to the language of the user and the region.

To implement this, I am using Alternate HREFLANG module which basically add HREFLANG attribute like this :

for all the three languages.

But I want to add language as well as region like this:

In the description of Alternate HREFLANG module description, they mentioned that if you want to alter this attribute, you can use hook_language_switch_links_alter() and hook_html_head_alter() these hooks.

But when I dsm these, I didn't get the HREFLANG attribute.

Can someone help me to achieve this?

I cant see what is in your "...which basically add HREFLANG attribute like this" and "But I want to add language as well as region like this."

I bet you want to replace, let's say, "en" to "en-GB" right? If so, you need to alter Drupal language ISO. Current is ISO 639, language+region is ISO 3166-1 alpha 2.

BTW for your question, you need something like this to locate the hreflang:

function YOUR_THEME_OR_MODULE_NAME_html_head_alter(&$head_elements) { drupal_set_message('<pre>' . print_r($head_elements,1) . '</pre>');
}


Clear cache and reload the page.

UPDATE

An easier way is by adding a custom language. E.g. visit admin/config/regional/language/add

Expand Custom language fieldset.
Enter your desired ISO 3166-1 alpha 2 language code to language code & path prefix language code.
Fill the rest (language name in English, native language name & Direction) as usual.

• Hi @ItangSanjana, How can I switch the language ISO 639 to ISO 3166-1 alpha 2? Can you please assist me? – Pankaj Sachdeva Jun 1 '16 at 7:07
• Updated. See easier solution above. – Itang Sanjana Jun 1 '16 at 17:53