class FileEntityNormalizer in Better Normalizers 8
Normalizer for File entity.
Hierarchy
- class \Drupal\serialization\Normalizer\NormalizerBase implements \Symfony\Component\Serializer\SerializerAwareInterface, CacheableNormalizerInterface uses \Symfony\Component\Serializer\SerializerAwareTrait
- class \Drupal\hal\Normalizer\NormalizerBase implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface
- class \Drupal\hal\Normalizer\ContentEntityNormalizer uses DeprecatedServicePropertyTrait, FieldableEntityNormalizerTrait
- class \Drupal\better_normalizers\Normalizer\FileEntityNormalizer
- class \Drupal\hal\Normalizer\ContentEntityNormalizer uses DeprecatedServicePropertyTrait, FieldableEntityNormalizerTrait
- class \Drupal\hal\Normalizer\NormalizerBase implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface
Expanded class hierarchy of FileEntityNormalizer
File
- src/
Normalizer/ FileEntityNormalizer.php, line 16
Namespace
Drupal\better_normalizers\NormalizerView source
class FileEntityNormalizer extends ContentEntityNormalizer {
/**
* {@inheritdoc}
*/
protected $supportedInterfaceOrClass = 'Drupal\\file\\FileInterface';
/**
* The file system service.
*
* @var \Drupal\Core\File\FileSystemInterface
*/
protected $fileSystem;
/**
* {@inheritdoc}
*/
public function __construct(LinkManagerInterface $link_manager, EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler, EntityTypeRepositoryInterface $entity_type_repository, EntityFieldManagerInterface $entity_field_manager, FileSystemInterface $file_system) {
parent::__construct($link_manager, $entity_type_manager, $module_handler, $entity_type_repository, $entity_field_manager);
$this->fileSystem = $file_system;
}
/**
* {@inheritdoc}
*/
public function normalize($entity, $format = NULL, array $context = array()) {
$data = parent::normalize($entity, $format, $context);
if (!isset($context['included_fields']) || in_array('data', $context['included_fields'])) {
// Save base64-encoded file contents to the "data" property.
$file_data = base64_encode(file_get_contents($entity
->getFileUri()));
$data += array(
'data' => array(
array(
'value' => $file_data,
),
),
);
}
return $data;
}
/**
* {@inheritdoc}
*/
public function denormalize($data, $class, $format = NULL, array $context = array()) {
// Avoid 'data' being treated as a field.
$file_data = $data['data'][0]['value'];
unset($data['data']);
// Decode and save to file.
$file_contents = base64_decode($file_data);
$entity = parent::denormalize($data, $class, $format, $context);
$dirname = $this->fileSystem
->dirname($entity
->getFileUri());
$this->fileSystem
->prepareDirectory($dirname, FileSystemInterface::CREATE_DIRECTORY);
if ($uri = $this->fileSystem
->saveData($file_contents, $entity
->getFileUri())) {
$entity
->setFileUri($uri);
}
else {
throw new \RuntimeException(sprintf('Failed to write %s.', $entity
->getFilename()));
}
return $entity;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CacheableNormalizerInterface:: |
constant | Name of key for bubbling cacheability metadata via serialization context. | ||
ContentEntityNormalizer:: |
protected | property | ||
ContentEntityNormalizer:: |
protected | property | The hypermedia link manager. | |
ContentEntityNormalizer:: |
protected | property | The module handler. | |
ContentEntityNormalizer:: |
protected | function | Constructs the entity URI. | 1 |
ContentEntityNormalizer:: |
protected | function | Gets the typed data IDs for a type URI. | |
DeprecatedServicePropertyTrait:: |
public | function | Allows to access deprecated/removed properties. | |
FieldableEntityNormalizerTrait:: |
protected | property | The entity field manager. | |
FieldableEntityNormalizerTrait:: |
protected | property | The entity type manager. | 1 |
FieldableEntityNormalizerTrait:: |
protected | property | The entity type repository. | |
FieldableEntityNormalizerTrait:: |
protected | function | Build the field item value using the incoming data. | 7 |
FieldableEntityNormalizerTrait:: |
protected | function | Denormalizes entity data by denormalizing each field individually. | |
FieldableEntityNormalizerTrait:: |
protected | function | Determines the entity type ID to denormalize as. | |
FieldableEntityNormalizerTrait:: |
protected | function | Denormalizes the bundle property so entity creation can use it. | |
FieldableEntityNormalizerTrait:: |
protected | function | Returns the entity field manager. | |
FieldableEntityNormalizerTrait:: |
protected | function | Gets the entity type definition. | |
FieldableEntityNormalizerTrait:: |
protected | function | Returns the entity type manager. | |
FieldableEntityNormalizerTrait:: |
protected | function | Returns the entity type repository. | |
FileEntityNormalizer:: |
protected | property | The file system service. | |
FileEntityNormalizer:: |
protected | property |
The interface or class that this Normalizer supports. Overrides ContentEntityNormalizer:: |
|
FileEntityNormalizer:: |
public | function |
Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::denormalize(). Overrides ContentEntityNormalizer:: |
|
FileEntityNormalizer:: |
public | function |
Normalizes an object into a set of arrays/scalars. Overrides ContentEntityNormalizer:: |
|
FileEntityNormalizer:: |
public | function |
Constructs an ContentEntityNormalizer object. Overrides ContentEntityNormalizer:: |
|
NormalizerBase:: |
protected | property |
List of formats which supports (de-)normalization. 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 | Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization() | 1 |
NormalizerBase:: |
public | function | Checks whether the given class is supported for normalization by this normalizer. | 1 |