|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\dgi_migrate\Plugin\migrate\process; |
| 4 | + |
| 5 | +use Drupal\Component\Transliteration\TransliterationInterface; |
| 6 | +use Drupal\Core\Language\LanguageInterface; |
| 7 | +use Drupal\migrate\MigrateExecutableInterface; |
| 8 | +use Drupal\migrate\Plugin\MigrateProcessInterface; |
| 9 | +use Drupal\migrate\Plugin\MigrationInterface; |
| 10 | +use Drupal\migrate\ProcessPluginBase; |
| 11 | +use Drupal\migrate\Row; |
| 12 | +use Symfony\Component\DependencyInjection\ContainerInterface; |
| 13 | + |
| 14 | +/** |
| 15 | + * General FOXML file handling plugin. |
| 16 | + * |
| 17 | + * Accepts: |
| 18 | + * - method: One of "copy" (to copy the file) or "direct" (to directly use the |
| 19 | + * file). Defaults to "copy". Can be set with the DGI_MIGRATE_FILE_METHOD |
| 20 | + * environment variable. |
| 21 | + * - pid: Property in the row containing the PID, to build out a destination if |
| 22 | + * copying. |
| 23 | + * - destination: Property in the row containing the destination, to build out |
| 24 | + * a destination path if copying. |
| 25 | + * - date: Property in the row containing a date with which to build a path in |
| 26 | + * the destination, when copying. |
| 27 | + * - mimetype; Property in the row containing the MIME type of the file, to |
| 28 | + * determine an extension. |
| 29 | + * |
| 30 | + * @MigrateProcessPlugin( |
| 31 | + * id = "dgi_migrate.foxml_file" |
| 32 | + * ) |
| 33 | + */ |
| 34 | +class FoxmlFile extends ProcessPluginBase { |
| 35 | + |
| 36 | + /** |
| 37 | + * Constructor. |
| 38 | + */ |
| 39 | + public function __construct( |
| 40 | + array $configuration, |
| 41 | + $plugin_id, |
| 42 | + $plugin_definition, |
| 43 | + protected MigrationInterface $migration, |
| 44 | + protected TransliterationInterface $transliteration, |
| 45 | + protected MigrateProcessInterface $naiveCopyPlugin, |
| 46 | + protected MigrateProcessInterface $extensionPlugin, |
| 47 | + ) { |
| 48 | + parent::__construct($configuration, $plugin_id, $plugin_definition); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * {@inheritDoc} |
| 53 | + */ |
| 54 | + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) { |
| 55 | + $configuration['method'] ??= getenv('DGI_MIGRATE_FILE_METHOD') ?: 'copy'; |
| 56 | + assert(in_array($configuration['method'], ['copy', 'direct'])); |
| 57 | + /** @var \Drupal\migrate\Plugin\MigratePluginManagerInterface $process_plugin_manager */ |
| 58 | + $process_plugin_manager = $container->get('plugin.manager.migrate.process'); |
| 59 | + return new static( |
| 60 | + $configuration, |
| 61 | + $plugin_id, |
| 62 | + $plugin_definition, |
| 63 | + $migration, |
| 64 | + $container->get('transliteration'), |
| 65 | + $process_plugin_manager->createInstance('dgi_migrate.naive_file_copy'), |
| 66 | + $process_plugin_manager->createInstance('dgi_migrate.process.extension_from_mimetype'), |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * {@inheritDoc} |
| 72 | + */ |
| 73 | + public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { |
| 74 | + return match ($this->configuration['method']) { |
| 75 | + 'copy' => $this->naiveCopyPlugin->transform([ |
| 76 | + $value, |
| 77 | + $this->getDestinationPath($migrate_executable, $row, $destination_property), |
| 78 | + ], $migrate_executable, $row, $destination_property), |
| 79 | + 'direct' => $value, |
| 80 | + }; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Build out path. |
| 85 | + */ |
| 86 | + protected function getDestinationPath(MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) : string { |
| 87 | + $pid = $row->get($this->configuration['pid']); |
| 88 | + // Adapted from https://api.drupal.org/api/drupal/core%21modules%21migrate%21src%21Plugin%21migrate%21process%21MachineName.php/class/MachineName/10 |
| 89 | + $safe_pid = preg_replace( |
| 90 | + '/_+/', |
| 91 | + '_', |
| 92 | + preg_replace( |
| 93 | + '/[^a-z0-9_]+/', |
| 94 | + '_', |
| 95 | + strtolower( |
| 96 | + $this->transliteration->transliterate($pid, LanguageInterface::LANGCODE_DEFAULT, '_'), |
| 97 | + ), |
| 98 | + ) |
| 99 | + ); |
| 100 | + |
| 101 | + $dest_dir = $row->get($this->configuration['destination']); |
| 102 | + $path = date('Y-m', $row->get($this->configuration['date'])); |
| 103 | + $ext = $this->extensionPlugin->transform($row->get($this->configuration['mimetype']), $migrate_executable, $row, $destination_property); |
| 104 | + |
| 105 | + return "{$dest_dir}/{$path}/{$safe_pid}.{$ext}"; |
| 106 | + } |
| 107 | + |
| 108 | +} |
0 commit comments