public function JsonapiHelper::handlePhysicalFiles in Entity Share 8.2
Same name and namespace in other branches
- 8 modules/entity_share_client/src/Service/JsonapiHelper.php \Drupal\entity_share_client\Service\JsonapiHelper::handlePhysicalFiles()
Create or update the entity reference field values of an entity.
Parameters
\Drupal\Core\Entity\ContentEntityInterface $entity: The entity to update.
array $data: An array of data. Can be modified to change the URI if needed.
Overrides JsonapiHelperInterface::handlePhysicalFiles
1 call to JsonapiHelper::handlePhysicalFiles()
- JsonapiHelper::importEntityListData in modules/
entity_share_client/ src/ Service/ JsonapiHelper.php - Use data from the JSON:API to import content.
File
- modules/
entity_share_client/ src/ Service/ JsonapiHelper.php, line 364
Class
- JsonapiHelper
- Class JsonapiHelper.
Namespace
Drupal\entity_share_client\ServiceCode
public function handlePhysicalFiles(ContentEntityInterface $entity, array &$data) {
if ($entity instanceof FileInterface) {
$resource_type = $this->resourceTypeRepository
->get($entity
->getEntityTypeId(), $entity
->bundle());
$uri_public_name = $resource_type
->getPublicName('uri');
$remote_uri = $data['attributes'][$uri_public_name]['value'];
$remote_url = $data['attributes'][$uri_public_name]['url'];
$stream_wrapper = $this->streamWrapperManager
->getViaUri($remote_uri);
$directory_uri = $stream_wrapper
->dirname($remote_uri);
$log_variables = [
'%url' => $remote_url,
'%directory' => $directory_uri,
'%id' => $entity
->id(),
'%uri' => $remote_uri,
];
// Create the destination folder.
if ($this->fileSystem
->prepareDirectory($directory_uri, FileSystemInterface::CREATE_DIRECTORY)) {
try {
$response = $this->requestService
->request($this
->getFileHttpClient(), 'GET', $remote_url);
$file_content = (string) $response
->getBody();
$result = @file_put_contents($remote_uri, $file_content);
if (!$result) {
throw new \Exception('Error writing file to ' . $remote_uri);
}
} catch (ClientException $e) {
$this->messenger
->addWarning($this
->t('Error importing file id %id. Missing file: %url', $log_variables));
$this->logger
->warning('Error importing file id %id. Missing file: %url', $log_variables);
} catch (\Throwable $e) {
$log_variables['@msg'] = $e
->getMessage();
$this->messenger
->addError($this
->t('Caught exception trying to import the file %url to %uri', $log_variables));
$this->logger
->error('Caught exception trying to import the file %url to %uri. Error message was @msg', $log_variables);
}
}
else {
$this->messenger
->addError($this
->t('Impossible to write in the directory %directory', $log_variables));
$this->logger
->error('Impossible to write in the directory %directory', $log_variables);
}
}
}