Newer
Older
<?php
namespace Drupal\migrate_file\Plugin\migrate\process;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Plugin\MigrateProcessInterface;
use Drupal\migrate\Row;
use Drupal\file\Entity\File;
use Symfony\Component\DependencyInjection\ContainerInterface;
use GuzzleHttp\Exception\ClientException;
/**
* 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.
*
* @see Drupal\migrate\Plugin\migrate\process\Get
*
* @see Drupal\migrate_file\Plugin\migrate\process\FileImport.php
*
* Example:
*
* @code
* 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:
* uid:
* plugin: default_value
* default_value: 1
* field_image:
* plugin: image_import
* source: image
* destination: constants/file_destination
* uid: @uid
* title: title
* alt: !file
* skip_on_missing_source: true
*
*
* @endcode
*
* @see \Drupal\migrate\Plugin\MigrateProcessInterface
*
* @MigrateProcessPlugin(
* id = "image_import"
* )
*/
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) {
// 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;
}
}
}