class UriNormalizer in Tome 8
Normalizer for Uri data.
@internal
Hierarchy
- class \Drupal\serialization\Normalizer\NormalizerBase implements \Symfony\Component\Serializer\SerializerAwareInterface, CacheableNormalizerInterface uses \Symfony\Component\Serializer\SerializerAwareTrait
- class \Drupal\serialization\Normalizer\PrimitiveDataNormalizer uses SerializedColumnNormalizerTrait
- class \Drupal\tome_sync\Normalizer\UriNormalizer implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface
- class \Drupal\serialization\Normalizer\PrimitiveDataNormalizer uses SerializedColumnNormalizerTrait
Expanded class hierarchy of UriNormalizer
1 string reference to 'UriNormalizer'
- tome_sync.services.yml in modules/
tome_sync/ tome_sync.services.yml - modules/tome_sync/tome_sync.services.yml
1 service uses UriNormalizer
File
- modules/
tome_sync/ src/ Normalizer/ UriNormalizer.php, line 16
Namespace
Drupal\tome_sync\NormalizerView source
class UriNormalizer extends PrimitiveDataNormalizer implements DenormalizerInterface {
/**
* {@inheritdoc}
*/
protected $supportedInterfaceOrClass = '\\Drupal\\Core\\TypedData\\Type\\UriInterface';
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The entity repository.
*
* @var \Drupal\Core\Entity\EntityRepositoryInterface
*/
protected $entityRepository;
/**
* Constructs a UriNormalizer object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The entity field manager.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityRepositoryInterface $entity_repository) {
$this->entityTypeManager = $entity_type_manager;
$this->entityRepository = $entity_repository;
}
/**
* {@inheritdoc}
*/
public function normalize($object, $format = NULL, array $context = []) {
$value = parent::normalize($object, $format, $context);
if (!empty($value) && strpos($value, 'entity:') === 0) {
$parts = explode('/', str_replace('entity:', '', $value));
if (count($parts) >= 2 && $this->entityTypeManager
->hasDefinition($parts[0]) && is_numeric($parts[1])) {
if ($entity = $this->entityTypeManager
->getStorage($parts[0])
->load($parts[1])) {
$parts[1] = $entity
->uuid();
$value = 'entity:' . implode('/', $parts);
}
}
}
return $value;
}
/**
* {@inheritdoc}
*/
public function denormalize($data, $type, $format = NULL, array $context = []) {
if (!empty($data) && strpos($data, 'entity:') === 0) {
$parts = explode('/', str_replace('entity:', '', $data));
if (count($parts) >= 2 && $this->entityTypeManager
->hasDefinition($parts[0]) && Uuid::isValid($parts[1])) {
if ($referenced_entity = $this->entityRepository
->loadEntityByUuid($parts[0], $parts[1])) {
$parts[1] = $referenced_entity
->id();
$data = 'entity:' . implode('/', $parts);
}
}
}
return $data;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CacheableNormalizerInterface:: |
constant | Name of key for bubbling cacheability metadata via serialization context. | ||
NormalizerBase:: |
protected | property | List of formats which supports (de-)normalization. | 3 |
NormalizerBase:: |
protected | function | Adds cacheability if applicable. | |
NormalizerBase:: |
protected | function | Checks if the provided format is supported by this normalizer. | 2 |
NormalizerBase:: |
public | function | Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization() | 1 |
NormalizerBase:: |
public | function | Checks whether the given class is supported for normalization by this normalizer. | 1 |
SerializedColumnNormalizerTrait:: |
protected | function | Checks if there is a serialized string for a column. | |
SerializedColumnNormalizerTrait:: |
protected | function | Checks if the data contains string value for serialize column. | |
SerializedColumnNormalizerTrait:: |
protected | function | Gets the names of all properties the plugin treats as serialized data. | |
SerializedColumnNormalizerTrait:: |
protected | function | Gets the names of all serialized properties. | |
UriNormalizer:: |
protected | property | The entity repository. | |
UriNormalizer:: |
protected | property | The entity type manager. | |
UriNormalizer:: |
protected | property |
The interface or class that this Normalizer supports. Overrides PrimitiveDataNormalizer:: |
|
UriNormalizer:: |
public | function | ||
UriNormalizer:: |
public | function |
Normalizes an object into a set of arrays/scalars. Overrides PrimitiveDataNormalizer:: |
|
UriNormalizer:: |
public | function | Constructs a UriNormalizer object. |