From 072fc0cb0b1c17dff6b73cb0674fb23455a2c404 Mon Sep 17 00:00:00 2001
From: jrockowitz <3537824+jrockowitz@users.noreply.github.com>
Date: Sat, 15 Feb 2025 16:53:40 -0500
Subject: [PATCH] Issue #3500942:
 hook_schemadotorg_jsonld_schema_type_field_alter should get the
 schema_property value as parameter

---
 .../schemadotorg_jsonld.api.php               | 14 ++---
 src/Utility/SchemaDotOrgFieldHelper.php       | 59 +++++++++++++++++++
 2 files changed, 63 insertions(+), 10 deletions(-)
 create mode 100644 src/Utility/SchemaDotOrgFieldHelper.php

diff --git a/modules/schemadotorg_jsonld/schemadotorg_jsonld.api.php b/modules/schemadotorg_jsonld/schemadotorg_jsonld.api.php
index 74290bff2..a1e9f7166 100644
--- a/modules/schemadotorg_jsonld/schemadotorg_jsonld.api.php
+++ b/modules/schemadotorg_jsonld/schemadotorg_jsonld.api.php
@@ -178,12 +178,8 @@ function hook_schemadotorg_jsonld_schema_type_field_alter(array &$data, \Drupal\
   $field_storage = $items->getFieldDefinition()->getFieldStorageDefinition();
   $field_type = $field_storage->getType();
   if ($field_type === 'text_with_summary') {
-    /** @var \Drupal\schemadotorg\SchemaDotOrgMappingStorageInterface $mapping_storage */
-    $mapping_storage = \Drupal::entityTypeManager()->getStorage('schemadotorg_mapping');
-    $mapping = $mapping_storage->loadByEntity($items->getEntity());
-    $field_name = $field_storage->getName();
     $cardinality = $field_storage->getCardinality();
-    $schema_property = $mapping->getSchemaPropertyMapping($field_name);
+    $schema_property = \Drupal\schemadotorg\Utility\SchemaDotOrgFieldHelper::getSchemaProperty($items);
     // For text and articleBody properties set the description
     // to the summary.
     if (in_array($schema_property, ['text', 'articleBody']) && $cardinality === 1) {
@@ -224,11 +220,9 @@ function hook_schemadotorg_jsonld_schema_property_alter(mixed &$value, \Drupal\C
   $main_property_data_type = $main_property_definition->getDataType();
 
   // Get Schema.org mapping.
-  /** @var \Drupal\schemadotorg\SchemaDotOrgMappingStorageInterface $mapping_storage */
-  $mapping_storage = \Drupal::entityTypeManager()->getStorage('schemadotorg_mapping');
-  $mapping = $mapping_storage->loadByEntity($entity);
-  $schema_type = $mapping->getSchemaType();
-  $schema_property = $mapping->getSchemaPropertyMapping($field_name);
+  $mapping = \Drupal\schemadotorg\Utility\SchemaDotOrgFieldHelper::getSchemaMapping($item);
+  $schema_type = \Drupal\schemadotorg\Utility\SchemaDotOrgFieldHelper::getSchemaType($item);
+  $schema_property = \Drupal\schemadotorg\Utility\SchemaDotOrgFieldHelper::getSchemaProperty($item);
 
   // Massage the data.
   // ...
diff --git a/src/Utility/SchemaDotOrgFieldHelper.php b/src/Utility/SchemaDotOrgFieldHelper.php
new file mode 100644
index 000000000..52c3ec1a1
--- /dev/null
+++ b/src/Utility/SchemaDotOrgFieldHelper.php
@@ -0,0 +1,59 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Drupal\schemadotorg\Utility;
+
+use Drupal\Core\Field\FieldItemInterface;
+use Drupal\Core\Field\FieldItemListInterface;
+use Drupal\schemadotorg\Entity\SchemaDotOrgMapping;
+use Drupal\schemadotorg\SchemaDotOrgMappingInterface;
+
+/**
+ * Helper class Schema.org field methods.
+ */
+class SchemaDotOrgFieldHelper {
+
+  /**
+   * Get the Schema.org mapping for a field item or items.
+   *
+   * @param \Drupal\Core\Field\FieldItemInterface|\Drupal\Core\Field\FieldItemListInterface $item
+   *   Field item or items.
+   *
+   * @return \Drupal\schemadotorg\SchemaDotOrgMappingInterface|null
+   *   The Schema.org mapping for a field item or items.
+   */
+  public static function getSchemaMapping(FieldItemInterface|FieldItemListInterface $item): ?SchemaDotOrgMappingInterface {
+    return SchemaDotOrgMapping::loadByEntity($item->getEntity());
+  }
+
+  /**
+   * Get the Schema.org type for a field item or items.
+   *
+   * @param \Drupal\Core\Field\FieldItemInterface|\Drupal\Core\Field\FieldItemListInterface $item
+   *   Field item or items.
+   *
+   * @return string|null
+   *   The Schema.org type for a field item or items.
+   */
+  public static function getSchemaType(FieldItemInterface|FieldItemListInterface $item): ?string {
+    $mapping = static::getSchemaMapping($item);
+    return $mapping ? $mapping->getSchemaType() : NULL;
+  }
+
+  /**
+   * Get the Schema.org property for a field item or items.
+   *
+   * @param \Drupal\Core\Field\FieldItemInterface|\Drupal\Core\Field\FieldItemListInterface $item
+   *   Field item or items.
+   *
+   * @return string|null
+   *   The Schema.org property for a field item or items.
+   */
+  public static function getSchemaProperty(FieldItemInterface|FieldItemListInterface $item): ?string {
+    $mapping = static::getSchemaMapping($item);
+    $field_name = $item->getFieldDefinition()->getName();
+    return $mapping ? $mapping->getSchemaPropertyMapping($field_name) : NULL;
+  }
+
+}
-- 
GitLab