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.
| Comment | File | Size | Author |
|---|---|---|---|
| #82 | metatag-n3067803-82.patch | 19.5 KB | damienmckenna |
| #66 | metatag-n3067803-66.patch | 19.69 KB | damienmckenna |
| #66 | metatag-n3067803-66.interdiff.txt | 1.15 KB | damienmckenna |
Issue fork metatag-3067803
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:
- 8.x-1.x
changes, plain diff MR !20
- 3067803-use-custom-delimiter
changes, plain diff MR !21
Comments
Comment #2
karens commentedComment #3
karens commentedComment #4
damienmckennaSeeing 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?
Comment #5
karens commentedWe 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.
Comment #6
karens commentedMaybe something like this then? Not really tested, just to show the idea.
Comment #8
karens commentedChanging title
Comment #9
karens commentedComment #11
karens commentedComment #12
karens commentedOK 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.
Comment #13
karens commentedFor 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.
Comment #14
earthday47I can verify the patch in #11 is working for me.
Comment #15
jeroentPatch in #11 is also working fine for me.
Comment #16
damienmckennaRerolled.
Comment #18
damienmckennaThis 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.
Comment #19
damienmckennaBack to 'needs work'.
Comment #20
peter3bweb commentedI 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?
Comment #21
peter3bweb commentedI created a patch for this issue with the Drupal 7 branch (this is my first time doing this)
Comment #22
peter3bweb commentedDidn't mean to hide Damien's files
Comment #23
damienmckennaThank you for the backport to D7.
Comment #24
peter3bweb commentedThanks for reviewing DamienMcKenna
Comment #25
damienmckennaWIP on adding a setting form to control the delimiter.
Comment #27
aludescher commentedComment #28
aludescher commentedComment #29
aludescher commentedComment #30
jeroentComment #31
robertshell22 commentedThis fixes a submission error on the settings page when trying to save the separator value.
Comment #33
karens commentedUpdated patch, includes:
Comment #34
karens commentedA tiny change, use the actual separator in the form description.
Comment #35
osopolarReroll #34.
Comment #36
danharper commentedI'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)
Comment #37
andrewsuth commentedPatch #35 is incompatible with the Metatag Views submodule as well:
Comment #38
elizoller commentedThe patch #35 no longer appears to be apply on version 1.15
Comment #39
ioanmar commentedI can confirm that latest version doesn't work, patch fails.
Comment #40
damienmckennaIf the patch doesn't work why do the tests pass? 🤔
I've started the tests again.
Comment #41
eeyorrAny updates on this?
Comment #42
ioanmar commentedAre there any plans for a working patch?
Comment #43
bmunslow commentedHi,
I'd like to report that #35 works for me on the
8.x-1.x-devbranch.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
robotsmetatag.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">Comment #48
elgandoz commentedMerge request 20 applies for me as patch on 8.x-1.16 and it does the job, although the test fails.
Comment #50
owenbush commentedMerge 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.
Comment #51
owenbush commentedIt 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()
Comment #52
brunodboUpdated the D7 patch in #21 so it uses the configured delimiter in
DrupalDefaultMetaTagas well.Comment #53
joshua.boltz commentedIt 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.
Comment #54
damienmckennaComment #55
damienmckennaComment #56
damienmckennaRerolled.
Comment #58
damienmckennaRerolled.
Comment #60
damienmckennaWrong file.
Comment #62
damienmckennaI think this cover it - there were a few additional locations that a comma was hardcoded in the logic.
Comment #63
damienmckennaA minor improvement on the settings form.
Comment #64
damienmckennaSome improvements, including removal of the global constant.
Comment #66
damienmckennaThis might work better.
Comment #67
damienmckennaWe 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?
Comment #68
thejimbirch commentedThe 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.
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.
After saving the default, I visited the page and was able to see the 3 different multiple tags in the page's source code.
As a reference, here is the error that happens if any schema metatags are on the node.
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!
Comment #69
damienmckennaI need to do more testing with Schema Metatag..
Comment #70
damienmckennaI 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.
Comment #71
damienmckennaMoving this to Metatag v2.
Comment #72
aaronelborg commented@thejimbirch So you got an error/white screen, right?
So how did you resolve it? Thanks!
Comment #73
thejimbirch commented@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.
Comment #74
ajv009 commentedGuys 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.
Comment #75
joshua.boltz commentedThe patch is #66 doesn't seem to apply to latest Metatag anymore, seems like this needs a re-roll.
Comment #76
basvredelingReroll of patch #66 to be compatible with latest 8.x-1.x-dev.
Comment #77
basvredelingMetatagSeparator.php class file was missing from #76, it's now included in the new patch file.
Comment #78
mmi.cse commentedI have tried patch#77, after applying the patch I got bellow error
Comment #79
damienmckennaYeah, the getSeparator() method needs to be rewritten to be more resilient in case the configuration isn't defined.
Comment #80
jonathan_hunt commentedI'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 thatMetatagSeparator.phpis in the context ofDrupal\metatag\Plugin\metatag\Tag\GeoPlacenameand$this->configFactory()is null.Debugging shows that
MetatagSeparator.phpis in the context ofDrupal\schema_article\Plugin\metatag\Tag\SchemaArticleTypeand$this->configFactoryis null.Something like the below seems to work:
Comment #81
damienmckennaThis might need to be rerolled for the 2.0.x branch.
Comment #82
damienmckennaRerolled.
Comment #84
damienmckennaCommitted. Thanks everyone!
Comment #86
castor-designs commentedI 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
Comment #87
damienmckenna@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.
Comment #88
basvredeling@DamienMcKenna can we port a fix to the 1.x branch?
Comment #89
damienmckennaThis isn't a feature I'm intending to backport as it caused problems with submodules.
Comment #90
basvredelingThanks for the reply... just leaving a patch here for others to use selectively then.
With correct line endings this time.
Comment #91
anna d commentedAny possibilities to have separator+space?
To fix "Value1,Value2" with "Value1, Value2"
Unfortunately space is trimmed after saving form settings.
Comment #92
damienmckennaPlease open a new issue and we'll be able to help. Thank you.