class SchemataSchemaNormalizer in JSON:API Extras 8.3
Same name and namespace in other branches
- 8 src/Normalizer/SchemataSchemaNormalizer.php \Drupal\jsonapi_extras\Normalizer\SchemataSchemaNormalizer
- 8.2 src/Normalizer/SchemataSchemaNormalizer.php \Drupal\jsonapi_extras\Normalizer\SchemataSchemaNormalizer
Applies JSONAPI Extras attribute overrides to entity schemas.
Hierarchy
- class \Drupal\serialization\Normalizer\NormalizerBase implements \Symfony\Component\Serializer\SerializerAwareInterface, CacheableNormalizerInterface uses \Symfony\Component\Serializer\SerializerAwareTrait
- class \Drupal\schemata\Normalizer\NormalizerBase implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface
- class \Drupal\schemata_json_schema\Normalizer\jsonapi\JsonApiNormalizerBase implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface
- class \Drupal\schemata_json_schema\Normalizer\jsonapi\SchemataSchemaNormalizer
- class \Drupal\jsonapi_extras\Normalizer\SchemataSchemaNormalizer
- class \Drupal\schemata_json_schema\Normalizer\jsonapi\SchemataSchemaNormalizer
- class \Drupal\schemata_json_schema\Normalizer\jsonapi\JsonApiNormalizerBase implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface
- class \Drupal\schemata\Normalizer\NormalizerBase implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface
Expanded class hierarchy of SchemataSchemaNormalizer
File
- src/
Normalizer/ SchemataSchemaNormalizer.php, line 12
Namespace
Drupal\jsonapi_extras\NormalizerView source
class SchemataSchemaNormalizer extends SchemataJsonSchemaSchemataSchemaNormalizer {
/**
* The JSON:API resource type repository.
*
* @var \Drupal\jsonapi\ResourceType\ResourceTypeRepository
*/
protected $resourceTypeRepository;
/**
* Constructs a SchemataSchemaNormalizer object.
*
* @param \Drupal\jsonapi\ResourceType\ResourceTypeRepository $resource_type_repository
* A resource 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.
$bundle = $entity
->getBundleId();
$bundle = $bundle ?: $entity
->getEntityTypeId();
$resource_type = $this->resourceTypeRepository
->get($entity
->getEntityTypeId(), $bundle);
if (!$resource_type) {
return $normalized;
}
// Alter the attributes according to the resource config.
if (!empty($normalized['definitions'])) {
$root =& $normalized['definitions'];
}
else {
$root =& $normalized['properties']['data']['properties'];
}
foreach ([
'attributes',
'relationships',
] as $property_type) {
if (!isset($root[$property_type]['required'])) {
$root[$property_type]['required'] = [];
}
$required_fields = [];
$properties = NestedArray::getValue($root, [
$property_type,
'properties',
]);
$properties = $properties ?: [];
foreach ($properties as $fieldname => $schema) {
unset($properties[$fieldname]);
if (!$resource_type
->isFieldEnabled($fieldname)) {
// If the field is disabled, do nothing after removal.
continue;
}
else {
// Otherwise, substitute the public name.
$public_name = $resource_type
->getPublicName($fieldname);
$properties[$public_name] = $schema;
if (in_array($fieldname, $root[$property_type]['required'])) {
$required_fields[] = $public_name;
}
}
}
$root[$property_type]['required'] = $required_fields;
}
return $normalized;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CacheableNormalizerInterface:: |
constant | Name of key for bubbling cacheability metadata via serialization context. | ||
JsonApiNormalizerBase:: |
protected | property | The formats that the Normalizer can handle. | |
JsonApiNormalizerBase:: |
protected | property |
The formats that the Normalizer can handle. Overrides NormalizerBase:: |
|
NormalizerBase:: |
protected | function | Adds cacheability if applicable. | |
NormalizerBase:: |
protected | function |
Checks if the provided format is supported by this normalizer. Overrides NormalizerBase:: |
|
NormalizerBase:: |
public | function | Denormalizes data back into an object of the given class. | |
NormalizerBase:: |
protected | function | Normalize an array of data definitions. | |
NormalizerBase:: |
protected | function | Determine if the given property is a required element of the schema. | |
NormalizerBase:: |
public | function |
Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization() Overrides NormalizerBase:: |
|
NormalizerBase:: |
public | function | Checks whether the given class is supported for normalization by this normalizer. | 1 |
SchemataSchemaNormalizer:: |
protected | property | The JSON:API resource type repository. | |
SchemataSchemaNormalizer:: |
protected | property |
The interface or class that this Normalizer supports. Overrides NormalizerBase:: |
|
SchemataSchemaNormalizer:: |
protected static | function | Identify properties of the data definition to normalize. | |
SchemataSchemaNormalizer:: |
public | function |
Normalizes an object into a set of arrays/scalars. Overrides SchemataSchemaNormalizer:: |
|
SchemataSchemaNormalizer:: |
protected | function | Normalize an array of data definitions. | |
SchemataSchemaNormalizer:: |
public | function | Constructs a SchemataSchemaNormalizer object. |