You are here

metatag_hreflang.module in Metatag 7

Same filename and directory in other branches
  1. 8 metatag_hreflang/metatag_hreflang.module

Primary hook implementations for Metatag:hreflang.

File

metatag_hreflang/metatag_hreflang.module
View source
<?php

/**
 * @file
 * Primary hook implementations for Metatag:hreflang.
 */

// @todo Clear caches for all versions of an entity when a translation is edited
// so that the hreflang meta tags update appropriately.

/**
 * Implements hook_ctools_plugin_api().
 */
function metatag_hreflang_ctools_plugin_api($owner, $api) {
  if ($owner == 'metatag' && $api == 'metatag') {
    return array(
      'version' => 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:
 * <link rel="[name]" hreflang="[langcode]" href="[value]" />
 */
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.'),
    '#default_value' => variable_get('metatag_hreflang_allow_dupe', FALSE),
  );
}

/**
 * Implements hook_metatag_metatags_view_alter().
 */
function metatag_hreflang_metatag_metatags_view_alter(&$output, $instance, $options) {

  // 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.
  // 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;
          }
        }
      }
    }
  }
}