Problem/Motivation
I have a content type with a multi value text field. I'm using a custom module to create a custom schema.org profile. Here I add the multiple = TRUE to allow the pivot option. When adding multiple values to the field the json is created correctly. So far this works.
{
"@context": "https://schema.org",
"@graph": [
{
"type": "Course",
"name": "INTERdisciPLAYnarity",
....
"creator": [
{
"@type": "Person",
"name": "Prof. Dr. Sandra Aßmann"
},
{
"@type": "Person",
"name": "Prof. Dr. Christian Bunnenberg"
}
]
}
]
}
However, when adding one value to the field it shows only a empty array:
{
"@context": "https://schema.org",
"@graph": [
{
"type": "Course",
"name": "INTERdisciPLAYnarity",
....
"creator": []
}
]
}
What have I missed here?
Steps to reproduce
1. Create a content type with a multi value text field
2. Add schema.org metatag with a Person property.
3. Enable Pivot
4. Add the token for your multi value field
5. Add one/multiple values to the content type
6. See results.
| Comment | File | Size | Author |
|---|---|---|---|
| #13 | schema_metatag-n3224210-13.patch | 656 bytes | damienmckenna |
| #5 | 3224210-pivot-one-valu-multivalue-field-5.patch | 414 bytes | deaom |
Issue fork schema_metatag-3224210
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
Comment #2
saif al-dilaimi commentedComment #3
saif al-dilaimi commentedComment #5
deaom commentedBecause pivot is used, the pivot function returns empty array, as the count is zero. This happens because the explode function does not return an array. After removing lines in code that return a string instead of array if there is only one element in array, the pivot on multi value field which only has one value added no longer returns an empty array in the ld+Json, but the actual data. Because it is still marked as multi value field because of pivot it is still "nested" in array, so not a "format" as without the pivot. Not sure if this breaks anything elsewhere. It needs manual testing and maybe also additional tests need to be written. I'm also attaching a patch besides the commit.
Comment #8
deaom commentedBesides the test needing to be updated, if this is the right approach, I think this now also "breaks" the FAQ rendering if there is an empty value for Q and A. It renders the "empty" main entity, something like:
My updated "quick" fix, that should only work for FAQ is to add additional check in the arrayTrim function in SchemaMetatagManager.php:
Not sure what this will break, so not committing the code just yet, but leaving it here if anybody also gets across this issue and has any better solution.
Comment #9
damienmckennaComment #10
wellsThe #5/MR!14 patch causes the metatag output to also create arrays where it shouldn't. E.g., when I apply the patch an
ImageObjectwithout pivot enabled gets turned in to:So this needs work for more than just tests.
Comment #12
wellsI have opened MR!15 with a different approach that tries to address this specific issue (a single entity in a pivot-configured field) by checking for the condition in
\Drupal\schema_metatag\SchemaMetatagManager::pivot(where we already know a pivot is happening). If pivotable items are not found at that point, the change in MR!15 just returns the original item value.Comment #13
damienmckennaRerolling for 3.0.x.
Comment #16
damienmckennaCommitted. Thank you.