diff -u b/src/Plugin/migrate/process/MediaWysiwygFilter.php b/src/Plugin/migrate/process/MediaWysiwygFilter.php --- b/src/Plugin/migrate/process/MediaWysiwygFilter.php +++ b/src/Plugin/migrate/process/MediaWysiwygFilter.php @@ -2,6 +2,7 @@ namespace Drupal\media_migration\Plugin\migrate\process; +use Drupal\file\Entity\File; use Drupal\migrate\MigrateExecutableInterface; use Drupal\migrate\ProcessPluginBase; use Drupal\migrate\Row; @@ -35,6 +36,12 @@ * process: * bar: * plugin: media_wysiwyg_filter + * view_mode_matching: + * default: full + * migrations: + * - upgrade_d7_file_entity_archive + * - upgrade_d7_file_entity_image + * - upgrade_d7_file_entity_publication * @endcode * * @MigrateProcessPlugin( @@ -46,6 +53,16 @@ /** * {@inheritdoc} */ + public function defaultConfiguration() { + return [ + 'view_mode_matching' => [], + 'migrations' => [], + ] + parent::defaultConfiguration(); + } + + /** + * {@inheritdoc} + */ public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { $pattern = '/\[\[(?.+?"type":"media".+?)\]\]/s'; $replacement_template = <<<'TEMPLATE' @@ -62,31 +79,20 @@ try { $tag_info = $decoder->decode($matches['tag_info'], JsonEncoder::FORMAT); - // Find matching view mode - $matching = $this->configuration['matching']; - if (isset($matching)) { - foreach ($matching as $match) { - foreach ($match as $key => $value) { - if ($key == $tag_info['view_mode']) { - $tag_info['view_mode'] = $value; - } + // Find matching view mode. + if ($this->configuration['view_mode_matching']) { + foreach ($this->configuration['view_mode_matching'] as $key => $match) { + if ($key == $tag_info['view_mode']) { + $tag_info['view_mode'] = $match; } } } - //change old id by new one - $lookup_migrations_ids = $this->configuration['migrations']; - if (!is_array($lookup_migrations_ids)) { - $lookup_migrations_ids = [$lookup_migrations_ids]; - } - $migrationPluginManager = \Drupal::service('plugin.manager.migration'); - $lookup_migrations = $migrationPluginManager->createInstances($lookup_migrations_ids); - foreach ($lookup_migrations as $lookup_migration_id => $lookup_migration) { - $source_id_values[$lookup_migration_id] = [$tag_info['fid']]; - $destination_ids = $lookup_migration->getIdMap()->lookupDestinationId($source_id_values[$lookup_migration_id]); - if (!empty($destination_ids)) { - $tag_info['fid'] = $destination_ids[0]; - } + + // Update fid to the new one. + if ($this->configuration['migrations']) { + $tag_info['fid'] = $this->findNewFid($tag_info['fid']); } + return sprintf($replacement_template, $tag_info['view_mode'], $tag_info['fid']); } catch (NotEncodableValueException $e) { @@ -99,5 +105,49 @@ + // Update fid and token in regex /file/{fid}/download?token={token}. + if ($this->configuration['migrations']) { + $pattern = '#\/file\/([0-9]*)\/download\?token=([a-zA-Z0-9]*)#'; + $replacement_template = '/file/%s/download?token=%s'; + $value['value'] = preg_replace_callback($pattern, function ($matches) use ($replacement_template) { + $oldFid = $matches[1]; + $newFid = $this->findNewFid($oldFid); + + $file = File::load($newFid); + $newToken = $file->getDownloadToken(); + + return sprintf($replacement_template, $newFid, $newToken); + }, $value['value']); + } + return $value; } + /** + * Find new fid using the migration lookup system. + * + * @param integer $oldFid + * The old fid. + * + * @return integer + * The new fid. + */ + private function findNewFid($oldFid) { + $newFid = $oldFid; + + $lookup_migrations_ids = $this->configuration['migrations']; + + $migrationPluginManager = \Drupal::service('plugin.manager.migration'); + $lookup_migrations = $migrationPluginManager->createInstances($lookup_migrations_ids); + + foreach ($lookup_migrations as $lookup_migration_id => $lookup_migration) { + $source_id_values[$lookup_migration_id] = [$oldFid]; + $destination_ids = $lookup_migration->getIdMap()->lookupDestinationId($source_id_values[$lookup_migration_id]); + + if (!empty($destination_ids)) { + $newFid = $destination_ids[0]; + } + } + + return $newFid; + } + }