class PhysicalFile in Entity Share 8.3
Handle physical file import.
Plugin annotation
@ImportProcessor(
id = "physical_file",
label = @Translation("Physical file"),
description = @Translation("When importing a File entity, also import the physical file."),
stages = {
"process_entity" = 0,
},
locked = false,
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\entity_share_client\ImportProcessor\ImportProcessorPluginBase implements ContainerFactoryPluginInterface, ImportProcessorInterface
- class \Drupal\entity_share_client\Plugin\EntityShareClient\Processor\PhysicalFile implements PluginFormInterface
- class \Drupal\entity_share_client\ImportProcessor\ImportProcessorPluginBase implements ContainerFactoryPluginInterface, ImportProcessorInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of PhysicalFile
File
- modules/
entity_share_client/ src/ Plugin/ EntityShareClient/ Processor/ PhysicalFile.php, line 30
Namespace
Drupal\entity_share_client\Plugin\EntityShareClient\ProcessorView source
class PhysicalFile extends ImportProcessorPluginBase implements PluginFormInterface {
/**
* The stream wrapper manager.
*
* @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface
*/
protected $streamWrapperManager;
/**
* Logger.
*
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
/**
* The remote manager.
*
* @var \Drupal\entity_share_client\Service\RemoteManagerInterface
*/
protected $remoteManager;
/**
* The file system.
*
* @var \Drupal\Core\File\FileSystemInterface
*/
protected $fileSystem;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$instance->streamWrapperManager = $container
->get('stream_wrapper_manager');
$instance->logger = $container
->get('logger.channel.entity_share_client');
$instance->remoteManager = $container
->get('entity_share_client.remote_manager');
$instance->fileSystem = $container
->get('file_system');
return $instance;
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'rename' => FALSE,
] + parent::defaultConfiguration();
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['rename'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Rename imported files with the same name, instead of overwriting'),
'#description' => $this
->t('If a file with the same name exists, the imported file will be saved as filename_0 or filename_1... etc. <strong>Warning! This can make a lot of duplicated files on your websites!</strong>'),
'#default_value' => $this->configuration['rename'],
];
return $form;
}
/**
* {@inheritdoc}
*
* @SuppressWarnings(PHPMD.ErrorControlOperator)
*/
public function processEntity(RuntimeImportContext $runtime_import_context, ContentEntityInterface $processed_entity, array $entity_json_data) {
if ($processed_entity instanceof FileInterface) {
$field_mappings = $runtime_import_context
->getFieldMappings();
$entity_type_id = $processed_entity
->getEntityTypeId();
$entity_bundle = $processed_entity
->bundle();
$uri_public_name = FALSE;
if (isset($field_mappings[$entity_type_id][$entity_bundle]['uri'])) {
$uri_public_name = $field_mappings[$entity_type_id][$entity_bundle]['uri'];
}
if (!$uri_public_name || !isset($entity_json_data['attributes'][$uri_public_name])) {
$this->logger
->error('Impossible to get the URI of the file in JSON:API data. Please check that the server website is correctly exposing it.');
$this
->messenger()
->addError($this
->t('Impossible to get the URI of the file in JSON:API data. Please check that the server website is correctly exposing it.'));
return;
}
$remote_file_uri = $entity_json_data['attributes'][$uri_public_name]['value'];
$remote_file_url = $entity_json_data['attributes'][$uri_public_name]['url'];
$stream_wrapper = $this->streamWrapperManager
->getViaUri($remote_file_uri);
$directory_uri = $stream_wrapper
->dirname($remote_file_uri);
$log_variables = [
'%url' => $remote_file_url,
'%directory' => $directory_uri,
'%id' => $processed_entity
->id(),
'%uri' => $remote_file_uri,
];
$file_overwrite_mode = $this->configuration['rename'] ? FileSystemInterface::EXISTS_RENAME : FileSystemInterface::EXISTS_REPLACE;
$file_destination = $this->fileSystem
->getDestinationFilename($processed_entity
->getFileUri(), $file_overwrite_mode);
$processed_entity
->setFileUri($file_destination);
$processed_entity
->setFilename($this->fileSystem
->basename($file_destination));
// Create the destination folder.
if ($this->fileSystem
->prepareDirectory($directory_uri, FileSystemInterface::CREATE_DIRECTORY)) {
try {
$response = $this->remoteManager
->request($runtime_import_context
->getRemote(), 'GET', $remote_file_url);
$file_content = (string) $response
->getBody();
$result = @file_put_contents($file_destination, $file_content);
if (!$result) {
throw new \Exception('Error writing file to ' . $file_destination);
}
} catch (ClientException $e) {
$this->logger
->warning('Error importing file id %id. Missing file: %url', $log_variables);
$this
->messenger()
->addWarning($this
->t('Error importing file id %id. Missing file: %url', $log_variables));
} catch (\Throwable $e) {
$log_variables['@msg'] = $e
->getMessage();
$this->logger
->error('Caught exception trying to import the file %url to %uri. Error message was @msg', $log_variables);
$this
->messenger()
->addError($this
->t('Caught exception trying to import the file %url to %uri', $log_variables));
}
}
else {
$this->logger
->error('Impossible to write in the directory %directory', $log_variables);
$this
->messenger()
->addError($this
->t('Impossible to write in the directory %directory', $log_variables));
}
}
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
ImportProcessorInterface:: |
constant | Processing stage: is entity importable. | ||
ImportProcessorInterface:: |
constant | Processing stage: post entity save. | ||
ImportProcessorInterface:: |
constant | Processing stage: prepare entity data. | ||
ImportProcessorInterface:: |
constant | Processing stage: prepare importable entity data. | ||
ImportProcessorInterface:: |
constant | Processing stage: process entity. | ||
ImportProcessorPluginBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
ImportProcessorPluginBase:: |
public | function |
Returns the plugin's description. Overrides ImportProcessorInterface:: |
|
ImportProcessorPluginBase:: |
public | function |
Returns the weight for a specific processing stage. Overrides ImportProcessorInterface:: |
|
ImportProcessorPluginBase:: |
public | function |
Method called on STAGE_IS_ENTITY_IMPORTABLE. Overrides ImportProcessorInterface:: |
2 |
ImportProcessorPluginBase:: |
public | function |
Determines whether this processor should always be enabled. Overrides ImportProcessorInterface:: |
|
ImportProcessorPluginBase:: |
public | function |
Returns the label for use on the administration pages. Overrides ImportProcessorInterface:: |
|
ImportProcessorPluginBase:: |
public | function |
Method called on STAGE_POST_ENTITY_SAVE. Overrides ImportProcessorInterface:: |
1 |
ImportProcessorPluginBase:: |
public | function |
Method called on STAGE_PREPARE_ENTITY_DATA. Overrides ImportProcessorInterface:: |
|
ImportProcessorPluginBase:: |
public | function |
Method called on STAGE_PREPARE_IMPORTABLE_ENTITY_DATA. Overrides ImportProcessorInterface:: |
4 |
ImportProcessorPluginBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
ImportProcessorPluginBase:: |
public | function |
Sets the weight for a specific processing stage. Overrides ImportProcessorInterface:: |
|
ImportProcessorPluginBase:: |
public | function | Form submission handler. | |
ImportProcessorPluginBase:: |
public | function |
Checks whether this processor implements a particular stage. Overrides ImportProcessorInterface:: |
|
ImportProcessorPluginBase:: |
public | function | Form validation handler. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PhysicalFile:: |
protected | property | The file system. | |
PhysicalFile:: |
protected | property | Logger. | |
PhysicalFile:: |
protected | property | The remote manager. | |
PhysicalFile:: |
protected | property | The stream wrapper manager. | |
PhysicalFile:: |
public | function |
Form constructor. Overrides PluginFormInterface:: |
|
PhysicalFile:: |
public static | function |
Creates an instance of the plugin. Overrides ImportProcessorPluginBase:: |
|
PhysicalFile:: |
public | function |
Gets default configuration for this plugin. Overrides ImportProcessorPluginBase:: |
|
PhysicalFile:: |
public | function |
Plugin annotation
@SuppressWarnings(PHPMD . ErrorControlOperator); Overrides ImportProcessorPluginBase:: |
|
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 92 |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |