Support from Acquia helps fund testing for Drupal Acquia logo

Comments

DamienMcKenna created an issue. See original summary.

DamienMcKenna’s picture

Once this is added we'll also add the x-default tag: #1969400: Add the hreflang="x-default" meta tag

DamienMcKenna’s picture

Oh, the other issue didn't actually *add* support, it just said "use this module instead." Doh.

DamienMcKenna’s picture

Status: Active » Needs review
FileSize
10.37 KB

This should cover it. It adds the hreflang="x-default" meta tag and automatically adds hreflang tags for each enabled locale. It also, by default, removes any hreflang="LANGCODE" tags that match the x-default value, per Google's recommendation. There's a test file but it needs to be done later.

DamienMcKenna’s picture

FileSize
67.78 KB

This is what it looks like:

Alternative hreflang tags

DamienMcKenna’s picture

DamienMcKenna’s picture

Going to be finishing this in time for the next release, just need to finish the i18n functionality first and then write some tests.

DamienMcKenna’s picture

FYI I wrote a patch to add a token to the Entity Translation module that works really well with this: #2603056: Provide tokens for URLs of all available translations for a given entity

DamienMcKenna’s picture

FileSize
13.95 KB

This merges in the token from the other issue, sets up defaults based upon the token, and improves the help text.

DamienMcKenna’s picture

FileSize
713 bytes
13.95 KB

A minor mistake.

The last submitted patch, 10: metatag-n2597301-10.patch, failed testing.

DamienMcKenna’s picture

FileSize
669 bytes
13.96 KB

A minor wording fix.

DamienMcKenna’s picture

Status: Needs review » Fixed

Committed.

  • DamienMcKenna committed 66ea1a2 on 7.x-1.x
    Issue #2597301 by DamienMcKenna: New submodule for handling the hreflang...
Perignon’s picture

Deja vu. I just started looking for something to provide the hreflang tag today and was hoping metatag did before I threw the code into an existing custom module on my site.

Perignon’s picture

What could cause this to not work? Something obviouse I am hoping.

I been testing this yesterday and this morning and I am not getting the metatags in the HEAD of the document of nodes.

I turn dupes on as well.

Perignon’s picture

Issue summary: View changes
FileSize
91.17 KB

This is my settings for my node. Since I do not have translations I only set the default to the source.

Perignon’s picture

So I installed on a default Drupal 7 and it works. Therefore it is something in my code base not allowing it to work. If you can throw me any ideas that would be much appreciated. Once I figure out they conflict we can document it if it is something someone else may run into.

DamienMcKenna’s picture

If you don't have translations installed then [node:source:url] won't have a value, it depends upon the $node->tnid value.

Perignon’s picture

That is rather interesting. You are right that I have the "Content Translation" module shut off. Why did it not throw an error that the tocketn [node:source:url] was not available?

Perignon’s picture

So the problem was the token being empty.

Is this worth documenting? I've also just found that even with the content translation module enabled that the token [node:source-url] is still empty. So for those of us just trying to get the default hreflang tag and have no other translations, you just need to use the node/page URL.

DamienMcKenna’s picture

Status: Fixed » Needs work

Yes, it's worth documenting, and the default token stuff should probably check to make sure the module is enabled.

Perignon’s picture

Yeah.... We got more issues. I would suggest that the default values be empty. After installing the module on my production site I found I could not publish any nodes. All threw errors saying that [node:url-en] did not exist. And what I found was the default value got put into every configuration causing everything to have a token that doesn't exist. I've had to go through all of my metatag settings and overrides and remove the language-specific value.

Installing this module on a site without localization will "freeze" all publishing of the site till you correct the default values.

So I would recommend this get removed or changed so that it tests for the existence of the language specific token first:

// Add all of the other hreflang values.
        $languages = language_list('enabled');
        if (!empty($languages[1])) {
          foreach (array_keys($languages[1]) as $langcode) {
            $config->config += array(
              'hreflang_' . $langcode => array('value' => '[node:url-' . $langcode . ']'),
            );
          }
        }
Perignon’s picture

Oddly enough, even though I had not overrode the Node metatag configuration it had the wrong token in it after setting global configuration to what I need. Maybe just a fluke.

DamienMcKenna’s picture

That's strange, because the tokens are added by the module itself.

Perignon’s picture

The [node:url-LANG]?

DamienMcKenna’s picture

Yep :)

DamienMcKenna’s picture

Status: Needs work » Needs review
FileSize
2.18 KB

How about this?

  • DamienMcKenna committed 5fa7f8b on 7.x-1.x
    Issue #2597301 by DamienMcKenna: Only use the new locale-URL tokens when...
DamienMcKenna’s picture

Status: Needs review » Fixed

Committed.

LiamPower’s picture

Status: Fixed » Needs work

When testing this I found that it's not picking up the translated nodes within metatag_hreflang_tokens as the $node->translations wasn't displaying the translations found in the node translate tab.

I found that getting the translations using translation_node_get_translations($node->nid) returned my correct translations.

The below is what worked for my translations.

/**
 * Implements hook_tokens().
 */
function metatag_hreflang_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();

  $sanitize = !empty($options['sanitize']);

  // Node tokens.
  if ($type == 'node' && !empty($data['node'])) {
    // Shortcuts.
    $node = $data['node'];
    $translations = translation_node_get_translations($node->nid);
    // Only generate tokens if there are multiple translations.
    if (isset($translations) && !empty($translations)) {
      $languages = language_list('enabled');
      if (!empty($languages[1]) && is_array($languages[1]) && count($languages[1]) > 1) {
        foreach ($tokens as $name => $original) {
          foreach ($translations as $langcode => $translation) {
            if ($name == 'url-' . $langcode) {
              $url_options = $options;
              $url_options['language'] = $languages[1][$langcode];
              $url_options['absolute'] = TRUE;
              $replacements[$original] = url('node/' . $translation->nid, $url_options);
            }
          }
        }
      }
    }
  }

  return $replacements;
}
DamienMcKenna’s picture

Were you using the core "Content translation" module or Entity Translation?

LiamPower’s picture

I didn't originally set this website up and it has both of the modules enabled but I believe the node was translated with 'Content Translation'

Within the publishing options Multilingual support the option: 'Enabled, with translation' is selected.

DamienMcKenna’s picture

Status: Needs work » Fixed

Lets move this into a new ticket: #2627598: Custom tokens for hreflang not working

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.