You are here

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

Same name and namespace in other branches
  1. 8.3 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 34

Class

SchemaFieldDefinitionNormalizer
Applies field enhancer schema changes to field schema.

Namespace

Drupal\jsonapi_extras\Normalizer

Code

public function normalize($entity, $format = NULL, array $context = []) {
  $normalized = parent::normalize($entity, $format, $context);

  // Load the resource type for this entity type and bundle.
  $resource_type = $this->resourceTypeRepository
    ->get($context['entityTypeId'], $context['bundleId']);
  if (!$resource_type || !$resource_type instanceof ConfigurableResourceType) {
    return $normalized;
  }
  $field_name = $context['name'];
  $enhancer = $resource_type
    ->getFieldEnhancer($field_name);
  if (!$enhancer) {
    return $normalized;
  }
  $original_field_schema = $normalized['properties']['attributes']['properties'][$field_name];
  $field_schema =& $normalized['properties']['attributes']['properties'][$field_name];
  $field_schema = $enhancer
    ->getJsonSchema();

  // Copy *some* properties from the original.
  $copied_properties = [
    'title',
    'description',
  ];
  foreach ($copied_properties as $property_name) {
    if (!empty($original_field_schema[$property_name])) {
      $field_schema[$property_name] = $original_field_schema[$property_name];
    }
  }
  return $normalized;
}