class ImageImport in Migrate Files (extended) 8
Same name and namespace in other branches
- 2.0.x src/Plugin/migrate/process/ImageImport.php \Drupal\migrate_file\Plugin\migrate\process\ImageImport
Imports an image from an local or external source.
Extends the regular file_import plugin but adds the following additional optional configuration keys.
- alt: The alt attribute for the image
- title: The title attribute for the image
- width: The width of the image
- height: The height of the image
All of the above fields fields support copying destination values. These are indicated by a starting @ sign. Values using @ must be wrapped in quotes. (the same as it works with the 'source' key).
Additionally, a special value is available to represent the filename of the file '!file'. Useful to just populate the alt or title field with the filename.
Example:
destination:
plugin: entity:node
source:
# assuming we're using a source plugin that lets us define fields like this
fields:
-
name: image
label: 'Main Image'
selector: /image
-
name: title
label: 'Some Title'
selector: /title
constants:
file_destination: 'public://path/to/save/'
process:
title: title
uid:
plugin: default_value
default_value: 1
field_image:
plugin: image_import
source: image
destination: constants/file_destination
uid: @uid
title: title
alt: !file
width: 1920
height: 1080
skip_on_missing_source: true
Plugin annotation
@MigrateProcessPlugin(
id = "image_import"
)
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\Plugin\migrate\process\FileProcessBase
- class \Drupal\migrate\Plugin\migrate\process\FileCopy implements ContainerFactoryPluginInterface
- class \Drupal\migrate_file\Plugin\migrate\process\FileImport
- class \Drupal\migrate_file\Plugin\migrate\process\ImageImport
- class \Drupal\migrate_file\Plugin\migrate\process\FileImport
- class \Drupal\migrate\Plugin\migrate\process\FileCopy implements ContainerFactoryPluginInterface
- class \Drupal\migrate\Plugin\migrate\process\FileProcessBase
- class \Drupal\migrate\ProcessPluginBase implements MigrateProcessInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of ImageImport
See also
Drupal\migrate\Plugin\migrate\process\Get
Drupal\migrate_file\Plugin\migrate\process\FileImport.php
\Drupal\migrate\Plugin\MigrateProcessInterface
File
- src/
Plugin/ migrate/ process/ ImageImport.php, line 79
Namespace
Drupal\migrate_file\Plugin\migrate\processView source
class ImageImport extends FileImport {
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, array $plugin_definition, StreamWrapperManagerInterface $stream_wrappers, FileSystemInterface $file_system, MigrateProcessInterface $download_plugin) {
$configuration += [
'title' => NULL,
'alt' => NULL,
'width' => NULL,
'height' => NULL,
];
parent::__construct($configuration, $plugin_id, $plugin_definition, $stream_wrappers, $file_system, $download_plugin);
}
/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
// Ignore this setting.
$this->configuration['id_only'] = FALSE;
// Run the parent transform to do all the file handling.
$value = parent::transform($value, $migrate_executable, $row, $destination_property);
if ($value && is_array($value)) {
// Add the image field specific sub fields.
foreach ([
'title',
'alt',
'width',
'height',
] as $key) {
if ($property = $this->configuration[$key]) {
if ($property == '!file') {
$file = File::load($value['target_id']);
$value[$key] = $file
->getFilename();
}
else {
$value[$key] = $this
->getPropertyValue($property, $row);
}
}
}
return $value;
}
else {
return NULL;
}
}
}
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 | |
FileCopy:: |
protected | property | An instance of the download process plugin. | |
FileCopy:: |
protected | property | The file system service. | |
FileCopy:: |
protected | property | The stream wrapper manager service. | |
FileCopy:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
FileCopy:: |
protected | function | Returns the directory component of a URI or path. | |
FileCopy:: |
protected | function | Determines if the given URI or path is considered local. | |
FileCopy:: |
protected | function | Determines if the source and destination URIs represent identical paths. | |
FileCopy:: |
protected | function | Tries to move or copy a file. | |
FileImport:: |
protected | function | Build the destination filename. | |
FileImport:: |
protected | function | Determines how to handle file conflicts. | |
FileImport:: |
protected | function | Gets a value from a source or destination property. | |
FileImport:: |
protected | function | Check if a path is a meant to be a directory. | |
FileImport:: |
protected | function | Check if a source exists. | |
ImageImport:: |
public | function |
Performs the associated process. Overrides FileImport:: |
|
ImageImport:: |
public | function |
Constructs a file_copy process plugin. Overrides FileImport:: |
|
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. |