public function SchemaFieldDefinitionNormalizer::normalize in JSON:API Extras 8.2
Same name and namespace in other branches
- 8.3 src/Normalizer/SchemaFieldDefinitionNormalizer.php \Drupal\jsonapi_extras\Normalizer\SchemaFieldDefinitionNormalizer::normalize()
- 8 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\NormalizerCode
public function normalize($entity, $format = NULL, array $context = []) {
$normalized = parent::normalize($entity, $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_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
->getOutputJsonSchema();
// 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;
}