diff --git a/composer.json b/composer.json index 9e9478136f..1d59dfed50 100644 --- a/composer.json +++ b/composer.json @@ -448,6 +448,9 @@ "3226473 - Opt-in system delete.": "https://www.drupal.org/files/issues/2023-03-28/3226473-opt-in-system-delete-2.patch", "3061620 - Cancel closes all forms": "https://www.drupal.org/files/issues/2022-07-21/inline_entity_form-close-only-forms-where-close-clicked-3061620-13.patch" }, + "drupal/jsonapi_extras": { + "3025283 - Default field enhancer for each field type": "patches/field-type-enhancer-3025283-3.patch" + }, "drupal/jsonapi_menu_items": { "3276561 - Add support for the Menu Item Extras module": "https://www.drupal.org/files/issues/2022-08-01/3276561-05-menu_item_extras_support.patch" }, diff --git a/patches/field-type-enhancer-3025283-3.patch b/patches/field-type-enhancer-3025283-3.patch new file mode 100644 index 0000000000..da26cf269e --- /dev/null +++ b/patches/field-type-enhancer-3025283-3.patch @@ -0,0 +1,36 @@ +diff --git a/src/Normalizer/FieldItemNormalizer.php b/src/Normalizer/FieldItemNormalizer.php +index 8882870..9f8abc8 100644 +--- a/src/Normalizer/FieldItemNormalizer.php ++++ b/src/Normalizer/FieldItemNormalizer.php +@@ -51,12 +51,30 @@ class FieldItemNormalizer extends JsonApiNormalizerDecoratorBase { + public function normalize($object, $format = NULL, array $context = []) { + // First get the regular output. + $normalized_output = parent::normalize($object, $format, $context); ++ + // Then detect if there is any enhancer to be applied here. + /** @var \Drupal\jsonapi_extras\ResourceType\ConfigurableResourceType $resource_type */ + $resource_type = $context['resource_object']->getResourceType(); + $enhancer = $resource_type->getFieldEnhancer($object->getParent()->getName()); ++ + if (!$enhancer) { +- return $normalized_output; ++ // Look for default field type enhancer. ++ $config = \Drupal::config('jsonapi_extras.jsonapi_field_type_config'); ++ $field_type = $object->getFieldDefinition()->getType(); ++ $field_type_config = $config->get('mapping.resourceFieldTypes')[$field_type]; ++ ++ if (!empty($field_type_config['enhancer']['id'])) { ++ // Get the enhancer instance. ++ /** @var \Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerInterface $enhancer */ ++ $enhancer = $this->enhancerManager->createInstance( ++ $field_type_config['enhancer']['id'], ++ (array) $field_type_config['enhancer']['settings'] ++ ); ++ } ++ ++ if (!$enhancer) { ++ return $normalized_output; ++ } + } + $cacheability = CacheableMetadata::createFromObject($normalized_output) + ->addCacheTags(['config:jsonapi_resource_config_list']);