Steps to reproduce
- Open for editing any node with metatags field
- Set 'Mask icon (SVG)' metatag field to any appropriate value, for example
icon.svg - Fill any other metatag, for example 'Description' with token
[node:field_metatags:mask_icon] - Save and try to open the node page
Message TypeError: preg_match_all(): Argument #2 ($subject) must be of type string, array given in preg_match_all() (line 240 of /core/lib/Drupal/Core/Utility/Token.php)
#0 /core/lib/Drupal/Core/Utility/Token.php(240): preg_match_all()
#1 /core/lib/Drupal/Core/Utility/Token.php(186): Drupal\Core\Utility\Token->scan()
#2 /modules/contrib/metatag/src/MetatagToken.php(66): Drupal\Core\Utility\Token->replace()
#3 /modules/contrib/metatag/src/MetatagManager.php(669): Drupal\metatag\MetatagToken->replace()
#4 /modules/contrib/metatag/metatag.tokens.inc(201): Drupal\metatag\MetatagManager->generateTokenValues()
#5 [internal function]: metatag_tokens()
#6 /core/lib/Drupal/Core/Extension/ModuleHandler.php(403): call_user_func_array()
#7 /core/lib/Drupal/Core/Utility/Token.php(304): Drupal\Core\Extension\ModuleHandler->invokeAll()
#8 /modules/contrib/metatag/metatag.tokens.inc(139): Drupal\Core\Utility\Token->generate()
#9 [internal function]: metatag_tokens()
| Comment | File | Size | Author |
|---|---|---|---|
| #24 | metatag-typeerror-preg_match_all-3258427-24.patch | 7.53 KB | eugene bocharov |
| #17 | cd538d85ba4e76102f5f.jpg | 568.09 KB | phthlaap |
| #14 | metatag-typeerror-preg_match_all-3258427-14-test-only.patch | 1.19 KB | eugene bocharov |
| #4 | 3258427-TypeError-preg_match_all.patch | 441 bytes | saranchuk_hys |
Comments
Comment #2
damienmckennaHow did you trigger this error? Were you viewing a node page? Were you editing configuration somewhere?
Comment #3
kulturmensch commentedGet the same error when saving a content like an article (Metatag 1.19, Drupal 9.3, Php 8.0, Ubuntu 20..LTS.
Comment #4
saranchuk_hys commentedpatch to fix this
Comment #5
avpadernoComment #6
kulturmensch commentedThank you for the patch. It works for me - tested with standard processes with my Drupal test installation so far.
Comment #7
captain hindsight commentedI receive the same warning on drupal 9.3.5 and metatag 1.19.0. The metatag "mask_icon" seems to trigger it in my case.
Comment #8
quicksketchConfirmed that I'm seeing this issue crop up specifically after updating to Drupal 9.3.5 and Metatag 1.19.0. Like @captain_hindsight, I also see this issue with the "mask_icon" metatag.
In the metatag.metatag_defaults.global.yml, the mask icon seems to not use the same format as all other tags. Instead of a string value, it's a multivalue array:
And then when the token is getting replaced, the array of both href and color is getting passed in, rather than just the href string.
That said, I don't think the current patch is the right fix. We should be fixing the data that gets passed in, not just skipping the metatag output.
Comment #9
eugene bocharov commentedI found that \Drupal\metatag\MetatagManager::generateRawElements already use some logic for dealing with array values. Why don't we use similar in \Drupal\metatag\MetatagManager::generateTokenValues
The patch with draft changes. Need to look closer. Probably this logic should also be obtained to separate method to not repeat it twice.
Comment #10
avpadernoComment #12
avpaderno(It seems the last patch passed all the tests, contrary to what the previous comment says.)
Comment #13
avpadernoComment #2 asked:
The IS should be updated with an answer to those questions.
Comment #14
eugene bocharov commentedAdding patch with tests. Set Needs Review to tests be running. But it needs further work to avoid code duplication.
Comment #16
eugene bocharov commentedOh, latest patch metatag-typeerror-preg_match_all-3258427-14.patch do not contains tests. Here the proper patch with test and fix.
Comment #17
phthlaap commentedAfter follow the Steps to reproduce I get the error below:

My environment:
- Drupal 9.4.0-dev
- Metatag 8.x-1.x
- PHP 8.1
After apply patch #16 the issue is resolved
Comment #18
phthlaap commentedComment #19
eugene bocharov commentedThis is the patch with same logic as #16, but the repeatitive code was extracted to the protected method processTagValue().
And there are a couple of typos fixes in the comments in MetatagManager.php
Comment #20
phthlaap commentedCannot apply patch #19
Comment #21
avpadernoThe tests apply the patch to test it. If the patch would not be applicable, the tests would not run.
Comment #22
avpadernoComment #23
phthlaap commentedThe tests fail to apply, please help to check again.
Comment #24
eugene bocharov commentedRerolled patch
Comment #26
eugene bocharov commentedWeird thing with tests. First run fails, but if rerun - passes.
Comment #27
mrshowermanI have the same issue with Metatag 1.19.0. When adding a new metatag setting for a node type of your choice, an empty
mask_iconkey gets added. Opening a node of that type leads to the reported error message.Patch #24 can't be applied to 1.19.0.
Comment #28
eugene bocharov commented#24 is for current dev branch. I think #19 should be suitable for 1.19
Comment #29
mrshowerman#19 didn't apply against 1.19.0 either.
Here's a slightly adapted version of #19 which does apply, but unfortunately doesn't fix the issue – the empty mask icon entry still gets added and crashes the node's detail page.
Patch added just for the record, thus hiding it.
Comment #31
damienmckennaGreat work, thank you all!
If there are additional scenarios that trigger similar problems, please open a new issue.
Comment #32
damienmckenna