hook_schemadotorg_jsonld_schema_type_field_alter should get the schema_property value as parameter

Created on 20 January 2025, 4 months ago

Problem/Motivation

According \Drupal\schemadotorg_jsonld\SchemaDotOrgJsonLdBuilder::buildMappedEntity(), the hook_schemadotorg_jsonld_schema_type_field_alter should get the schema_property is called for each property in the entity mapping. The thing is that as the $schema_property value is not passed to the hook, we have to load the mapping to be able to alter the appropriate property (as shown in \Drupal\schemadotorg_smart_date\SchemaDotOrgSmartDateJsonLdManager::alterProperties()).

This step wouldn't be necessary in most use cases if the $schema_property value were available in the hook.

✨ Feature request
Status

Active

Version

1.0

Component

Code

Created by

πŸ‡«πŸ‡·France duaelfr Montpellier, France

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

Merge Requests

Comments & Activities

  • Issue created by @duaelfr
  • πŸ‡ΊπŸ‡ΈUnited States jrockowitz Brooklyn, NY

    Your request makes sense. Let me think about the implications before we make the change.

    Below are hooks that could have $schema_type and $schema_property defined. Another approach is to add a $context that would allows $schema_mapping, $schema_type, and $schema_property.

    hook_schemadotorg_jsonld_schema_type_field_alter(array &$data, \Drupal\Core\Field\FieldItemListInterface $items, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata);
    
    hook_schemadotorg_jsonld_schema_property_alter(mixed &$value, \Drupal\Core\Field\FieldItemInterface $item, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata);
    
    hook_schemadotorg_jsonld_schema_properties_alter(array &$values, \Drupal\Core\Field\FieldItemListInterface $items, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata);
    
  • πŸ‡ΊπŸ‡ΈUnited States jrockowitz Brooklyn, NY

    Passing the \Drupal\Core\Field\FieldItemInterface or \Drupal\Core\Field\FieldItemListInterface to the below hooks feels like the right thing to do, and we MUST pass the BubbleableMetadata, which allows JSON-LD to be cached.

    hook_schemadotorg_jsonld_schema_type_field_alter(array &$data, \Drupal\Core\Field\FieldItemListInterface $items, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata);
    
    hook_schemadotorg_jsonld_schema_property_alter(mixed &$value, \Drupal\Core\Field\FieldItemInterface $item, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata);
    
    hook_schemadotorg_jsonld_schema_properties_alter(array &$values, \Drupal\Core\Field\FieldItemListInterface $items, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata);
    
    A maximum of 2 alterable arguments is supported. In case more arguments need to be passed and alterable, modules provide additional variables assigned by reference in the last $context argument:

    @see https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Extension...

    I experimented with adding $context, which contains the items, mapping, schema_type, and schema_property, and that started to feel ugly because users have started parsing through the $context. Moving $items to $context also breaks anyone's current implementation of these hooks.

    The best compromise I can come up with is to provide a \Drupal\schemadotorg\Utility\SchemaDotOrgFieldHelper, which makes it easier to get the mapping, scheme type, and schema property.

    Please review the attached proposed MR.

  • πŸ‡ΊπŸ‡ΈUnited States jrockowitz Brooklyn, NY

    I think the SchemaDotOrgFieldHelper does improve things.

Production build 0.71.5 2024