Created
March 21, 2019 10:48
-
-
Save 5n00p4eg/fff18b3e7db396bb19aff2583c6c97f0 to your computer and use it in GitHub Desktop.
Drupal 8 migrate menu links with node references
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Drupal\oht_migrate\Plugin\migrate\process; | |
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; | |
use Drupal\migrate\Annotation\MigrateProcessPlugin; | |
use Drupal\migrate\Plugin\migrate\process\MigrationLookup; | |
use Drupal\migrate\Plugin\MigratePluginManagerInterface; | |
use Drupal\migrate\Plugin\MigrationInterface; | |
use Drupal\migrate\ProcessPluginBase; | |
use Drupal\migrate\MigrateExecutableInterface; | |
use Drupal\migrate\Row; | |
use Symfony\Component\DependencyInjection\ContainerInterface; | |
/** | |
* Provides a 'MigratedPathLookup' migrate process plugin. | |
* | |
* @MigrateProcessPlugin( | |
* id = "migrated_path_lookup" | |
* ) | |
*/ | |
class MigratedPathLookup extends ProcessPluginBase implements ContainerFactoryPluginInterface { | |
/** | |
* The migration plugin manager. | |
* | |
* @var \Drupal\migrate\Plugin\MigratePluginManagerInterface | |
*/ | |
protected $migrationPluginManager; | |
/** | |
* The migration to be executed. | |
* | |
* @var \Drupal\migrate\Plugin\MigrationInterface | |
*/ | |
protected $migration; | |
/** | |
* Constructs a MigrationLookup object. | |
* | |
* @param array $configuration | |
* A configuration array containing information about the plugin instance. | |
* @param string $plugin_id | |
* The plugin_id for the plugin instance. | |
* @param mixed $plugin_definition | |
* The plugin implementation definition. | |
* @param \Drupal\migrate\Plugin\MigrationInterface $migration | |
* The Migration the plugin is being used in. | |
* @param \Drupal\migrate\Plugin\MigratePluginManagerInterface $migration_plugin_manager | |
* The Migration Plugin Manager Interface. | |
*/ | |
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, MigratePluginManagerInterface $migration_plugin_manager) { | |
parent::__construct($configuration, $plugin_id, $plugin_definition); | |
$this->migrationPluginManager = $migration_plugin_manager; | |
$this->migration = $migration; | |
} | |
/** | |
* Creates an instance of the plugin. | |
* | |
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container | |
* The container to pull out services used in the plugin. | |
* @param array $configuration | |
* A configuration array containing information about the plugin instance. | |
* @param string $plugin_id | |
* The plugin ID for the plugin instance. | |
* @param mixed $plugin_definition | |
* The plugin implementation definition. | |
* | |
* @return static | |
* Returns an instance of this plugin. | |
*/ | |
/** | |
* {@inheritdoc} | |
*/ | |
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('plugin.manager.migrate.process') | |
); | |
} | |
/** | |
* {@inheritdoc} | |
* @throws \Drupal\migrate\MigrateSkipProcessException | |
* @throws \Drupal\Component\Plugin\Exception\PluginException | |
*/ | |
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { | |
$matches = []; | |
if (preg_match('/^(node|taxonomy\/term)\/(\d+)$/', $value, $matches)) { | |
$id = $matches[2]; | |
$base_path = $matches[1]; | |
switch ($base_path) { | |
case 'taxonomy/term': | |
$migration_ids = [ | |
'oht_taxonomy_term' | |
]; | |
break; | |
case 'node': | |
$migration_ids = [ | |
'oht_node_api', | |
'oht_node_article', | |
]; | |
break; | |
default: | |
$migration_ids = []; | |
} | |
$config = [ | |
'migration' => $migration_ids, | |
]; | |
/** @var MigrationLookup|bool $migration_lookup */ | |
$migration_lookup = $this->migrationPluginManager->createInstance('migration_lookup', $config, $this->migration); | |
if ($migration_lookup) { | |
$migrated_id = $migration_lookup->transform($id, $migrate_executable, $row, $destination_property); | |
if ($migrated_id) { | |
$value = $base_path . '/' . $migrated_id; | |
} | |
} | |
} | |
return $value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I found this really useful, but in case anyone runs into difficulties, the following:
Should be:
This is because the link_uri process plugin expects a string and not an array.