View source
<?php
namespace Drupal\panopoly_core\Plugin\migrate\process;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Image\ImageFactory;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\file\FileStorageInterface;
use Drupal\migrate\MigrateException;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
use Symfony\Component\DependencyInjection\ContainerInterface;
class DemoImage extends ProcessPluginBase implements ContainerFactoryPluginInterface {
protected $migration;
protected $fileStorage;
protected $moduleHandler;
protected $filesystem;
protected $imageFactory;
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, FileStorageInterface $file_storage, ModuleHandlerInterface $module_handler, FileSystemInterface $filesystem, ImageFactory $image_factory) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->migration = $migration;
$this->fileStorage = $file_storage;
$this->moduleHandler = $module_handler;
$this->filesystem = $filesystem;
$this->imageFactory = $image_factory;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
return new static($configuration, $plugin_id, $plugin_definition, $migration, $container
->get('entity_type.manager')
->getStorage('file'), $container
->get('module_handler'), $container
->get('file_system'), $container
->get('image.factory'));
}
protected function removeTrailing($path) {
return rtrim($path, '/ ');
}
protected function getSourcePath() {
$module_path = $this->moduleHandler
->getModule($this->migration
->getPluginDefinition()['provider'])
->getPath();
$relative_path = !empty($this->configuration['source_path']) ? $this->configuration['source_path'] : 'import/images';
return $this
->removeTrailing($module_path . '/' . $relative_path);
}
protected function getDestinationPath() {
if (!empty($this->configuration['destination_path'])) {
return $this
->removeTrailing($this->configuration['destination_path']);
}
return 'public://demo';
}
protected function findExistingFile($uri) {
$files = $this->fileStorage
->loadByProperties([
'uri' => $uri,
]);
if (!empty($files)) {
return reset($files);
}
return NULL;
}
protected function copyFile($source_path, $destination_path) {
$dir = $this->filesystem
->dirname($destination_path);
if (substr($dir, -3) == '://') {
$dir = $this->filesystem
->realpath($dir);
}
if ($this->filesystem
->prepareDirectory($dir, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS)) {
return @copy($source_path, $destination_path);
}
return FALSE;
}
protected function createFile($source_path, $destination_path) {
if (!$this
->copyFile($source_path, $destination_path)) {
throw new MigrateException("Unable to copy {$source_path} to {$destination_path}");
}
$file = $this->fileStorage
->create([
'uri' => $destination_path,
'status' => TRUE,
]);
$file
->save();
return $file;
}
protected function findOrCreateFile($source_path, $destination_path) {
$file = $this
->findExistingFile($destination_path);
if (!$file) {
$file = $this
->createFile($source_path, $destination_path);
}
return $file;
}
protected function checkFile($path) {
return file_exists($path);
}
protected function getOptionalProperty($config_name, Row $row) {
if (!empty($this->configuration[$config_name])) {
return $row
->getSourceProperty($this->configuration[$config_name]);
}
}
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
if (empty($this->configuration['filename_property'])) {
return [];
}
$filename = $row
->getSourceProperty($this->configuration['filename_property']);
$destination_path = $this
->getDestinationPath() . '/' . $filename;
$source_path = $this
->getSourcePath() . '/' . $filename;
if (!$this
->checkFile($source_path)) {
throw new MigrateException('Cannot find source image: ' . $source_path);
}
$file = $this
->findOrCreateFile($source_path, $destination_path);
$image = $this->imageFactory
->get($destination_path);
return [
'target_id' => $file
->id(),
'display' => 1,
'description' => $this
->getOptionalProperty('description_property', $row) ?: '',
'alt' => $this
->getOptionalProperty('alt_property', $row) ?: '',
'title' => $this
->getOptionalProperty('title_property', $row) ?: '',
'width' => $image
->getWidth(),
'height' => $image
->getHeight(),
];
}
}