Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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.
Comment | File | Size | Author |
---|---|---|---|
#10 | metatag-n2051407-10.patch | 731 bytes | DamienMcKenna |
Comments
Comment #1
cha0s CreditAttribution: cha0s commentedI 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.
Comment #2
illeace CreditAttribution: illeace commentedComment #3
illeace CreditAttribution: illeace commentedI 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.
Comment #4
illeace CreditAttribution: illeace commentedNevermind, 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 linebecause it's trying to get the union of a null value and an array.Comment #5
illeace CreditAttribution: illeace commentedComment #6
DamienMcKennaComment #7
pingwin4eg2 @illeace: If all good you should have returned RTBC status.
I confirm that patch from #1 works.
Comment #8
DamienMcKennaDoes this also resolve the same problem?
Comment #9
pingwin4egOk, even better checking. Maybe then let it be
for the same reason as #1?
Comment #10
DamienMcKennaEt voila.
Comment #11
pingwin4egPerfect! :)
Comment #12
DamienMcKennaCommitted! Thanks for your help.