Json encode outputs array keys where it shouldn't if some items where removed from array

Created on 10 November 2021, about 3 years ago
Updated 25 August 2023, over 1 year ago

Sorry for the complicated title. Feel free to modify.

Problem/Motivation

Having an array, i.e. for the sameAs property like [0 => 'url0', 2 => 'url2', 3 => 'url3'] the json encoded output will look like:

"sameAs": {
  "0": "url0",
  "2": "url2",
  "3": "url3"
},

Schema Markup Validator complains: sameAs ... is not a known valid target type for the sameAs property.

Steps to reproduce

Create a profile node with different link fields for different social media profiles of the person. Define the sameAs property for "Schema.org: Person": [node:field_social_link_0:uri],[node:field_social_link_1:uri],[node:field_social_link_2:uri],[node:field_social_link_3:uri]

Create the profile node and provide urls for all field_social_link_* fields except field_social_link_1. The string with replaced token will look like url0,,url2,url3 (there is no url1). Which, after explode, results in following php array:

[
0 => url0,
1 => '',
2 => url2,
3 => url3,
]

The element 1 will be removed by array_filter() in \Drupal\schema_metatag\Plugin\metatag\Tag\SchemaNameBase::processItem(), which results into following array:

[
0 => url0,
2 => url2,
3 => url3,
]

Proposed resolution

Change $value = array_filter($value); to $value = array_values(array_filter($value)); to re-index array.

πŸ› Bug report
Status

Needs work

Version

3.0

Component

Code

Created by

πŸ‡©πŸ‡ͺGermany osopolar πŸ‡©πŸ‡ͺ GER 🌐

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024