1); } } /** * Implements hook_theme(). */ function metatag_hreflang_theme() { $info['metatag_link_hreflang'] = array( 'render element' => 'element', ); return $info; } /** * Theme callback for a rel link tag with the hreflang. * * The format is: * */ function theme_metatag_link_hreflang($variables) { $element = &$variables['element']; $element['#name'] = 'alternate'; $args = array( '#name' => 'rel', '#hreflang' => 'hreflang', '#value' => 'href', ); element_set_attributes($element, $args); unset($element['#value']); return theme('html_tag', $variables); } /** * Implements hook_form_FORM_ID_alter for metatag_admin_settings_form(). */ function metatag_hreflang_form_metatag_admin_settings_form_alter(&$form, &$form_state, $form_id) { $form['advanced']['metatag_hreflang_allow_dupe'] = array( '#type' => 'checkbox', '#title' => t('Allow hreflang tag that matches the x-default tag'), '#description' => t('It is recommended to not have hreflang="x-default" and hreflang="SOMELANGCODE" meta tags pointing at the same URL. By default if there is a hreflang="SOMELANGCODE" meta tag with the same URL as the hreflang="x-default" meta tag then the hreflang="SOMELANGCODE" tag will be removed. Checking '), '#default_value' => variable_get('metatag_hreflang_allow_dupe', FALSE), ); } /** * Implements hook_metatag_metatags_view_alter(). * * Remove any hreflang="LANGCODE" values that match hreflang="x-default". Using * this hook instead of hook_html_head_alter() as it gets closer to Metatag's * data structures, and the results are cached so this won't be executed on * every page request. */ function metatag_hreflang_metatag_metatags_view_alter(&$output, $instance, $options) { // This behaviour may be disabled from the Metatag settings page. if (!variable_get('metatag_hreflang_allow_dupe', FALSE)) { if (!empty($output['hreflang_xdefault'])) { $default = $output['hreflang_xdefault']['#attached']['drupal_add_html_head'][0][0]['#value']; foreach ($output as $tag_name => &$tag) { // Skip the x-default tag. if ($tag_name == 'hreflang_xdefault') { continue; } if (strpos($tag_name, 'hreflang_') === 0) { if ($tag['#attached']['drupal_add_html_head'][0][0]['#value'] == $default) { $tag['#attached']['drupal_add_html_head'][0][0]['#access'] = FALSE; } } } } } }