You are here

class ImageImport in Migrate Files (extended) 8

Same name and namespace in other branches
  1. 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

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\process
View 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

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FileCopy::$downloadPlugin protected property An instance of the download process plugin.
FileCopy::$fileSystem protected property The file system service.
FileCopy::$streamWrapperManager protected property The stream wrapper manager service.
FileCopy::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
FileCopy::getDirectory protected function Returns the directory component of a URI or path.
FileCopy::isLocalUri protected function Determines if the given URI or path is considered local.
FileCopy::isLocationUnchanged protected function Determines if the source and destination URIs represent identical paths.
FileCopy::writeFile protected function Tries to move or copy a file.
FileImport::getDestinationFilePath protected function Build the destination filename.
FileImport::getOverwriteMode protected function Determines how to handle file conflicts.
FileImport::getPropertyValue protected function Gets a value from a source or destination property.
FileImport::isDirectory protected function Check if a path is a meant to be a directory.
FileImport::sourceExists protected function Check if a source exists.
ImageImport::transform public function Performs the associated process. Overrides FileImport::transform
ImageImport::__construct public function Constructs a file_copy process plugin. Overrides FileImport::__construct
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
ProcessPluginBase::multiple public function Indicates whether the returned value requires multiple handling. Overrides MigrateProcessInterface::multiple 3
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.