Using tokens in other fields (e.g. [node:metatag:description]) is broken due to languages not being accounted for when building the token result array. When the keyed array merge (+=) happens, the structure isn't aligned with the result of metatag_config_load_with_defaults(), since the actual results are keyed by language.

The attached patch looks up the language and uses it to key into the metatags array so that the merge that immediately follows is matched with the correct structure.

Here are some steps to reproduce:

  • Make sure the following modules are installed and enabled:
    • Token
    • Token Filter
    • Metatag (and dependencies)
  • Go to Configuration > Text Formats > Full HTML > Configure.
  • Under "Enabled Filters" check the "Replace tokens" option and click Save configuration.
  • Go to Structure > Basic Page > manage fields.
  • Add a new text field. Make sure you set Text processing to "Filtered text".
  • Add a new Basic Page node.
  • Enter data for the required fields.
  • In the Meta tags > Page title field enter "Token test".
  • In your custom text field, enter "[node:metatag:title]". Make sure the text format is set to "Full HTML".
  • Save your node.
  • Note: the token is replaced with the node's title, not the metatag title value.
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

cha0s’s picture

I did some more testing, and apparently when no metatags exist, the metatags array is not keyed by language, so instead of doing isset() we need to check !empty(). New patch reflects this approach.

illeace’s picture

Issue summary: View changes
illeace’s picture

Status: Needs review » Reviewed & tested by the community

I tested the patch and it fixed the issue. I found another issue when using metatag tokens in text area fields (like the node body).

Update:
The issue isn't actually related to text area fields at all. The problem is that it's easy to create an infinite loop when using metatag tokens. I'll create a new issue to explain the problem in more detail.

illeace’s picture

Status: Reviewed & tested by the community » Needs work

Nevermind, I was testing the wrong patch. The problem below is exactly the reason for the second patch.

After some further testing, I've come across a case that this patch doesn't handle. Use the same steps to reproduce from above, but this time in your custom text field enter the token "[node:metatag:keywords]". Make sure the the metatag keywords field is empty. When you save and view the node you should get the error "Fatal error: Unsupported operand types" in the metatag.tokens.inc file. This is because the $entity->metatags array exists, but is empty. This results in $metatags being null, resulting in an error in this line

$metatags += metatag_config_load_with_defaults($instance);

because it's trying to get the union of a null value and an array.

illeace’s picture

Issue summary: View changes
DamienMcKenna’s picture

pingwin4eg’s picture

Status: Needs work » Reviewed & tested by the community

2 @illeace: If all good you should have returned RTBC status.

I confirm that patch from #1 works.

DamienMcKenna’s picture

Status: Reviewed & tested by the community » Needs review
FileSize
730 bytes

Does this also resolve the same problem?

pingwin4eg’s picture

Ok, even better checking. Maybe then let it be

      if (!empty($entity->metatags[$language])) 

for the same reason as #1?

DamienMcKenna’s picture

FileSize
731 bytes

Et voila.

pingwin4eg’s picture

Status: Needs review » Reviewed & tested by the community

Perfect! :)

DamienMcKenna’s picture

Status: Reviewed & tested by the community » Fixed

Committed! Thanks for your help.

  • DamienMcKenna committed f344562 on 7.x-1.x authored by cha0s
    Issue #2051407 by cha0s, DamienMcKenna: Language support for token...

Status: Fixed » Closed (fixed)

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