You are here

public function SchemaFieldDefinitionNormalizer::normalize in JSON:API Extras 8.3

Same name and namespace in other branches
  1. 8 src/Normalizer/SchemaFieldDefinitionNormalizer.php \Drupal\jsonapi_extras\Normalizer\SchemaFieldDefinitionNormalizer::normalize()
  2. 8.2 src/Normalizer/SchemaFieldDefinitionNormalizer.php \Drupal\jsonapi_extras\Normalizer\SchemaFieldDefinitionNormalizer::normalize()

Overrides FieldDefinitionNormalizer::normalize

File

src/Normalizer/SchemaFieldDefinitionNormalizer.php, line 36

Class

SchemaFieldDefinitionNormalizer
Applies field enhancer schema changes to field schema.

Namespace

Drupal\jsonapi_extras\Normalizer

Code

public function normalize($field_definition, $format = NULL, array $context = []) {
  assert($field_definition instanceof FieldDefinitionInterface);
  $normalized = parent::normalize($field_definition, $format, $context);

  // Load the resource type for this entity type and bundle.
  $bundle = empty($context['bundleId']) ? $context['entityTypeId'] : $context['bundleId'];
  $resource_type = $this->resourceTypeRepository
    ->get($context['entityTypeId'], $bundle);
  if (!$resource_type || !$resource_type instanceof ConfigurableResourceType) {
    return $normalized;
  }
  $field_name = $context['name'];
  $enhancer = $resource_type
    ->getFieldEnhancer($field_definition
    ->getName());
  if (!$enhancer) {
    return $normalized;
  }
  $parents = [
    'properties',
    'attributes',
    'properties',
    $field_name,
  ];
  $original_field_schema = NestedArray::getValue($normalized, $parents);
  $to_copy = [
    'title',
    'description',
  ];
  $field_schema = array_merge($enhancer
    ->getOutputJsonSchema(), array_intersect_key($original_field_schema, array_flip($to_copy)));
  NestedArray::setValue($normalized, $parents, $field_schema);
  return $normalized;
}