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.

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

Saif Al-Dilaimi created an issue. See original summary.

saif al-dilaimi’s picture

Issue summary: View changes
saif al-dilaimi’s picture

Priority: Normal » Major

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

deaom’s picture

Status: Active » Needs review
StatusFileSize
new414 bytes

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

Status: Needs review » Needs work

The last submitted patch, 5: 3224210-pivot-one-valu-multivalue-field-5.patch, failed testing. View results

deaom’s picture

Besides 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:

"@type": "FAQPage",
            "mainEntity": [
                {
                    "@type": "Question",
                    "answerCount": "1",
                    "acceptedAnswer": {
                        "@type": "Answer"
                    }
                }
            ]

My updated "quick" fix, that should only work for FAQ is to add additional check in the arrayTrim function in SchemaMetatagManager.php:

   // Added before the foreach
   $countBefore = count($array);

    // Specific for FAQ, if the answer array changes (text field is removed)
    // it means it's empty, so remove the array.
    if ($countBefore > count($array) && array_key_last($array) === 'pivot') {
      return [];
    }

    // Specific for FAQ, if the last key is answerCount it means the question
    // is empty so nothing to return.
    if (count($array) == 3 && array_key_last($array) === 'answerCount') {
      return [];
    }
   
    }

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.

damienmckenna’s picture

Version: 8.x-2.2 » 8.x-2.x-dev
Component: Code » Existing structures
wells’s picture

The #5/MR!14 patch causes the metatag output to also create arrays where it shouldn't. E.g., when I apply the patch an ImageObject without pivot enabled gets turned in to:

"image": {
  "@type": ["ImageObject"],
  "representativeOfPage": ["True"],
  "url": ["{URL}"],
  "width": ["1424"],
  "height": ["801"]
}

So this needs work for more than just tests.

wells’s picture

Status: Needs work » Needs review

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

damienmckenna’s picture

Version: 8.x-2.x-dev » 3.0.x-dev
Parent issue: » #3367955: Plan for Schema Metatag 3.0.1
Related issues: +#3325539: Plan for Schema.org Metatag 8.x-2.5
StatusFileSize
new656 bytes

Rerolling for 3.0.x.

  • DamienMcKenna committed 8b231935 on 3.0.x authored by DeaOm
    Issue #3224210 by DeaOm, wells, DamienMcKenna, Saif Al-Dilaimi: Pivot...

  • DamienMcKenna committed 446b31be on 8.x-2.x authored by DeaOm
    Issue #3224210 by DeaOm, wells, DamienMcKenna, Saif Al-Dilaimi: Pivot...
damienmckenna’s picture

Status: Needs review » Fixed

Committed. Thank you.

Status: Fixed » Closed (fixed)

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