You are here

class AttachmentNormalizer in Replication 8

Same name and namespace in other branches
  1. 8.2 src/Normalizer/AttachmentNormalizer.php \Drupal\replication\Normalizer\AttachmentNormalizer

Hierarchy

Expanded class hierarchy of AttachmentNormalizer

1 string reference to 'AttachmentNormalizer'
replication.services.yml in ./replication.services.yml
replication.services.yml
1 service uses AttachmentNormalizer
replication.normalizer.attachment in ./replication.services.yml
Drupal\replication\Normalizer\AttachmentNormalizer

File

src/Normalizer/AttachmentNormalizer.php, line 9

Namespace

Drupal\replication\Normalizer
View source
class AttachmentNormalizer extends ContentEntityNormalizer implements DenormalizerInterface {

  /**
   * @var string[]
   */
  protected $supportedInterfaceOrClass = [
    '\\Drupal\\file\\FileInterface',
  ];

  /**
   * @var string[]
   */
  protected $format = [
    'stream',
    'base64_stream',
  ];

  /**
   * {@inheritdoc}
   */
  public function normalize($data, $format = NULL, array $context = []) {

    // If the 'new_revision_id' context is TRUE then normalize file entity as a
    // content entity not stream.
    if (!empty($context['new_revision_id'])) {
      return parent::normalize($data, $format, $context);
    }

    /** @var \Drupal\file\FileInterface $data */
    $stream = fopen($data
      ->getFileUri(), 'r');
    return $stream;
  }

  /**
   * {@inheritdoc}
   */
  public function denormalize($data, $class, $format = NULL, array $context = []) {
    $meta_data = is_resource($data) ? stream_get_meta_data($data) : NULL;

    // @todo {@link https://www.drupal.org/node/2599926 Use $class to instantiate the entity.}
    $file_data = [];
    if (isset($meta_data['uri'])) {
      $file_data['uri'] = $meta_data['uri'];
    }
    elseif (isset($context['uri'])) {
      $file_data['uri'] = $context['uri'];
    }
    $file_info_keys = [
      'uuid',
      'status',
      'uid',
      'workspace',
    ];
    foreach ($file_info_keys as $key) {
      if (isset($context[$key])) {
        $file_data[$key] = $context[$key];
      }
    }
    if (isset($context['uuid'])) {
      $workspace = isset($context['workspace']) ? $context['workspace'] : NULL;

      /** @var UuidIndexInterface $uuid_index */
      $uuid_index = $this->indexFactory
        ->get('multiversion.entity_index.uuid', $workspace);
      $entity_info = $uuid_index
        ->get($context['uuid']);
      if (!empty($entity_info)) {

        /** @var FileInterface $file */
        $file = $this->entityManager
          ->getStorage($entity_info['entity_type_id'])
          ->load($entity_info['entity_id']);
        if (!empty($file)) {
          foreach ($file_data as $key => $data) {
            $file->{$key} = $data;
          }
          return $file;
        }
      }
    }
    return $this->entityManager
      ->getStorage('file')
      ->create($file_data);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AttachmentNormalizer::$format protected property Overrides ContentEntityNormalizer::$format
AttachmentNormalizer::$supportedInterfaceOrClass protected property Overrides ContentEntityNormalizer::$supportedInterfaceOrClass
AttachmentNormalizer::denormalize public function Denormalizes data back into an object of the given class. Overrides ContentEntityNormalizer::denormalize
AttachmentNormalizer::normalize public function Normalizes an object into a set of arrays/scalars. Overrides ContentEntityNormalizer::normalize
CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY constant Name of key for bubbling cacheability metadata via serialization context.
ContentEntityNormalizer::$dispatcher protected property
ContentEntityNormalizer::$indexFactory protected property
ContentEntityNormalizer::$languageManager protected property
ContentEntityNormalizer::$moduleHandler private property
ContentEntityNormalizer::$selectionManager protected property
ContentEntityNormalizer::$usersMapping protected property
ContentEntityNormalizer::createEntityInstance private function Handles entity creation for fieldable and non-fieldable entities.
ContentEntityNormalizer::denormalizeMenuLinkParent protected function
ContentEntityNormalizer::denormalizeTranslation private function
ContentEntityNormalizer::__construct public function 1
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.
NormalizerBase::addCacheableDependency protected function Adds cacheability if applicable.
NormalizerBase::checkFormat protected function Checks if the provided format is supported by this normalizer. 2
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