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.
Files: 
CommentFileSizeAuthor
#1 entity-token-language-handling-2051407-1.patch690 bytescha0s
PASSED: [[SimpleTest]]: [MySQL] 73 pass(es).
[ View ]
entity-token-language-handling.patch689 bytescha0s
PASSED: [[SimpleTest]]: [MySQL] 73 pass(es).
[ View ]

Comments

StatusFileSize
new690 bytes
PASSED: [[SimpleTest]]: [MySQL] 73 pass(es).
[ View ]

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.

Issue summary:View changes

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.

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

<?php
$metatags
+= metatag_config_load_with_defaults($instance);
?>

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

Issue summary:View changes