3b907a3607b07de1cd75f8d447da82c01e0153b0
[yaffs-website] / web / core / modules / file / src / Plugin / migrate / process / d6 / FieldFile.php
1 <?php
2
3 namespace Drupal\file\Plugin\migrate\process\d6;
4
5 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
6 use Drupal\migrate\Plugin\MigrationInterface;
7 use Drupal\migrate\MigrateExecutableInterface;
8 use Drupal\migrate\Plugin\MigrateProcessInterface;
9 use Drupal\migrate\ProcessPluginBase;
10 use Drupal\migrate\Row;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12
13 /**
14  * @MigrateProcessPlugin(
15  *   id = "d6_field_file"
16  * )
17  */
18 class FieldFile extends ProcessPluginBase implements ContainerFactoryPluginInterface {
19
20   /**
21    * The migration process plugin, configured for lookups in d6_file.
22    *
23    * @var \Drupal\migrate\Plugin\MigrateProcessInterface
24    */
25   protected $migrationPlugin;
26
27   /**
28    * Constructs a FieldFile plugin instance.
29    *
30    * @param array $configuration
31    *   The plugin configuration.
32    * @param string $plugin_id
33    *   The plugin ID.
34    * @param mixed $plugin_definition
35    *   The plugin definition.
36    * @param \Drupal\migrate\Plugin\MigrationInterface $migration
37    *   The current migration.
38    * @param \Drupal\migrate\Plugin\MigrateProcessInterface $migration_plugin
39    *   An instance of the 'migration' process plugin.
40    */
41   public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, MigrateProcessInterface $migration_plugin) {
42     parent::__construct($configuration, $plugin_id, $plugin_definition);
43     $this->migration = $migration;
44     $this->migrationPlugin = $migration_plugin;
45   }
46
47   /**
48    * {@inheritdoc}
49    */
50   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
51     // Configure the migration process plugin to look up migrated IDs from
52     // a d6 file migration.
53     $migration_plugin_configuration = $configuration + [
54       'migration' => 'd6_file',
55       'source' => ['fid'],
56     ];
57
58     return new static(
59       $configuration,
60       $plugin_id,
61       $plugin_definition,
62       $migration,
63       $container->get('plugin.manager.migrate.process')->createInstance('migration', $migration_plugin_configuration, $migration)
64     );
65   }
66
67   /**
68    * {@inheritdoc}
69    */
70   public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
71     $options = unserialize($value['data']);
72
73     // Try to look up the ID of the migrated file. If one cannot be found, it
74     // means the file referenced by the current field item did not migrate for
75     // some reason -- file migration is notoriously brittle -- and we do NOT
76     // want to send invalid file references into the field system (it causes
77     // fatals), so return an empty item instead.
78     if ($fid = $this->migrationPlugin->transform($value['fid'], $migrate_executable, $row, $destination_property)) {
79       return [
80         'target_id' => $fid,
81         'display' => $value['list'],
82         'description' => isset($options['description']) ? $options['description'] : '',
83         'alt' => isset($options['alt']) ? $options['alt'] : '',
84         'title' => isset($options['title']) ? $options['title'] : '',
85       ];
86     }
87     else {
88       return [];
89     }
90   }
91
92 }