annotate core/modules/node/src/Plugin/migrate/D6NodeDeriver.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 129ea1e6d783
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\Component\Plugin\Exception\PluginNotFoundException;
Chris@0 7 use Drupal\Core\Database\DatabaseExceptionWrapper;
Chris@0 8 use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
Chris@0 9 use Drupal\migrate\Exception\RequirementsException;
Chris@0 10 use Drupal\migrate\Plugin\MigrationDeriverTrait;
Chris@0 11 use Drupal\migrate_drupal\Plugin\MigrateCckFieldPluginManagerInterface;
Chris@0 12 use Drupal\migrate_drupal\Plugin\MigrateFieldPluginManagerInterface;
Chris@0 13 use Symfony\Component\DependencyInjection\ContainerInterface;
Chris@0 14
Chris@0 15 /**
Chris@0 16 * Deriver for Drupal 6 node and node revision migrations based on node types.
Chris@0 17 */
Chris@0 18 class D6NodeDeriver extends DeriverBase implements ContainerDeriverInterface {
Chris@0 19 use MigrationDeriverTrait;
Chris@0 20
Chris@0 21 /**
Chris@0 22 * The base plugin ID this derivative is for.
Chris@0 23 *
Chris@0 24 * @var string
Chris@0 25 */
Chris@0 26 protected $basePluginId;
Chris@0 27
Chris@0 28 /**
Chris@0 29 * Already-instantiated cckfield plugins, keyed by ID.
Chris@0 30 *
Chris@0 31 * @var \Drupal\migrate_drupal\Plugin\MigrateCckFieldInterface[]
Chris@0 32 */
Chris@0 33 protected $cckPluginCache;
Chris@0 34
Chris@0 35 /**
Chris@0 36 * The CCK plugin manager.
Chris@0 37 *
Chris@0 38 * @var \Drupal\migrate_drupal\Plugin\MigrateCckFieldPluginManagerInterface
Chris@0 39 */
Chris@0 40 protected $cckPluginManager;
Chris@0 41
Chris@0 42 /**
Chris@0 43 * Already-instantiated field plugins, keyed by ID.
Chris@0 44 *
Chris@0 45 * @var \Drupal\migrate_drupal\Plugin\MigrateFieldInterface[]
Chris@0 46 */
Chris@0 47 protected $fieldPluginCache;
Chris@0 48
Chris@0 49 /**
Chris@0 50 * The field plugin manager.
Chris@0 51 *
Chris@0 52 * @var \Drupal\migrate_drupal\Plugin\MigrateFieldPluginManagerInterface
Chris@0 53 */
Chris@0 54 protected $fieldPluginManager;
Chris@0 55
Chris@0 56 /**
Chris@0 57 * Whether or not to include translations.
Chris@0 58 *
Chris@0 59 * @var bool
Chris@0 60 */
Chris@0 61 protected $includeTranslations;
Chris@0 62
Chris@0 63 /**
Chris@0 64 * D6NodeDeriver constructor.
Chris@0 65 *
Chris@0 66 * @param string $base_plugin_id
Chris@0 67 * The base plugin ID for the plugin ID.
Chris@0 68 * @param \Drupal\migrate_drupal\Plugin\MigrateCckFieldPluginManagerInterface $cck_manager
Chris@0 69 * The CCK plugin manager.
Chris@0 70 * @param \Drupal\migrate_drupal\Plugin\MigrateFieldPluginManagerInterface $field_manager
Chris@0 71 * The field plugin manager.
Chris@0 72 * @param bool $translations
Chris@0 73 * Whether or not to include translations.
Chris@0 74 */
Chris@0 75 public function __construct($base_plugin_id, MigrateCckFieldPluginManagerInterface $cck_manager, MigrateFieldPluginManagerInterface $field_manager, $translations) {
Chris@0 76 $this->basePluginId = $base_plugin_id;
Chris@0 77 $this->cckPluginManager = $cck_manager;
Chris@0 78 $this->fieldPluginManager = $field_manager;
Chris@0 79 $this->includeTranslations = $translations;
Chris@0 80 }
Chris@0 81
Chris@0 82 /**
Chris@0 83 * {@inheritdoc}
Chris@0 84 */
Chris@0 85 public static function create(ContainerInterface $container, $base_plugin_id) {
Chris@0 86 // Translations don't make sense unless we have content_translation.
Chris@0 87 return new static(
Chris@0 88 $base_plugin_id,
Chris@0 89 $container->get('plugin.manager.migrate.cckfield'),
Chris@0 90 $container->get('plugin.manager.migrate.field'),
Chris@0 91 $container->get('module_handler')->moduleExists('content_translation')
Chris@0 92 );
Chris@0 93 }
Chris@0 94
Chris@0 95 /**
Chris@0 96 * Gets the definition of all derivatives of a base plugin.
Chris@0 97 *
Chris@0 98 * @param array $base_plugin_definition
Chris@0 99 * The definition array of the base plugin.
Chris@0 100 *
Chris@0 101 * @return array
Chris@0 102 * An array of full derivative definitions keyed on derivative id.
Chris@0 103 *
Chris@0 104 * @see \Drupal\Component\Plugin\Derivative\DeriverBase::getDerivativeDefinition()
Chris@0 105 */
Chris@0 106 public function getDerivativeDefinitions($base_plugin_definition) {
Chris@0 107 if ($base_plugin_definition['id'] == 'd6_node_translation' && !$this->includeTranslations) {
Chris@0 108 // Refuse to generate anything.
Chris@0 109 return $this->derivatives;
Chris@0 110 }
Chris@0 111
Chris@0 112 $node_types = static::getSourcePlugin('d6_node_type');
Chris@0 113 try {
Chris@0 114 $node_types->checkRequirements();
Chris@0 115 }
Chris@0 116 catch (RequirementsException $e) {
Chris@0 117 // If the d6_node_type requirements failed, that means we do not have a
Chris@0 118 // Drupal source database configured - there is nothing to generate.
Chris@0 119 return $this->derivatives;
Chris@0 120 }
Chris@0 121
Chris@0 122 // Read all field instance definitions in the source database.
Chris@0 123 $fields = [];
Chris@0 124 try {
Chris@0 125 $source_plugin = static::getSourcePlugin('d6_field_instance');
Chris@0 126 $source_plugin->checkRequirements();
Chris@0 127
Chris@0 128 foreach ($source_plugin as $row) {
Chris@0 129 $fields[$row->getSourceProperty('type_name')][$row->getSourceProperty('field_name')] = $row->getSource();
Chris@0 130 }
Chris@0 131 }
Chris@0 132 catch (RequirementsException $e) {
Chris@0 133 // If checkRequirements() failed then the content module did not exist and
Chris@0 134 // we do not have any fields. Therefore, $fields will be empty and
Chris@0 135 // below we'll create a migration just for the node properties.
Chris@0 136 }
Chris@0 137
Chris@0 138 try {
Chris@0 139 foreach ($node_types as $row) {
Chris@0 140 $node_type = $row->getSourceProperty('type');
Chris@0 141 $values = $base_plugin_definition;
Chris@0 142
Chris@0 143 $values['label'] = t("@label (@type)", [
Chris@0 144 '@label' => $values['label'],
Chris@0 145 '@type' => $node_type,
Chris@0 146 ]);
Chris@0 147 $values['source']['node_type'] = $node_type;
Chris@0 148 $values['destination']['default_bundle'] = $node_type;
Chris@0 149
Chris@0 150 // If this migration is based on the d6_node_revision migration or
Chris@0 151 // is for translations of nodes, it should explicitly depend on the
Chris@0 152 // corresponding d6_node variant.
Chris@0 153 if (in_array($base_plugin_definition['id'], ['d6_node_revision', 'd6_node_translation'])) {
Chris@0 154 $values['migration_dependencies']['required'][] = 'd6_node:' . $node_type;
Chris@0 155 }
Chris@0 156
Chris@0 157 /** @var \Drupal\migrate\Plugin\Migration $migration */
Chris@0 158 $migration = \Drupal::service('plugin.manager.migration')->createStubMigration($values);
Chris@0 159 if (isset($fields[$node_type])) {
Chris@0 160 foreach ($fields[$node_type] as $field_name => $info) {
Chris@0 161 $field_type = $info['type'];
Chris@0 162 try {
Chris@0 163 $plugin_id = $this->fieldPluginManager->getPluginIdFromFieldType($field_type, ['core' => 6], $migration);
Chris@0 164 if (!isset($this->fieldPluginCache[$field_type])) {
Chris@0 165 $this->fieldPluginCache[$field_type] = $this->fieldPluginManager->createInstance($plugin_id, ['core' => 6], $migration);
Chris@0 166 }
Chris@0 167 $this->fieldPluginCache[$field_type]
Chris@0 168 ->processFieldValues($migration, $field_name, $info);
Chris@0 169 }
Chris@0 170 catch (PluginNotFoundException $ex) {
Chris@0 171 try {
Chris@0 172 $plugin_id = $this->cckPluginManager->getPluginIdFromFieldType($field_type, ['core' => 6], $migration);
Chris@0 173 if (!isset($this->cckPluginCache[$field_type])) {
Chris@0 174 $this->cckPluginCache[$field_type] = $this->cckPluginManager->createInstance($plugin_id, ['core' => 6], $migration);
Chris@0 175 }
Chris@0 176 $this->cckPluginCache[$field_type]
Chris@0 177 ->processCckFieldValues($migration, $field_name, $info);
Chris@0 178 }
Chris@0 179 catch (PluginNotFoundException $ex) {
Chris@0 180 $migration->setProcessOfProperty($field_name, $field_name);
Chris@0 181 }
Chris@0 182 }
Chris@0 183 }
Chris@0 184 }
Chris@0 185 $this->derivatives[$node_type] = $migration->getPluginDefinition();
Chris@0 186 }
Chris@0 187 }
Chris@0 188 catch (DatabaseExceptionWrapper $e) {
Chris@0 189 // Once we begin iterating the source plugin it is possible that the
Chris@0 190 // source tables will not exist. This can happen when the
Chris@0 191 // MigrationPluginManager gathers up the migration definitions but we do
Chris@0 192 // not actually have a Drupal 6 source database.
Chris@0 193 }
Chris@0 194
Chris@0 195 return $this->derivatives;
Chris@0 196 }
Chris@0 197
Chris@0 198 }