annotate core/modules/node/src/Plugin/migrate/D7NodeDeriver.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 7 node and node revision migrations based on node types.
Chris@0 15 */
Chris@0 16 class D7NodeDeriver 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 * D7NodeDeriver 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@0 69 * {@inheritdoc}
Chris@0 70 */
Chris@0 71 public function getDerivativeDefinitions($base_plugin_definition) {
Chris@0 72 if (in_array('translation', $base_plugin_definition['migration_tags']) && !$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('d7_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 d7_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' => $row->getSourceProperty('name'),
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 // Comment status must be mapped to correct comment type.
Chris@0 100 // Comment type migration creates a separate comment type for each
Chris@0 101 // node type except for Forum which uses 'comment_forum'.
Chris@0 102 $comment_type = 'comment_node_' . $node_type;
Chris@0 103 if ($node_type == 'forum') {
Chris@0 104 $comment_type = 'comment_forum';
Chris@0 105 }
Chris@0 106 $nested_key = $comment_type . '/0/status';
Chris@0 107 $values['process'][$nested_key] = 'comment';
Chris@0 108
Chris@0 109 // If this migration is based on the d7_node_revision migration or
Chris@0 110 // is for translations of nodes, it should explicitly depend on the
Chris@0 111 // corresponding d7_node variant.
Chris@0 112 if ($base_plugin_definition['id'] == ['d7_node_revision'] || in_array('translation', $base_plugin_definition['migration_tags'])) {
Chris@0 113 $values['migration_dependencies']['required'][] = 'd7_node:' . $node_type;
Chris@0 114 }
Chris@0 115
Chris@18 116 /** @var \Drupal\migrate\Plugin\MigrationInterface $migration */
Chris@0 117 $migration = \Drupal::service('plugin.manager.migration')->createStubMigration($values);
Chris@18 118 $this->fieldDiscovery->addBundleFieldProcesses($migration, 'node', $node_type);
Chris@0 119 $this->derivatives[$node_type] = $migration->getPluginDefinition();
Chris@0 120 }
Chris@0 121 }
Chris@0 122 catch (DatabaseExceptionWrapper $e) {
Chris@0 123 // Once we begin iterating the source plugin it is possible that the
Chris@0 124 // source tables will not exist. This can happen when the
Chris@0 125 // MigrationPluginManager gathers up the migration definitions but we do
Chris@0 126 // not actually have a Drupal 7 source database.
Chris@0 127 }
Chris@0 128 return $this->derivatives;
Chris@0 129 }
Chris@0 130
Chris@0 131 }