class ProcessFileAttachment in Replication 8
Same name and namespace in other branches
- 8.2 src/ProcessFileAttachment.php \Drupal\replication\ProcessFileAttachment
Hierarchy
- class \Drupal\replication\ProcessFileAttachment
Expanded class hierarchy of ProcessFileAttachment
1 file declares its use of ProcessFileAttachment
- FileEntityNormalizer.php in src/
Normalizer/ FileEntityNormalizer.php
1 string reference to 'ProcessFileAttachment'
1 service uses ProcessFileAttachment
File
- src/
ProcessFileAttachment.php, line 11
Namespace
Drupal\replicationView source
class ProcessFileAttachment {
/** @var \Drupal\Core\Session\AccountProxyInterface */
protected $current_user;
/** @var \Drupal\Core\Entity\EntityTypeManagerInterface */
protected $entity_type_manager;
/** @var \Drupal\multiversion\Entity\Index\MultiversionIndexFactory */
protected $index_factory;
function __construct(AccountProxyInterface $current_user, EntityTypeManagerInterface $entity_type_manager, MultiversionIndexFactory $index_factory) {
$this->current_user = $current_user;
$this->entity_type_manager = $entity_type_manager;
$this->index_factory = $index_factory;
}
/**
* Processes a file attachment.
*
* Returns the file object or NULL if it can't be created.
*
* @param array $data
* @param string $format
* @param \Drupal\multiversion\Entity\WorkspaceInterface $workspace
*
* @return \Drupal\file\FileInterface|NULL
*/
public function process($data, $format, WorkspaceInterface $workspace = null) {
$current_user_id = $this->current_user
->id();
$uri = $data['uri'];
$file_uuid = $data['uuid'];
multiversion_prepare_file_destination($uri);
// Check if exists a file entity with this uuid.
$uuid_index = $this->index_factory
->get('multiversion.entity_index.uuid', $workspace);
$entity_info = $uuid_index
->get($file_uuid);
if (!empty($entity_info)) {
/** @var FileInterface $file */
$file = $this->entity_type_manager
->getStorage($entity_info['entity_type_id'])
->load($entity_info['entity_id']);
if (!$file || !is_file($file
->getFileUri())) {
$file_context = [
'uri' => $uri,
'uuid' => $file_uuid,
'status' => FILE_STATUS_PERMANENT,
'uid' => $current_user_id,
];
if ($workspace) {
$file_context['workspace'] = $workspace;
}
$file = \Drupal::getContainer()
->get('serializer')
->deserialize($data['data'], '\\Drupal\\file\\FileInterface', $format, $file_context);
}
return $file;
}
// Create the new entity file and the file itself.
// Check if exists a file with this $uri, if it exists then rename the file.
$existing_files = $this->entity_type_manager
->getStorage('file')
->loadByProperties([
'uri' => $uri,
]);
if (count($existing_files)) {
$uri = file_destination($uri, FILE_EXISTS_RENAME);
}
$file_context = [
'uri' => $uri,
'uuid' => $file_uuid,
'status' => FILE_STATUS_PERMANENT,
'uid' => $current_user_id,
'workspace' => $workspace,
];
$file = \Drupal::getContainer()
->get('serializer')
->deserialize($data['data'], '\\Drupal\\file\\FileInterface', $format, $file_context);
return $file;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ProcessFileAttachment:: |
protected | property | @var \Drupal\Core\Session\AccountProxyInterface | |
ProcessFileAttachment:: |
protected | property | @var \Drupal\Core\Entity\EntityTypeManagerInterface | |
ProcessFileAttachment:: |
protected | property | @var \Drupal\multiversion\Entity\Index\MultiversionIndexFactory | |
ProcessFileAttachment:: |
public | function | Processes a file attachment. | |
ProcessFileAttachment:: |
function |