You are here

class FileEntityNormalizer in File Entity (fieldable files) 8.2

Normalizer for File entity.

Hierarchy

Expanded class hierarchy of FileEntityNormalizer

1 file declares its use of FileEntityNormalizer
FileEntityServiceProvider.php in src/FileEntityServiceProvider.php

File

src/Normalizer/FileEntityNormalizer.php, line 12

Namespace

Drupal\file_entity\Normalizer
View source
class FileEntityNormalizer extends ContentEntityNormalizer {

  /**
   * {@inheritdoc}
   */
  protected $supportedInterfaceOrClass = 'Drupal\\file\\FileInterface';

  /**
   * {@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 = \Drupal::service('file_system')
      ->dirname($entity
      ->getFileUri());
    \Drupal::service('file_system')
      ->prepareDirectory($dirname, FileSystemInterface::CREATE_DIRECTORY);
    if ($uri = \Drupal::service('file_system')
      ->saveData($file_contents, $entity
      ->getFileUri())) {
      $entity
        ->setFileUri($uri);
    }
    else {
      throw new \RuntimeException(new FormattableMarkup('Failed to write @filename.', array(
        '@filename' => $entity
          ->getFilename(),
      )));
    }
    return $entity;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY constant Name of key for bubbling cacheability metadata via serialization context.
ContentEntityNormalizer::$deprecatedProperties protected property
ContentEntityNormalizer::$linkManager protected property The hypermedia link manager.
ContentEntityNormalizer::$moduleHandler protected property The module handler.
ContentEntityNormalizer::getEntityUri protected function Constructs the entity URI. 1
ContentEntityNormalizer::getTypedDataIds protected function Gets the typed data IDs for a type URI.
ContentEntityNormalizer::__construct public function Constructs an ContentEntityNormalizer object. 1
DeprecatedServicePropertyTrait::__get public function Allows to access deprecated/removed properties.
FieldableEntityNormalizerTrait::$entityFieldManager protected property The entity field manager.
FieldableEntityNormalizerTrait::$entityTypeManager protected property The entity type manager. 1
FieldableEntityNormalizerTrait::$entityTypeRepository protected property The entity type repository.
FieldableEntityNormalizerTrait::constructValue protected function Build the field item value using the incoming data. 7
FieldableEntityNormalizerTrait::denormalizeFieldData protected function Denormalizes entity data by denormalizing each field individually.
FieldableEntityNormalizerTrait::determineEntityTypeId protected function Determines the entity type ID to denormalize as.
FieldableEntityNormalizerTrait::extractBundleData protected function Denormalizes the bundle property so entity creation can use it.
FieldableEntityNormalizerTrait::getEntityFieldManager protected function Returns the entity field manager.
FieldableEntityNormalizerTrait::getEntityTypeDefinition protected function Gets the entity type definition.
FieldableEntityNormalizerTrait::getEntityTypeManager protected function Returns the entity type manager.
FieldableEntityNormalizerTrait::getEntityTypeRepository protected function Returns the entity type repository.
FileEntityNormalizer::$supportedInterfaceOrClass protected property The interface or class that this Normalizer supports. Overrides ContentEntityNormalizer::$supportedInterfaceOrClass
FileEntityNormalizer::denormalize public function Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::denormalize(). Overrides ContentEntityNormalizer::denormalize
FileEntityNormalizer::normalize public function Normalizes an object into a set of arrays/scalars. Overrides ContentEntityNormalizer::normalize
NormalizerBase::$format protected property List of formats which supports (de-)normalization. Overrides NormalizerBase::$format
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::supportsDenormalization public function Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization() 1
NormalizerBase::supportsNormalization public function Checks whether the given class is supported for normalization by this normalizer. 1