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()

Comments

saranchuk_hys created an issue. See original summary.

damienmckenna’s picture

Status: Active » Postponed (maintainer needs more info)

How did you trigger this error? Were you viewing a node page? Were you editing configuration somewhere?

kulturmensch’s picture

Get the same error when saving a content like an article (Metatag 1.19, Drupal 9.3, Php 8.0, Ubuntu 20..LTS.

The website encountered an unexpected error. Please try again later.
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).
saranchuk_hys’s picture

StatusFileSize
new441 bytes

patch to fix this

avpaderno’s picture

Version: 8.x-1.19 » 8.x-1.x-dev
kulturmensch’s picture

Thank you for the patch. It works for me - tested with standard processes with my Drupal test installation so far.

captain hindsight’s picture

I 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.

quicksketch’s picture

Status: Postponed (maintainer needs more info) » Needs work

Confirmed 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.

  • Visit /admin/config/search/metatag
  • Configure the Global metatags
  • Specify a "Mask icon (SVG)" value
  • Save
  • View any page that uses the global metatags, with full error reporting enabled, you now get the preg_match_all() error at the top of the page.

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:

  mask_icon:
    href: /themes/custom/curio/assets/images/metadata/safari-pinned-tab.svg
    color: ''

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.

eugene bocharov’s picture

I 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.

avpaderno’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 9: metatag-typeerror-preg_match_all-3258427-9.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

avpaderno’s picture

Status: Needs work » Needs review

(It seems the last patch passed all the tests, contrary to what the previous comment says.)

avpaderno’s picture

Status: Needs review » Needs work
Issue tags: +Needs issue summary update

Comment #2 asked:

How did you trigger this error? Were you viewing a node page? Were you editing configuration somewhere?

The IS should be updated with an answer to those questions.

eugene bocharov’s picture

Issue summary: View changes
Status: Needs work » Needs review
StatusFileSize
new1.19 KB
new2.19 KB

Adding patch with tests. Set Needs Review to tests be running. But it needs further work to avoid code duplication.

eugene bocharov’s picture

StatusFileSize
new3.38 KB

Oh, latest patch metatag-typeerror-preg_match_all-3258427-14.patch do not contains tests. Here the proper patch with test and fix.

phthlaap’s picture

StatusFileSize
new568.09 KB

After follow the Steps to reproduce I get the error below:
error

My environment:
- Drupal 9.4.0-dev
- Metatag 8.x-1.x
- PHP 8.1

After apply patch #16 the issue is resolved

phthlaap’s picture

Status: Needs review » Reviewed & tested by the community
eugene bocharov’s picture

Status: Reviewed & tested by the community » Needs review
Issue tags: -Needs issue summary update
StatusFileSize
new7.53 KB
new6.17 KB

This 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

phthlaap’s picture

Status: Needs review » Needs work

Cannot apply patch #19

Only local images are allowed.

avpaderno’s picture

Status: Needs work » Needs review

The tests apply the patch to test it. If the patch would not be applicable, the tests would not run.

avpaderno’s picture

Issue summary: View changes
phthlaap’s picture

Status: Needs review » Needs work

The tests fail to apply, please help to check again.

eugene bocharov’s picture

Status: Needs work » Needs review
StatusFileSize
new7.53 KB

Rerolled patch

Status: Needs review » Needs work

The last submitted patch, 24: metatag-typeerror-preg_match_all-3258427-24.patch, failed testing. View results

eugene bocharov’s picture

Status: Needs work » Needs review

Weird thing with tests. First run fails, but if rerun - passes.

mrshowerman’s picture

I 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_icon key gets added. Opening a node of that type leads to the reported error message.
Patch #24 can't be applied to 1.19.0.

eugene bocharov’s picture

#24 is for current dev branch. I think #19 should be suitable for 1.19

mrshowerman’s picture

#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.

damienmckenna’s picture

Great work, thank you all!

If there are additional scenarios that trigger similar problems, please open a new issue.

damienmckenna’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

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