class FileBlob in Migrate Plus 8.5
Same name and namespace in other branches
- 8.2 src/Plugin/migrate/process/FileBlob.php \Drupal\migrate_plus\Plugin\migrate\process\FileBlob
- 8.3 src/Plugin/migrate/process/FileBlob.php \Drupal\migrate_plus\Plugin\migrate\process\FileBlob
- 8.4 src/Plugin/migrate/process/FileBlob.php \Drupal\migrate_plus\Plugin\migrate\process\FileBlob
Copy a file from a blob into a file.
The source value is an indexed array of two values:
- The destination URI, e.g. 'public://example.txt'.
- The binary blob data.
Available configuration keys:
- reuse: true
@codingStandardsIgnoreStart
Examples:
uri:
plugin: file_blob
source:
- 'public://example.txt'
- blob
Above, a basic configuration.
source:
constants:
destination: public://images
process:
destination_blob:
plugin: callback
callable: base64_decode
source:
- blob
destination_basename:
plugin: callback
callable: basename
source: file_name
destination_path:
plugin: concat
source:
- constants/destination
- @destination_basename
uri:
plugin: file_blob
source:
- @destination_path
- @destination_blob
In the example above, it is necessary to manipulate the values before they are processed by this plugin. This is because this plugin takes a binary blob and saves it as a file. In many cases, as in this example, the data is base64 encoded and should be decoded first. In destination_blob, the incoming data is decoded from base64 to binary. The destination_path element is concatenating the base filename with the destination directory set in the constants to create the final path. The resulting values are then referenced as the source of the file_blob plugin.
@codingStandardsIgnoreEnd
Plugin annotation
@MigrateProcessPlugin(
id = "file_blob"
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\migrate\ProcessPluginBase implements MigrateProcessInterface
- class \Drupal\migrate_plus\Plugin\migrate\process\FileBlob implements ContainerFactoryPluginInterface
- class \Drupal\migrate\ProcessPluginBase implements MigrateProcessInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of FileBlob
File
- src/
Plugin/ migrate/ process/ FileBlob.php, line 75
Namespace
Drupal\migrate_plus\Plugin\migrate\processView source
class FileBlob extends ProcessPluginBase implements ContainerFactoryPluginInterface {
/**
* The file system service.
*
* @var \Drupal\Core\File\FileSystemInterface
*/
protected $fileSystem;
/**
* Constructs a file_blob process plugin.
*
* @param array $configuration
* The plugin configuration.
* @param string $plugin_id
* The plugin ID.
* @param mixed $plugin_definition
* The plugin definition.
* @param \Drupal\Core\File\FileSystemInterface $file_system
* The file system service.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, FileSystemInterface $file_system) {
$configuration += [
'reuse' => FALSE,
];
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->fileSystem = $file_system;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('file_system'));
}
/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
// If we're stubbing a file entity, return a URI of NULL so it will get
// stubbed by the general process.
if ($row
->isStub()) {
return NULL;
}
[
$destination,
$blob,
] = $value;
// Determine if we going to overwrite existing files or not touch them.
$replace = $this
->getOverwriteMode();
// Attempt to save the file to avoid calling file_prepare_directory() any
// more than absolutely necessary.
if ($this
->putFile($destination, $blob, $replace)) {
return $destination;
}
$dir = $this
->getDirectory($destination);
$success = $this->fileSystem
->prepareDirectory($dir, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
if (!$success) {
throw new MigrateSkipProcessException("Could not create directory '{$dir}'");
}
if ($this
->putFile($destination, $blob, $replace)) {
return $destination;
}
throw new MigrateSkipProcessException("Blob data could not be copied to {$destination}.");
}
/**
* Try to save the file.
*
* @param string $destination
* The destination path or URI.
* @param string $blob
* The base64 encoded file contents.
* @param int $replace
* (optional) either FileSystemInterface::EXISTS_REPLACE; (default) or
* FileSystemInterface::EXISTS_ERROR, depending on the configuration.
*
* @return bool|string
* File path on success, FALSE on failure.
*/
protected function putFile($destination, $blob, $replace = FileSystemInterface::EXISTS_REPLACE) {
$path = $this->fileSystem
->getDestinationFilename($destination, $replace);
if ($path) {
if (file_put_contents($path, $blob)) {
return $path;
}
else {
return FALSE;
}
}
// File was already copied.
return $destination;
}
/**
* Determines how to handle file conflicts.
*
* @return int
* Either FileSystemInterface::EXISTS_REPLACE; (default) or
* FileSystemInterface::EXISTS_ERROR, depending on the configuration.
*/
protected function getOverwriteMode() {
if (isset($this->configuration['reuse']) && !empty($this->configuration['reuse'])) {
return FileSystemInterface::EXISTS_ERROR;
}
return FileSystemInterface::EXISTS_REPLACE;
}
/**
* Returns the directory component of a URI or path.
*
* For URIs like public://foo.txt, the full physical path of public://
* will be returned, since a scheme by itself will trip up certain file
* API functions (such as file_prepare_directory()).
*
* @param string $uri
* The URI or path.
*
* @return string|false
* The directory component of the path or URI, or FALSE if it could not
* be determined.
*/
protected function getDirectory($uri) {
$dir = $this->fileSystem
->dirname($uri);
if (substr($dir, -3) == '://') {
return $this->fileSystem
->realpath($dir);
}
return $dir;
}
}
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 | |
FileBlob:: |
protected | property | The file system service. | |
FileBlob:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
FileBlob:: |
protected | function | Returns the directory component of a URI or path. | |
FileBlob:: |
protected | function | Determines how to handle file conflicts. | |
FileBlob:: |
protected | function | Try to save the file. | |
FileBlob:: |
public | function |
Performs the associated process. Overrides ProcessPluginBase:: |
|
FileBlob:: |
public | function |
Constructs a file_blob process plugin. Overrides PluginBase:: |
|
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
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. | |
ProcessPluginBase:: |
public | function |
Indicates whether the returned value requires multiple handling. Overrides MigrateProcessInterface:: |
3 |
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. |