annotate core/modules/node/src/Plugin/migrate/D7NodeDeriver.php @ 12:7a779792577d

Update Drupal core to v8.4.5 (via Composer)
author Chris Cannam
date Fri, 23 Feb 2018 15:52:07 +0000
parents 4c8ae668cc8c
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 7 node and node revision migrations based on node types.
Chris@0 17 */
Chris@0 18 class D7NodeDeriver 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 * D7NodeDeriver 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 * {@inheritdoc}
Chris@0 97 */
Chris@0 98 public function getDerivativeDefinitions($base_plugin_definition) {
Chris@0 99 if (in_array('translation', $base_plugin_definition['migration_tags']) && !$this->includeTranslations) {
Chris@0 100 // Refuse to generate anything.
Chris@0 101 return $this->derivatives;
Chris@0 102 }
Chris@0 103
Chris@0 104 $node_types = static::getSourcePlugin('d7_node_type');
Chris@0 105 try {
Chris@0 106 $node_types->checkRequirements();
Chris@0 107 }
Chris@0 108 catch (RequirementsException $e) {
Chris@0 109 // If the d7_node_type requirements failed, that means we do not have a
Chris@0 110 // Drupal source database configured - there is nothing to generate.
Chris@0 111 return $this->derivatives;
Chris@0 112 }
Chris@0 113
Chris@0 114 $fields = [];
Chris@0 115 try {
Chris@0 116 $source_plugin = static::getSourcePlugin('d7_field_instance');
Chris@0 117 $source_plugin->checkRequirements();
Chris@0 118
Chris@0 119 // Read all field instance definitions in the source database.
Chris@0 120 foreach ($source_plugin as $row) {
Chris@0 121 if ($row->getSourceProperty('entity_type') == 'node') {
Chris@0 122 $fields[$row->getSourceProperty('bundle')][$row->getSourceProperty('field_name')] = $row->getSource();
Chris@0 123 }
Chris@0 124 }
Chris@0 125 }
Chris@0 126 catch (RequirementsException $e) {
Chris@0 127 // If checkRequirements() failed then the field module did not exist and
Chris@0 128 // we do not have any fields. Therefore, $fields will be empty and below
Chris@0 129 // we'll create a migration just for the node properties.
Chris@0 130 }
Chris@0 131
Chris@0 132 try {
Chris@0 133 foreach ($node_types as $row) {
Chris@0 134 $node_type = $row->getSourceProperty('type');
Chris@0 135 $values = $base_plugin_definition;
Chris@0 136
Chris@0 137 $values['label'] = t('@label (@type)', [
Chris@0 138 '@label' => $values['label'],
Chris@0 139 '@type' => $row->getSourceProperty('name'),
Chris@0 140 ]);
Chris@0 141 $values['source']['node_type'] = $node_type;
Chris@0 142 $values['destination']['default_bundle'] = $node_type;
Chris@0 143
Chris@0 144 // Comment status must be mapped to correct comment type.
Chris@0 145 // Comment type migration creates a separate comment type for each
Chris@0 146 // node type except for Forum which uses 'comment_forum'.
Chris@0 147 $comment_type = 'comment_node_' . $node_type;
Chris@0 148 if ($node_type == 'forum') {
Chris@0 149 $comment_type = 'comment_forum';
Chris@0 150 }
Chris@0 151 $nested_key = $comment_type . '/0/status';
Chris@0 152 $values['process'][$nested_key] = 'comment';
Chris@0 153
Chris@0 154 // If this migration is based on the d7_node_revision migration or
Chris@0 155 // is for translations of nodes, it should explicitly depend on the
Chris@0 156 // corresponding d7_node variant.
Chris@0 157 if ($base_plugin_definition['id'] == ['d7_node_revision'] || in_array('translation', $base_plugin_definition['migration_tags'])) {
Chris@0 158 $values['migration_dependencies']['required'][] = 'd7_node:' . $node_type;
Chris@0 159 }
Chris@0 160
Chris@0 161 $migration = \Drupal::service('plugin.manager.migration')->createStubMigration($values);
Chris@0 162 if (isset($fields[$node_type])) {
Chris@0 163 foreach ($fields[$node_type] as $field_name => $info) {
Chris@0 164 $field_type = $info['type'];
Chris@0 165 try {
Chris@0 166 $plugin_id = $this->fieldPluginManager->getPluginIdFromFieldType($field_type, ['core' => 7], $migration);
Chris@0 167 if (!isset($this->fieldPluginCache[$field_type])) {
Chris@0 168 $this->fieldPluginCache[$field_type] = $this->fieldPluginManager->createInstance($plugin_id, ['core' => 7], $migration);
Chris@0 169 }
Chris@0 170 $this->fieldPluginCache[$field_type]
Chris@0 171 ->processFieldValues($migration, $field_name, $info);
Chris@0 172 }
Chris@0 173 catch (PluginNotFoundException $ex) {
Chris@0 174 try {
Chris@0 175 $plugin_id = $this->cckPluginManager->getPluginIdFromFieldType($field_type, ['core' => 7], $migration);
Chris@0 176 if (!isset($this->cckPluginCache[$field_type])) {
Chris@0 177 $this->cckPluginCache[$field_type] = $this->cckPluginManager->createInstance($plugin_id, ['core' => 7], $migration);
Chris@0 178 }
Chris@0 179 $this->cckPluginCache[$field_type]
Chris@0 180 ->processCckFieldValues($migration, $field_name, $info);
Chris@0 181 }
Chris@0 182 catch (PluginNotFoundException $ex) {
Chris@0 183 $migration->setProcessOfProperty($field_name, $field_name);
Chris@0 184 }
Chris@0 185 }
Chris@0 186 }
Chris@0 187 }
Chris@0 188 $this->derivatives[$node_type] = $migration->getPluginDefinition();
Chris@0 189 }
Chris@0 190 }
Chris@0 191 catch (DatabaseExceptionWrapper $e) {
Chris@0 192 // Once we begin iterating the source plugin it is possible that the
Chris@0 193 // source tables will not exist. This can happen when the
Chris@0 194 // MigrationPluginManager gathers up the migration definitions but we do
Chris@0 195 // not actually have a Drupal 7 source database.
Chris@0 196 }
Chris@0 197 return $this->derivatives;
Chris@0 198 }
Chris@0 199
Chris@0 200 }