annotate core/modules/node/src/Plugin/migrate/D6NodeDeriver.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\node\Plugin\migrate;
Chris@0 4
Chris@0 5 use Drupal\Component\Plugin\Derivative\DeriverBase;
Chris@0 6 use Drupal\Core\Database\DatabaseExceptionWrapper;
Chris@0 7 use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
Chris@0 8 use Drupal\migrate\Exception\RequirementsException;
Chris@0 9 use Drupal\migrate\Plugin\MigrationDeriverTrait;
Chris@18 10 use Drupal\migrate_drupal\FieldDiscoveryInterface;
Chris@0 11 use Symfony\Component\DependencyInjection\ContainerInterface;
Chris@0 12
Chris@0 13 /**
Chris@0 14 * Deriver for Drupal 6 node and node revision migrations based on node types.
Chris@0 15 */
Chris@0 16 class D6NodeDeriver extends DeriverBase implements ContainerDeriverInterface {
Chris@0 17 use MigrationDeriverTrait;
Chris@0 18
Chris@0 19 /**
Chris@0 20 * The base plugin ID this derivative is for.
Chris@0 21 *
Chris@0 22 * @var string
Chris@0 23 */
Chris@0 24 protected $basePluginId;
Chris@0 25
Chris@0 26 /**
Chris@0 27 * Whether or not to include translations.
Chris@0 28 *
Chris@0 29 * @var bool
Chris@0 30 */
Chris@0 31 protected $includeTranslations;
Chris@0 32
Chris@0 33 /**
Chris@18 34 * The migration field discovery service.
Chris@18 35 *
Chris@18 36 * @var \Drupal\migrate_drupal\FieldDiscoveryInterface
Chris@18 37 */
Chris@18 38 protected $fieldDiscovery;
Chris@18 39
Chris@18 40 /**
Chris@0 41 * D6NodeDeriver constructor.
Chris@0 42 *
Chris@0 43 * @param string $base_plugin_id
Chris@0 44 * The base plugin ID for the plugin ID.
Chris@0 45 * @param bool $translations
Chris@0 46 * Whether or not to include translations.
Chris@18 47 * @param \Drupal\migrate_drupal\FieldDiscoveryInterface $field_discovery
Chris@18 48 * The migration field discovery service.
Chris@0 49 */
Chris@18 50 public function __construct($base_plugin_id, $translations, FieldDiscoveryInterface $field_discovery) {
Chris@0 51 $this->basePluginId = $base_plugin_id;
Chris@0 52 $this->includeTranslations = $translations;
Chris@18 53 $this->fieldDiscovery = $field_discovery;
Chris@0 54 }
Chris@0 55
Chris@0 56 /**
Chris@0 57 * {@inheritdoc}
Chris@0 58 */
Chris@0 59 public static function create(ContainerInterface $container, $base_plugin_id) {
Chris@0 60 // Translations don't make sense unless we have content_translation.
Chris@0 61 return new static(
Chris@0 62 $base_plugin_id,
Chris@18 63 $container->get('module_handler')->moduleExists('content_translation'),
Chris@18 64 $container->get('migrate_drupal.field_discovery')
Chris@0 65 );
Chris@0 66 }
Chris@0 67
Chris@0 68 /**
Chris@18 69 * {@inheritdoc}
Chris@0 70 */
Chris@0 71 public function getDerivativeDefinitions($base_plugin_definition) {
Chris@0 72 if ($base_plugin_definition['id'] == 'd6_node_translation' && !$this->includeTranslations) {
Chris@0 73 // Refuse to generate anything.
Chris@0 74 return $this->derivatives;
Chris@0 75 }
Chris@0 76
Chris@0 77 $node_types = static::getSourcePlugin('d6_node_type');
Chris@0 78 try {
Chris@0 79 $node_types->checkRequirements();
Chris@0 80 }
Chris@0 81 catch (RequirementsException $e) {
Chris@0 82 // If the d6_node_type requirements failed, that means we do not have a
Chris@0 83 // Drupal source database configured - there is nothing to generate.
Chris@0 84 return $this->derivatives;
Chris@0 85 }
Chris@0 86
Chris@0 87 try {
Chris@0 88 foreach ($node_types as $row) {
Chris@0 89 $node_type = $row->getSourceProperty('type');
Chris@0 90 $values = $base_plugin_definition;
Chris@0 91
Chris@0 92 $values['label'] = t("@label (@type)", [
Chris@0 93 '@label' => $values['label'],
Chris@0 94 '@type' => $node_type,
Chris@0 95 ]);
Chris@0 96 $values['source']['node_type'] = $node_type;
Chris@0 97 $values['destination']['default_bundle'] = $node_type;
Chris@0 98
Chris@0 99 // If this migration is based on the d6_node_revision migration or
Chris@0 100 // is for translations of nodes, it should explicitly depend on the
Chris@0 101 // corresponding d6_node variant.
Chris@0 102 if (in_array($base_plugin_definition['id'], ['d6_node_revision', 'd6_node_translation'])) {
Chris@0 103 $values['migration_dependencies']['required'][] = 'd6_node:' . $node_type;
Chris@0 104 }
Chris@0 105
Chris@0 106 /** @var \Drupal\migrate\Plugin\Migration $migration */
Chris@0 107 $migration = \Drupal::service('plugin.manager.migration')->createStubMigration($values);
Chris@18 108 $this->fieldDiscovery->addBundleFieldProcesses($migration, 'node', $node_type);
Chris@0 109 $this->derivatives[$node_type] = $migration->getPluginDefinition();
Chris@0 110 }
Chris@0 111 }
Chris@0 112 catch (DatabaseExceptionWrapper $e) {
Chris@0 113 // Once we begin iterating the source plugin it is possible that the
Chris@0 114 // source tables will not exist. This can happen when the
Chris@0 115 // MigrationPluginManager gathers up the migration definitions but we do
Chris@0 116 // not actually have a Drupal 6 source database.
Chris@0 117 }
Chris@0 118
Chris@0 119 return $this->derivatives;
Chris@0 120 }
Chris@0 121
Chris@0 122 }