You are here

class SchemaFieldDefinitionNormalizer in JSON:API Extras 8

Same name and namespace in other branches
  1. 8.3 src/Normalizer/SchemaFieldDefinitionNormalizer.php \Drupal\jsonapi_extras\Normalizer\SchemaFieldDefinitionNormalizer
  2. 8.2 src/Normalizer/SchemaFieldDefinitionNormalizer.php \Drupal\jsonapi_extras\Normalizer\SchemaFieldDefinitionNormalizer

Applies field enhancer schema changes to field schema.

Hierarchy

Expanded class hierarchy of SchemaFieldDefinitionNormalizer

File

src/Normalizer/SchemaFieldDefinitionNormalizer.php, line 12

Namespace

Drupal\jsonapi_extras\Normalizer
View source
class SchemaFieldDefinitionNormalizer extends SchemataJsonSchemaFieldDefinitionNormalizer {

  /**
   * The JSON API resource type repository.
   *
   * @var \Drupal\jsonapi\ResourceType\ResourceTypeRepository
   */
  protected $resourceTypeRepository;

  /**
   * Constructs a SchemaFieldDefinitionNormalizer object.
   *
   * @param \Drupal\jsonapi\ResourceType\ResourceTypeRepository $resource_type_repository
   *   A resource type repository.
   */
  public function __construct(ResourceTypeRepository $resource_type_repository) {
    $this->resourceTypeRepository = $resource_type_repository;
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY constant Name of key for bubbling cacheability metadata via serialization context.
DataDefinitionNormalizer::$describedFormat protected property The formats that the Normalizer can handle. Overrides JsonNormalizerBase::$describedFormat
DataDefinitionNormalizer::$format protected property The formats that the Normalizer can handle. Overrides JsonNormalizerBase::$format
DataDefinitionNormalizer::extractPropertyData protected function Extracts property details from a data definition.
FieldDefinitionNormalizer::$supportedInterfaceOrClass protected property The interface or class that this Normalizer supports. Overrides ListDataDefinitionNormalizer::$supportedInterfaceOrClass
ListDataDefinitionNormalizer::isReferenceField protected function Determine if the current field is a reference field.
NormalizerBase::addCacheableDependency protected function Adds cacheability if applicable.
NormalizerBase::checkFormat protected function Checks if the provided format is supported by this normalizer. Overrides NormalizerBase::checkFormat
NormalizerBase::denormalize public function Denormalizes data back into an object of the given class.
NormalizerBase::normalizeProperties protected function Normalize an array of data definitions.
NormalizerBase::requiredProperty protected function Determine if the given property is a required element of the schema.
NormalizerBase::supportsDenormalization public function Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization() Overrides NormalizerBase::supportsDenormalization
NormalizerBase::supportsNormalization public function Checks whether the given class is supported for normalization by this normalizer. 1
SchemaFieldDefinitionNormalizer::$resourceTypeRepository protected property The JSON API resource type repository.
SchemaFieldDefinitionNormalizer::normalize public function Normalizes an object into a set of arrays/scalars. Overrides FieldDefinitionNormalizer::normalize
SchemaFieldDefinitionNormalizer::__construct public function Constructs a SchemaFieldDefinitionNormalizer object.