Schema Metatag depends on Metatag module to do token replacements, but one kind of replacement that is a problem is managing content which may contain commas where multiple value fields are delimited with commas, because we have to explode on the delimiter to convert those lists back into arrays.

Token module provides tokens for multiple field values that join multiple values with commas, but the code will use a different delimiter if passed as a 'join' option. Using a custom delimiter would fix most of the problems we're having in Schema Metatag.

Metatag module calls token replace with a hard-coded array of options that contains only a language option. As currently configured, there is no way to pass any additional options to the Token service. I tried to find a way to get this working without any patches, but I think a small patch is required to allow an alter hook right before the tag is replaced.

Patch coming.

CommentFileSizeAuthor
#90 metatag-n3067803-89.patch19.63 KBbasvredeling
#88 interdiff.txt6.32 KBbasvredeling
#88 metatag-n3067803-88.patch19.72 KBbasvredeling
#82 metatag-n3067803-82.patch19.5 KBdamienmckenna
#77 metatag-3067803-77.patch19.53 KBbasvredeling
#76 interdiff.txt7.77 KBbasvredeling
#76 metatag-3067803-76.patch18.56 KBbasvredeling
#68 metatag-4-errors.png314.44 KBthejimbirch
#68 metatag-3-tags.png47.2 KBthejimbirch
#68 metatag-2-tag.png105.25 KBthejimbirch
#68 metatag-1-configure.png54.09 KBthejimbirch
#66 metatag-n3067803-66.patch19.69 KBdamienmckenna
#66 metatag-n3067803-66.interdiff.txt1.15 KBdamienmckenna
#64 metatag-n3067803-64.patch19.74 KBdamienmckenna
#64 metatag-n3067803-64.interdiff.txt4.98 KBdamienmckenna
#63 metatag-n3067803-63.patch19.75 KBdamienmckenna
#63 metatag-n3067803-63.interdiff.txt2.37 KBdamienmckenna
#62 metatag-n3067803-62.patch19.86 KBdamienmckenna
#62 metatag-n3067803-62.interdiff.txt1.28 KBdamienmckenna
#60 metatag-n3067803-60.patch18.79 KBdamienmckenna
#58 metatag-n3067803-58.patch1.57 KBdamienmckenna
#56 metatag-n3067803-56.patch18.67 KBdamienmckenna
#52 metatag-3067803-52.patch2.19 KBbrunodbo
#35 reroll_diff_3067803-34-3067803-35.txt7.23 KBosopolar
#35 metatag-n3067803-35.patch18.71 KBosopolar
#34 metatag-n3067803-34.patch18.66 KBkarens
#33 metatag-n3067803-33.patch18.64 KBkarens
#31 metatag-n3067803-30.patch7.5 KBrobertshell22
#29 metatag-n3067803-29.patch7.49 KBaludescher
#28 metatag-n3067803-28.patch7.48 KBaludescher
#27 metatag-n3067803-27.patch7.5 KBaludescher
#25 metatag-n3067803-25.interdiff.txt1.57 KBdamienmckenna
#25 metatag-n3067803-25.patch6.25 KBdamienmckenna
#21 metatag-3067803-21.patch1.87 KBpeter3bweb
#18 metatag-n3067803-17.patch4.69 KBdamienmckenna
#18 metatag-n3067803-17.interdiff.txt1.22 KBdamienmckenna
#16 metatag-n3067803-16.patch4.98 KBdamienmckenna
#11 3067803-token-alter-hook.patch4.99 KBkarens
#9 3067803-token-alter-hook.patch4.32 KBkarens
#6 3067803-token-alter-hook.patch4.28 KBkarens
#2 3067803-token-alter-hook.patch3.14 KBkarens

Issue fork metatag-3067803

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

KarenS created an issue. See original summary.

karens’s picture

Status: Active » Needs review
StatusFileSize
new3.14 KB
karens’s picture

Issue summary: View changes
damienmckenna’s picture

Seeing as use of comma as the delimiter causes other problems, e.g. #2761909: og:image does not parse url from image field properly when there is a comma in the alt field, maybe we should deprecate use of commas an change to something else?

karens’s picture

We could change the delimiter for all multiple values, and that solves the problem that Schema Metatag has. I think other tags still have a problem because in some cases you want a comma separator in the final value for something like a list of tags for keywords. So there might need to be an optional final step of replacing the delimiter with commas in the final output if they haven't been replaced by some other module. As long as there's an opportunity for each module to decide what to do with the delimiter first that could work.

karens’s picture

StatusFileSize
new4.28 KB

Maybe something like this then? Not really tested, just to show the idea.

Status: Needs review » Needs work

The last submitted patch, 6: 3067803-token-alter-hook.patch, failed testing. View results

karens’s picture

Title: Allow Schema.org Metatag to alter token options » Use custom delimiter instead of commas for multiple values

Changing title

karens’s picture

Status: Needs work » Needs review
StatusFileSize
new4.32 KB

Status: Needs review » Needs work

The last submitted patch, 9: 3067803-token-alter-hook.patch, failed testing. View results

karens’s picture

Status: Needs work » Needs review
StatusFileSize
new4.99 KB
karens’s picture

OK tests pass and I think this is actually working. A remaining step would be to update existing configuration to use the new delimiter instead of commas. That is tricky though because we can't be sure which commas in the existing config are separators and which are actual commas. That confusion is the reason we need something like this patch.

I think config will have to be updated manually. So perhaps the way to go about this would be to make this a new branch (so people know there is a major change here), with instructions to check your configuration afterward.

Alternatively, still as a new backward-breaking branch, we could replace all commas in the current config with the new delimiter, since that will be the right result most of the time, and warn users to double-check any config that might have contained actual commas. Most config should be tokens, and replacement of commas between tokens would be safe, so a third solution would be to only replace patterns like "],[", commas between tokens.

karens’s picture

For reference, if this got in, the corresponding patch for Schema Metatag would be https://www.drupal.org/files/issues/2019-07-15/2976935-use-metatag-delim.... Working locally with both patches, they seem to work well to differentiate between delimiters and commas.

earthday47’s picture

I can verify the patch in #11 is working for me.

jeroent’s picture

Status: Needs review » Reviewed & tested by the community

Patch in #11 is also working fine for me.

damienmckenna’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new4.98 KB

Rerolled.

Status: Needs review » Needs work

The last submitted patch, 16: metatag-n3067803-16.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

damienmckenna’s picture

Status: Needs work » Needs review
Issue tags: +Needs tests
StatusFileSize
new1.22 KB
new4.69 KB

This is really good, thank you.

I see one issue with this: backwards compatibility for all sites that have multiple values entered. While the existing tests work, we need additional test coverage to ensure backwards compatibility. I'd also like to see this made configurable, maybe a global configuration to choose between the two options, and for new installs it would use the new value but for older sites it would use commas.

This makes a minor change to verify $item['#attributes']['content'] exists before using it.

damienmckenna’s picture

Status: Needs review » Needs work

Back to 'needs work'.

peter3bweb’s picture

I have this issue on Drupal 7. We have images that have a comma in the URL and the OpenGraph image link is trucated at the comma. Does anybody know of a fix for this?

peter3bweb’s picture

Version: 8.x-1.x-dev » 7.x-1.x-dev
Assigned: Unassigned » peter3bweb
Category: Feature request » Task
Status: Needs work » Patch (to be ported)
StatusFileSize
new1.87 KB

I created a patch for this issue with the Drupal 7 branch (this is my first time doing this)

peter3bweb’s picture

Didn't mean to hide Damien's files

damienmckenna’s picture

Version: 7.x-1.x-dev » 8.x-1.x-dev
Assigned: peter3bweb » Unassigned
Category: Task » Feature request
Status: Patch (to be ported) » Needs review

Thank you for the backport to D7.

peter3bweb’s picture

Thanks for reviewing DamienMcKenna

damienmckenna’s picture

StatusFileSize
new6.25 KB
new1.57 KB

WIP on adding a setting form to control the delimiter.

Status: Needs review » Needs work

The last submitted patch, 25: metatag-n3067803-25.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

aludescher’s picture

StatusFileSize
new7.5 KB
aludescher’s picture

StatusFileSize
new7.48 KB
aludescher’s picture

StatusFileSize
new7.49 KB
jeroent’s picture

Status: Needs work » Needs review
robertshell22’s picture

StatusFileSize
new7.5 KB

This fixes a submission error on the settings page when trying to save the separator value.

Status: Needs review » Needs work

The last submitted patch, 31: metatag-n3067803-30.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

karens’s picture

Status: Needs work » Needs review
StatusFileSize
new18.64 KB

Updated patch, includes:

  • Settings form configuration to choose a separator to use
  • Schema for the configuration (so tests work)
  • Set the default value of the separator to comma (which avoids the need for any update hooks, doing nothing will use the comma as always)
  • Added a method to detect the separator to use, which is the configured value if set, otherwise the default
  • Added tests for the new separator to set values and test how it behaves on multiple values.
karens’s picture

StatusFileSize
new18.66 KB

A tiny change, use the actual separator in the form description.

osopolar’s picture

Reroll #34.

danharper’s picture

I've tried patch from 35 bit get this error.

Message Error: Call to a member function get() on null in Drupal\metatag\Plugin\metatag\Tag\MetaNameBase->getSeparator() (line 296 of /app/public_html/modules/contrib/metatag/src/Plugin/metatag/Tag/MetaNameBase.php)

andrewsuth’s picture

Patch #35 is incompatible with the Metatag Views submodule as well:

ArgumentCountError: Too few arguments to function Drupal\metatag\MetatagManager::__construct(), 5 passed in /app/web/core/lib/Drupal/Component/DependencyInjection/Container.php on line 259 and exactly 6 expected in Drupal\metatag\MetatagManager->__construct() (line 78 of /app/web/modules/contrib/metatag/src/MetatagManager.php)
#0 /app/web/core/lib/Drupal/Component/DependencyInjection/Container.php(259): Drupal\metatag\MetatagManager->__construct(Object(Drupal\metatag\MetatagGroupPluginManager), Object(Drupal\metatag\MetatagTagPluginManager), Object(Drupal\metatag\MetatagToken), Object(Drupal\Core\Logger\LoggerChannelFactory), Object(Drupal\Core\Entity\EntityTypeManager))
#1 /app/web/core/lib/Drupal/Component/DependencyInjection/Container.php(173): Drupal\Component\DependencyInjection\Container->createService(Array, 'metatag.manager')
#2 /app/web/modules/contrib/metatag/metatag_views/src/Plugin/views/display_extender/MetatagDisplayExtender.php(47): Drupal\Component\DependencyInjection\Container->get('metatag.manager')
#3 /app/web/core/lib/Drupal/Core/Plugin/Factory/ContainerFactory.php(21): Drupal\metatag_views\Plugin\views\display_extender\MetatagDisplayExtender::create(Object(Drupal\Core\DependencyInjection\Container), Array, 'metatag_display...', Array)
#4 /app/web/core/lib/Drupal/Component/Plugin/PluginManagerBase.php(83): Drupal\Core\Plugin\Factory\ContainerFactory->createInstance('metatag_display...', Array)
...
elizoller’s picture

The patch #35 no longer appears to be apply on version 1.15

ioanmar’s picture

I can confirm that latest version doesn't work, patch fails.

damienmckenna’s picture

If the patch doesn't work why do the tests pass? 🤔

I've started the tests again.

eeyorr’s picture

Any updates on this?

ioanmar’s picture

Status: Needs review » Needs work

Are there any plans for a working patch?

bmunslow’s picture

Hi,

I'd like to report that #35 works for me on the 8.x-1.x-dev branch.

Multiple field values are correctly joined together respecting the new delimiter and preserving commas.

The one issue I found with the patch, though, is that this delimiter is also used on tags where the comma delimiter rightly belongs, namely, the robots metatag.

After applying the patch, and setting the delimiter to ||, this is what the robots metatag looks like:

<meta name="robots" content="index|| follow">

but it should be of course:

<meta name="robots" content="index, follow">

jonathan_hunt made their first commit to this issue’s fork.

elgandoz’s picture

Merge request 20 applies for me as patch on 8.x-1.16 and it does the job, although the test fails.

owenbush made their first commit to this issue’s fork.

owenbush’s picture

Status: Needs work » Needs review

Merge Request 20 also applied cleanly for me and worked as expected. I believe the issue with the failed test is because there are two images defined in the og_image_url tag

'og_image_url' => 'https://www.example.com/example/foo.png,https://www.example.com/example/foo2.png',

And the test was missing the second image from the expected tags, I added in the second image and the tests pass locally.

Moving this to Needs Review and waiting for the test bot results to come back.

owenbush’s picture

It did seem like the tests were overlapping each other quite a bit. MetatagManagerTest::testMetatagOrder() and MetatagManagerTest::testMetatagMultiple() were doing exactly the same thing after my change. MetatagManagerTest::testSeparator() was also doing some of the same checks.

I thought maybe MetatagManagerTest::testMetatagOrder() should just have a single value for 'og_image_url' and MetatagManagerTest::testMetatagMultiple() should test for a multi-value.

I've updated the merge request to revert the test back to how it was before my previous commit, and change the value of og_image_url to be a single value in MetatagManagerTest::testMetatagOrder()

brunodbo’s picture

StatusFileSize
new2.19 KB

Updated the D7 patch in #21 so it uses the configured delimiter in DrupalDefaultMetaTag as well.

joshua.boltz’s picture

It may just be me for some reason, but I've had no luck getting any of these D8 patches to apply.
I've seen comments of people applying it to 8.x-1.16, which is also my current version, but I've been unsuccessful.
It seems that this definitely needs a re-roll against latest 8.x-1.19, but has anyone else been successful in applying the patches to any specific versions?

Update:
I've attempted a re-roll of this patch, but the functionality was not working as expected.

I set the custom delimeter to "^".

With the re-rolled patch applied, I was getting a metatag result like so:
"User A^ User B^ User X"

But it seems it was for some reason not then converting the character back to commas for the output.

damienmckenna’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll
damienmckenna’s picture

damienmckenna’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new18.67 KB

Rerolled.

Status: Needs review » Needs work

The last submitted patch, 56: metatag-n3067803-56.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

damienmckenna’s picture

Status: Needs work » Needs review
StatusFileSize
new1.57 KB

Rerolled.

Status: Needs review » Needs work

The last submitted patch, 58: metatag-n3067803-58.patch, failed testing. View results

damienmckenna’s picture

Status: Needs work » Needs review
StatusFileSize
new18.79 KB

Wrong file.

Status: Needs review » Needs work

The last submitted patch, 60: metatag-n3067803-60.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

damienmckenna’s picture

Status: Needs work » Needs review
StatusFileSize
new1.28 KB
new19.86 KB

I think this cover it - there were a few additional locations that a comma was hardcoded in the logic.

damienmckenna’s picture

StatusFileSize
new2.37 KB
new19.75 KB

A minor improvement on the settings form.

damienmckenna’s picture

StatusFileSize
new4.98 KB
new19.74 KB

Some improvements, including removal of the global constant.

Status: Needs review » Needs work

The last submitted patch, 64: metatag-n3067803-64.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

damienmckenna’s picture

Status: Needs work » Needs review
StatusFileSize
new1.15 KB
new19.69 KB

This might work better.

damienmckenna’s picture

Issue tags: -Needs tests

We now have a working solution for both D7 and D9, which should unblock some improvements elsewhere in the ecosystem.

Has anyone tried using the latest patch? Does it work for you?

thejimbirch’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new54.09 KB
new105.25 KB
new47.2 KB
new314.44 KB

The patch in #66 works as expected and it is quite elegant. I am going to RTBC, however, care should be given as to how this is released as it breaks and schema metatags that are installed and white screens the site. I assume the maintainers of both modules will need to work together and merge to dev/release at the same time.

After installing the patch, the user is presented a new option on the metatag settings page with excellent instructions.

Metatag settings page

After changing the default , to ^ I saved.

I then edited a metatag default and added an array of terms using the custom separator. Note that the description on the tags that allow multiple are dynamic and now list the custom separator instead of the comma.

Metatag default configuration

After saving the default, I visited the page and was able to see the 3 different multiple tags in the page's source code.

Meta tags in source code

As a reference, here is the error that happens if any schema metatags are on the node.

Error message

You can see the error is happening in metatag, but that it is caused by schema_web_page.

Thanks for all the great work on this!

damienmckenna’s picture

I need to do more testing with Schema Metatag..

damienmckenna’s picture

I had honestly forgotten that the schema_metatag module was no longer tested by Metatag, so I've rerolled #3123521 and will look into this problem to see if it's possible to avoid the compatibility break.

damienmckenna’s picture

Moving this to Metatag v2.

aaronelborg’s picture

@thejimbirch So you got an error/white screen, right?

So how did you resolve it? Thanks!

thejimbirch’s picture

@AaronELBorg I was just testing the patch. The patch will work in you only use the Metatag module, and not the Schema.org Metatag module.

I believe you will have to wait for Metatag 2.0.0 https://www.drupal.org/project/metatag/issues/3272774 and Schema Metatag https://www.drupal.org/project/schema_metatag/issues/3272777 to catch up, but there has been no movement there it looks like.

ajv009’s picture

Guys dont we have a quick solution or patch for this, which will work with schema metatag module too?

I cant afford to wait too long.

joshua.boltz’s picture

The patch is #66 doesn't seem to apply to latest Metatag anymore, seems like this needs a re-roll.

basvredeling’s picture

StatusFileSize
new18.56 KB
new7.77 KB

Reroll of patch #66 to be compatible with latest 8.x-1.x-dev.

basvredeling’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new19.53 KB

MetatagSeparator.php class file was missing from #76, it's now included in the new patch file.

mmi.cse’s picture

Status: Needs review » Needs work

I have tried patch#77, after applying the patch I got bellow error

The website encountered an unexpected error. Please try again later.
Error: Call to a member function get() on null in Drupal\metatag\Plugin\metatag\Tag\MetaNameBase->getSeparator() (line 27 of modules/contrib/metatag/src/MetatagSeparator.php).
Drupal\metatag\Plugin\metatag\Tag\MetaNameBase->getSeparator() (Line: 780)
Drupal\metatag\MetatagManager->processTagValue(Object, Array, Array, , 'en') (Line: 628)
Drupal\metatag\MetatagManager->generateRawElements(Array, Object) (Line: 560)
Drupal\metatag\MetatagManager->generateElements(Array, Object) (Line: 528)
metatag_get_tags_from_route() (Line: 285)
_metatag_remove_duplicate_entity_tags(Array) (Line: 227)
metatag_entity_view_alter(Array, Object, Object) (Line: 562)
Drupal\Core\Extension\ModuleHandler->alter('node_view', Array, Object, Object) (Line: 305)
Drupal\Core\Entity\EntityViewBuilder->buildMultiple(Array) (Line: 239)
Drupal\Core\Entity\EntityViewBuilder->build(Array)
call_user_func_array(Array, Array) (Line: 101)
Drupal\Core\Render\Renderer->doTrustedCallback(Array, Array, 'Render #pre_render callbacks must be methods of a class that implements \Drupal\Core\Security\TrustedCallbackInterface or be an anonymous function. The callback was %s. See https://www.drupal.org/node/2966725', 'exception', 'Drupal\Core\Render\Element\RenderCallbackInterface') (Line: 772)
Drupal\Core\Render\Renderer->doCallback('#pre_render', Array, Array) (Line: 363)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 201)
Drupal\Core\Render\Renderer->render(Array, ) (Line: 241)
Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 564)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 242)
Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object) (Line: 132)
Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object)
call_user_func(Array, Object, 'kernel.view', Object) (Line: 142)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch(Object, 'kernel.view') (Line: 164)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 81)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 58)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 50)
Drupal\ban\BanMiddleware->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 709)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
damienmckenna’s picture

Yeah, the getSeparator() method needs to be rewritten to be more resilient in case the configuration isn't defined.

jonathan_hunt’s picture

I've applied patch #77 and I get the same error: "Error: Call to a member function get() on null in Drupal\metatag\Plugin\metatag\Tag\MetaNameBase->getSeparator() (line 27 of modules/contrib/metatag/src/MetatagSeparator.php)." Debugging shows that MetatagSeparator.php is in the context of Drupal\metatag\Plugin\metatag\Tag\GeoPlacename and $this->configFactory() is null.
Debugging shows that MetatagSeparator.php is in the context of Drupal\schema_article\Plugin\metatag\Tag\SchemaArticleType and $this->configFactory is null.

Something like the below seems to work:

public function getSeparator() {
    $separator = '';
    if (isset($this->configFactory)) {
      $config = $this->configFactory->get('metatag.settings');
      $separator = trim($config->get('separator') ?? '');
      if ($separator === '') {
        $separator = $this::$default_separator;
      }
      return $separator;
    }
  }
damienmckenna’s picture

Version: 8.x-1.x-dev » 2.0.x-dev

This might need to be rerolled for the 2.0.x branch.

damienmckenna’s picture

Status: Needs work » Needs review
StatusFileSize
new19.5 KB

Rerolled.

damienmckenna’s picture

Status: Needs review » Fixed

Committed. Thanks everyone!

Status: Fixed » Closed (fixed)

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

castor-designs’s picture

I tried to apply patch from #82 and got this error (while a different patch works flawlessly):
Could not apply patch! Skipping. The error was: Cannot apply patch https://www.drupal.org/files/issues/2023-01-05/metatag-n3067803-82.patch

/edit: so it has been committed I guess? But where is the setting even? :D

damienmckenna’s picture

@castor-designs: This change was committed to the 2.0.x branch to be included in the new 2.0.0 release, it isn't in 8.x-1.x releases.

basvredeling’s picture

Version: 2.0.x-dev » 8.x-1.x-dev
StatusFileSize
new19.72 KB
new6.32 KB

@DamienMcKenna can we port a fix to the 1.x branch?

damienmckenna’s picture

This isn't a feature I'm intending to backport as it caused problems with submodules.

basvredeling’s picture

StatusFileSize
new19.63 KB

Thanks for the reply... just leaving a patch here for others to use selectively then.
With correct line endings this time.

anna d’s picture

Any possibilities to have separator+space?
To fix "Value1,Value2" with "Value1, Value2"

Unfortunately space is trimmed after saving form settings.

damienmckenna’s picture

Please open a new issue and we'll be able to help. Thank you.