Problem/Motivation
After applying patch #3182969: Wrong property_type for schema_qa_page_main_entity I can now populate the fields for Q/A in the Schema.org metatags for FAQpage. The problem is that if you enable pivot the questions are formatted correctly, but the answers are not split up properly. The output looks like this:
{
"@context": "https://schema.org",
"@graph": [
{
"@type": "FAQPage",
"mainEntity": [
{
"@type": "Question",
"name": "This is a test meta q1",
"answerCount": "1",
"acceptedAnswer": {
"@type": "Answer",
"text": [
"This is a test meta answer 1",
"This is a test meta answer 2"
],
"url": "https://mysite.com/en/test-1"
}
},
{
"@type": "Question",
"name": "This is a test meta q2",
"answerCount": "1",
"acceptedAnswer": {
"@type": "Answer",
"text": [
"This is a test meta answer 1",
"This is a test meta answer 2"
],
"url": "https://mysite.com/en/test-1"
}
}
]
},
{
"@type": "WebPage"
}
]
}I believe the correct format should be:
{
"@context": "https://schema.org",
"@graph": [
{
"@type": "FAQPage",
"mainEntity": [
{
"@type": "Question",
"name": "This is a test meta q1",
"answerCount": "1",
"acceptedAnswer": {
"@type": "Answer",
"text": [
"This is a test meta answer 1"
],
"url": "https://mysite.com/en/test-1"
}
},
{
"@type": "Question",
"name": "This is a test meta q2",
"answerCount": "1",
"acceptedAnswer": {
"@type": "Answer",
"text": [
"This is a test meta answer 2"
],
"url": "https://mysite.com/en/test-1"
}
}
]
},
{
"@type": "WebPage"
}
]
}Steps to reproduce
1. Enable FAQPage for schema_metatags on a content-type
2. Create two multivalue long text fields for question and answer on the c-t
3. Configure settings for schema_metadate FAQ/QA page set @type to FAQPage, configure question as token [node:field_meta_question], configure and answer as token [node:field_meta_answer] and choose PIVOT.
4. Create a node with a title and two questions and answers
5. View node header and see the issue
Proposed resolution
TBD
| Comment | File | Size | Author |
|---|---|---|---|
| #22 | 3186311_22.patch | 1.34 KB | ghost of drupal past |
Issue fork schema_metatag-3186311
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
Steppico commentedHi, I'm having the same issue.
I had a look into the source code, and I noticed that we should add a "Pivot" option to the acceptedAnswer field to obtain the wanted result.
I tested this real quick by hardcoding it in my code, just so to follow my hunch, and it worked as intended.
If no one comes up with a solution before I do, I'll write a patch for this.
Comment #3
denizserhat commentedI still have not found a solution to the issue and issues have come up at google.com/test/rich-results. You can share the patch until a solution is found?
Comment #4
Steppico commentedSorry for the late reply, I ended up working on other features.
I'll come back soon at this issue and commit a proper patch.
Comment #5
Steppico commented@denizserhat,
I started working on this. If however you want a quick solution, you can create a custom module and add a hook_form_alter that creates a pivot to "acceptedAnswer".
Something like this:
It should work correctly, for now ✌️
Comment #6
lindsay.wils commentedThanks for starting to look at this. I am also seeing this issue and hoping for a patch. I tried the hook_form_alter on both the meta tags settings form and the node form and this did look to give the desired result.
Comment #7
IsaacOldwood commentedThe hook_form_alter() in #5 worked for me although I did need to alter it to
Comment #8
avpadernoComment #9
denizserhat commented@lindsay.wils
Thanks for your reply
I don't know how to create a "custom module and add a hook_form_alter". For the time being I use 8.x-1.8.
Comment #10
adrienco88 commentedFor people who need this until this is patched in the module, I created this in a more dynamic form, so it can be used on all the forms that have the FAQ Schema and also to save the "pivot" value when you re-edit the node. In my case I needed the pivot for both accepted answer and suggested answer.
Comment #11
basby commentedThanks! #10 works for me ... almost. I had to replace field_metatags with field_meta_tags.
Comment #12
lindsay.wils commentedHello. Adding a related issue to this.
With the hook_form_alter alter in place, the JSON formatted schema is now correct. Though, if a comma "," is entered into any question or answer text, this seems to mess with the pivot and breaks the output up over a series of extra question and answer combinations.
Is anyone else seeing this issue? Would be great to get this resolved also if a patch is to be created.
Thanks in advance.
Comment #13
mrdalesmith commentedMy version of the code in #10 is slightly more complex but will work whatever you've called your metatag fields and also works on the default configuration form.
this is obviously just a quick fix: I can't work out the notation of the Question and Answer types to work out how to correct get the pivot field set by the existing code.
Comment #14
damienmckennaLet's see if this is still a problem after #2976935 lands.
Comment #15
hunchang commentedafter apply the both https://www.drupal.org/project/schema_metatag/issues/2976935 & https://www.drupal.org/project/metatag/issues/3067803 patches the separator is working.
However, the answer still not split out properly.
Before apply #10 solution,
after #10 solution
Comment #17
deaom commentedIt seems that the #2976935: Improve support for multi-value schema using Metatag's custom separator option did not solve the pivot issue on FAQ and that issue is also postponed due to #3067803: Use custom delimiter instead of commas for multiple values which is RTBC. There are two possible solutions, one is to add the pivot field to sub properties (which this patch/commit does and what the workarounds suggest), the other one would be to better handle the pivot item, when there is a main entity that has the pivot set. Not sure how that would work if you do not wanna have the pivot automatically set for all the sub properties.
Comment #19
liliplanet commentedI tried #17 patch, site error
<'ParseError: syntax error, unexpected token "else", expecting end of file in Composer\Autoload\includeFile() (line 248 of /home/mysite/public_html/modules/contrib/schema_metatag/src/Plugin/schema_metatag/PropertyTypeBase.php)/code>Comment #20
damienmckenna@liliplanet: I'm not seeing that error locally, can you please try again? Thank you.
Comment #21
damienmckennaComment #22
ghost of drupal pastThis is a lovely patch. I have simplified a little and added a strict search to
array_search.Comment #23
ghost of drupal pastComment #24
dmezquiaPatch #22 works for me OK.
Drupal 9.4.8,
schema_metatag 2.4
Comment #25
castor-designs commentedPatch works for me as well but definitely should be added to the main version of this module!
Comment #26
castor-designs commentedSecond topic: As soon as your sentence e. g. in the answer has a comma in it it pivots as well. So if I use fields to populate this section e. g. like this:
[term:field_faq:0],[term:field_faq:1],[term:field_faq:2]then as soon as a value of this field has a comma in it e. g. "How long have you been studying English, and what motivated you to start?" it will pivot that but it makes no sense of course.So is there a way to prevent this?
Comment #27
borutpiletic commentedI can confirm, patch #22 solved the issue for me on FAQPage.
schema_metatag 2.4
Drupal core 9.5.9
Comment #30
damienmckennaCommitted. Thank you!
Comment #31
damienmckennaCastor-designs: Please see the changes from #2976935: Improve support for multi-value schema using Metatag's custom separator option that went into the 3.0.x branch; you'll need to upgrade to Metatag v2 in order to use it.