View source
<?php
namespace Drupal\replication\Normalizer;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\file\FileInterface;
use Drupal\multiversion\Entity\Index\MultiversionIndexFactory;
use Drupal\replication\ProcessFileAttachment;
use Drupal\replication\UsersMapping;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
class FileEntityNormalizer extends ContentEntityNormalizer implements DenormalizerInterface {
protected $supportedInterfaceOrClass = [
'Drupal\\file\\FileInterface',
];
protected $format = [
'json',
];
protected $processFileAttachment;
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;
}
public function normalize($data, $format = NULL, array $context = []) {
$normalized = parent::normalize($data, $format, $context);
$file_system = \Drupal::service('file_system');
$uri = $data
->getFileUri();
if (empty($uri)) {
return $normalized;
}
if ($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);
}
$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;
}
public function denormalize($data, $class, $format = NULL, array $context = []) {
$file = NULL;
if (!empty($data['@attachment']['uuid'])) {
$workspace = isset($context['workspace']) ? $context['workspace'] : NULL;
if ($file = $this->processFileAttachment
->process($data['@attachment'], 'base64_stream', $workspace)) {
$language_code = $file
->language()
->getId();
$data[$language_code]['filename'][0]['value'] = $file
->get('filename')->value;
$data[$language_code]['uri'][0]['value'] = $file
->get('uri')->value;
}
unset($data['@attachment']);
}
return parent::denormalize($data, $class, $format, $context);
}
public function supportsDenormalization($data, $type, $format = NULL) {
if (in_array($type, [
'Drupal\\Core\\Entity\\ContentEntityInterface',
'Drupal\\file\\FileInterface',
], TRUE)) {
if (!empty($data['@attachment'])) {
return TRUE;
}
}
return FALSE;
}
}