You are here

class FileEntityNormalizer in Replication 8.2

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

Hierarchy

Expanded class hierarchy of FileEntityNormalizer

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

File

src/Normalizer/FileEntityNormalizer.php, line 16

Namespace

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

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

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

  /**
   * @var \Drupal\replication\ProcessFileAttachment
   */
  protected $processFileAttachment;

  /**
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
   * @param \Drupal\multiversion\Entity\Index\MultiversionIndexFactory $index_factory
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   * @param \Drupal\replication\UsersMapping $users_mapping
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   * @param \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManagerInterface $selection_manager
   * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
   * @param \Drupal\replication\ProcessFileAttachment $process_file_attachment
   */
  public function __construct(EntityManagerInterface $entity_manager, MultiversionIndexFactory $index_factory, LanguageManagerInterface $language_manager, UsersMapping $users_mapping, ModuleHandlerInterface $module_handler, SelectionPluginManagerInterface $selection_manager = NULL, EventDispatcherInterface $event_dispatcher = NULL, ProcessFileAttachment $process_file_attachment) {
    parent::__construct($entity_manager, $index_factory, $language_manager, $users_mapping, $module_handler, $selection_manager, $event_dispatcher);
    $this->processFileAttachment = $process_file_attachment;
  }

  /**
   * {@inheritdoc}
   */
  public function normalize($data, $format = NULL, array $context = []) {
    $normalized = parent::normalize($data, $format, $context);
    $file_system = \Drupal::service('file_system');
    $uri = $data
      ->getFileUri();
    $file_contents = file_get_contents($uri);
    if (in_array($file_system
      ->uriScheme($uri), [
      'public',
      'private',
    ]) == FALSE) {
      $file_data = '';
    }
    else {
      $file_data = base64_encode($file_contents);
    }

    // @todo {@link https://www.drupal.org/node/2600360 Add revpos and other missing properties to the result array.}
    $normalized['@attachment'] = [
      'uuid' => $data
        ->uuid(),
      'uri' => $uri,
      'content_type' => $data
        ->getMimeType(),
      'digest' => 'md5-' . base64_encode(md5($file_contents)),
      'length' => $data
        ->getSize(),
      'data' => $file_data,
    ];
    return $normalized;
  }

  /**
   * {@inheritdoc}
   */
  public function denormalize($data, $class, $format = NULL, array $context = []) {
    $file = NULL;
    if (!empty($data['@attachment']['uuid'])) {
      $workspace = isset($context['workspace']) ? $context['workspace'] : NULL;

      /** @var FileInterface $file */
      $this->processFileAttachment
        ->process($data['@attachment'], 'base64_stream', $workspace);
      unset($data['@attachment']);
    }
    return parent::denormalize($data, $class, $format, $context);
  }
  public function supportsDenormalization($data, $type, $format = NULL) {

    // We need to accept both FileInterface and ContentEntityInterface classes.
    // File entities are treated as standard content entities.
    if (in_array($type, [
      'Drupal\\Core\\Entity\\ContentEntityInterface',
      'Drupal\\file\\FileInterface',
    ], true)) {

      // If a document has _attachment then we assume it's a file entity.
      if (!empty($data['@attachment'])) {
        return true;
      }
    }
    return false;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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
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::$format protected property Overrides NormalizerBase::$format
FileEntityNormalizer::$processFileAttachment protected property
FileEntityNormalizer::$supportedInterfaceOrClass protected property Overrides ContentEntityNormalizer::$supportedInterfaceOrClass
FileEntityNormalizer::denormalize public function Denormalizes data back into an object of the given class. Overrides ContentEntityNormalizer::denormalize
FileEntityNormalizer::normalize public function Normalizes an object into a set of arrays/scalars. Overrides ContentEntityNormalizer::normalize
FileEntityNormalizer::supportsDenormalization public function Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization() Overrides NormalizerBase::supportsDenormalization
FileEntityNormalizer::__construct public function Overrides ContentEntityNormalizer::__construct
NormalizerBase::addCacheableDependency protected function Adds cacheability if applicable.
NormalizerBase::checkFormat protected function Checks if the provided format is supported by this normalizer. 2
NormalizerBase::supportsNormalization public function Checks whether the given class is supported for normalization by this normalizer. 1