class DrupalMigrationGenerator in Drupal-to-Drupal data migration 8.3
Functionality to construct Drupal migrations from broad configuration settings.
Hierarchy
- class \Drupal\migrate_d2d\DrupalMigrationGenerator
Expanded class hierarchy of DrupalMigrationGenerator
1 file declares its use of DrupalMigrationGenerator
- ImportWizard.php in migrate_d2d_ui/
src/ Wizard/ ImportWizard.php
File
- src/
DrupalMigrationGenerator.php, line 12
Namespace
Drupal\migrate_d2dView source
class DrupalMigrationGenerator {
/**
* Configuration to guide our migration creation process.
*
* @var array
*/
protected $configuration = [];
/**
* Process plugin configuration for uid references.
*
* @var array
*/
protected $uidMapping = [];
/**
* ID of the configuration entity for user migration.
*
* @var string
*/
protected $userID = '';
/**
* ID of the configuration entity for tag migration.
*
* @var string
*/
protected $tagsID = '';
/**
* ID of the configuration entity for category migration.
*
* @var string
*/
protected $categoriesID = '';
/**
* Constructs a WordPress migration generator, using provided configuration.
*
* @todo: Validate inputs (e.g., make sure post type exists).
* @link https://www.drupal.org/node/2742283
*
* @param $configuration
* An associative array:
* - file_uri: Drupal stream wrapper of the source WordPress XML file.
* - group_id: ID of the MigrationGroup holding the generated migrations.
* - prefix: String to prefix to the IDs of generated migrations.
* - default_author: If present, username to author all imported content. If
* absent or empty, users will be imported from WordPress.
* - tag_vocabulary: Machine name of vocabulary to hold tags.
* - category_vocabulary: Machine name of vocabulary to hold categories.
* - [post|page]: Associative array of type-specific configuration:
* - type: Machine name of Drupal node bundle to hold content.
* - text_format: Machine name of text format for body field.
*/
public function __construct(array $configuration) {
$this->configuration = $configuration;
}
/**
* Creates a set of WordPress import migrations based on configuration settings.
*/
public function createMigrations() {
// Create the migration group.
$group_configuration = [
'id' => $this->configuration['group_id'],
// @todo: Add Drupal site title in here.
'label' => 'Imports from Drupal site',
'source_type' => 'Drupal ' . $this->configuration['version'],
'shared_configuration' => [
'source' => [
'key' => 'drupal_import',
'database' => [
$this->configuration['database'],
],
],
],
];
MigrationGroup::create($group_configuration)
->save();
// Determine the uid mappings, creating an author migration if needed.
if ($this->configuration['user_migration']) {
// @todo: Implement role migration.
$this->userID = $this->configuration['prefix'] . 'd6_user';
$migration = static::createEntityFromPlugin('d6_user', $this->userID);
$migration
->set('migration_group', $this->configuration['group_id']);
$migration
->save();
$this->uidMapping = [
'plugin' => 'migration',
'migration' => $this->userID,
'source' => 'uid',
];
}
return;
// Set up the attachment migration.
/* @todo: Wait until https://www.drupal.org/node/2695297 to complete implementation.
$attachment_id = $this->configuration['prefix'] . 'wordpress_attachments';
$migration = static::createEntityFromPlugin('wordpress_attachments', $attachment_id);
$migration->set('migration_group', $this->configuration['group_id']);
$process = $migration->get('process');
$process['uid'] = $this->uidMapping;
$migration->set('process', $process);
if (!empty($this->authorID)) {
$migration->set('migration_dependencies', ['required' => [$this->authorID]]);
}
$migration->save();
*/
// Setup vocabulary migrations if requested.
if ($this->configuration['tag_vocabulary']) {
$this->tagsID = $this->configuration['prefix'] . 'wordpress_tags';
$migration = static::createEntityFromPlugin('wordpress_tags', $this->tagsID);
$migration
->set('migration_group', $this->configuration['group_id']);
$process = $migration
->get('process');
$process['vid'] = [
'plugin' => 'default_value',
'default_value' => $this->configuration['tag_vocabulary'],
];
$migration
->set('process', $process);
$migration
->save();
}
if ($this->configuration['category_vocabulary']) {
$this->categoriesID = $this->configuration['prefix'] . 'wordpress_categories';
$migration = static::createEntityFromPlugin('wordpress_categories', $this->categoriesID);
$migration
->set('migration_group', $this->configuration['group_id']);
$process = $migration
->get('process');
$process['vid'] = [
'plugin' => 'default_value',
'default_value' => $this->configuration['category_vocabulary'],
];
$migration
->set('process', $process);
$migration
->save();
}
// Setup the content migrations.
foreach ([
'post',
'page',
] as $wordpress_type) {
if (!empty($this->configuration[$wordpress_type]['type'])) {
$this
->createContentMigration($wordpress_type);
}
}
}
/**
* Setup the migration for a given WordPress content type.
*
* @param $wordpress_type
* WordPress content type - 'post' or 'page'.
*/
protected function createContentMigration($wordpress_type) {
$dependencies = [];
$content_id = $this->configuration['prefix'] . 'wordpress_content_' . $wordpress_type;
$migration = static::createEntityFromPlugin('wordpress_content', $content_id);
$migration
->set('migration_group', $this->configuration['group_id']);
$source = $migration
->get('source');
$source['item_selector'] .= '[wp:post_type="' . $wordpress_type . '"]';
$migration
->set('source', $source);
$process = $migration
->get('process');
$process['uid'] = $this->uidMapping;
$process['body/format'] = [
'plugin' => 'default_value',
'default_value' => $this->configuration[$wordpress_type]['text_format'],
];
$process['type'] = [
'plugin' => 'default_value',
'default_value' => $this->configuration[$wordpress_type]['type'],
];
if ($this->configuration['tag_vocabulary']) {
if ($term_field = $this
->termField($this->configuration[$wordpress_type]['type'], $this->configuration['tag_vocabulary'])) {
$process[$term_field] = [
'plugin' => 'migration',
'migration' => $this->tagsID,
'source' => 'post_tag',
];
$dependencies[] = $this->tagsID;
}
}
if ($this->configuration['category_vocabulary']) {
if ($term_field = $this
->termField($this->configuration[$wordpress_type]['type'], $this->configuration['category_vocabulary'])) {
$process[$term_field] = [
'plugin' => 'migration',
'migration' => $this->categoriesID,
'source' => 'category',
];
$dependencies[] = $this->categoriesID;
}
}
$migration
->set('process', $process);
if (!empty($this->authorID)) {
$dependencies[] = $this->authorID;
}
$migration
->set('migration_dependencies', [
'required' => $dependencies,
]);
$migration
->save();
// Also create a comment migration, if the content type has a comment field.
/** @var \Drupal\Core\Field\FieldDefinitionInterface[] $all_fields */
$all_fields = \Drupal::service('entity_field.manager')
->getFieldDefinitions('node', $this->configuration[$wordpress_type]['type']);
foreach ($all_fields as $field_name => $field_definition) {
if ($field_definition
->getType() == 'comment') {
$storage = $field_definition
->getFieldStorageDefinition();
$id = $this->configuration['prefix'] . 'wordpress_comment_' . $wordpress_type;
$migration = static::createEntityFromPlugin('wordpress_comment', $id);
$migration
->set('migration_group', $this->configuration['group_id']);
$source = $migration
->get('source');
$source['item_selector'] = str_replace(':content_type', $wordpress_type, $source['item_selector']);
$migration
->set('source', $source);
$process = $migration
->get('process');
$process['entity_id'][0]['migration'] = $content_id;
$process['comment_type'][0]['default_value'] = $storage
->getSetting('comment_type');
$process['pid'][0]['migration'] = $id;
$process['field_name'][0]['default_value'] = $field_name;
$migration
->set('process', $process);
$migration
->set('migration_dependencies', [
'required' => [
$content_id,
],
]);
$migration
->save();
break;
}
}
}
/**
* Returns the first field referencing a given vocabulary.
*
* @param string $bundle
* @param string $vocabulary
* @return string
*/
protected function termField($bundle, $vocabulary) {
/** @var \Drupal\Core\Field\FieldDefinitionInterface[] $all_fields */
$all_fields = \Drupal::service('entity_field.manager')
->getFieldDefinitions('node', $bundle);
foreach ($all_fields as $field_name => $field_definition) {
if ($field_definition
->getType() == 'entity_reference') {
$storage = $field_definition
->getFieldStorageDefinition();
if ($storage
->getSetting('target_type') == 'taxonomy_term') {
$handler_settings = $field_definition
->getSetting('handler_settings');
if (isset($handler_settings['target_bundles'][$vocabulary])) {
return $field_name;
}
}
}
}
return '';
}
/**
* Create a configuration entity from a core migration plugin's configuration.
*
* @todo: Remove and replace calls to use Migration::createEntityFromPlugin()
* when there's a migrate_plus release containing it we can have a dependency
* on.
*
* @param string $plugin_id
* ID of a migration plugin managed by MigrationPluginManager.
* @param string $new_plugin_id
* ID to use for the new configuration entity.
*
* @return \Drupal\migrate_plus\Entity\MigrationInterface
* A Migration configuration entity (not saved to persistent storage).
*/
protected static function createEntityFromPlugin($plugin_id, $new_plugin_id) {
/** @var \Drupal\migrate\Plugin\MigrationPluginManagerInterface $plugin_manager */
$plugin_manager = \Drupal::service('plugin.manager.migration');
$migration_plugin = $plugin_manager
->createInstance($plugin_id);
$entity_array['id'] = $new_plugin_id;
$entity_array['migration_tags'] = $migration_plugin
->get('migration_tags');
$entity_array['label'] = $migration_plugin
->label();
$entity_array['source'] = $migration_plugin
->getSourceConfiguration();
$entity_array['destination'] = $migration_plugin
->getDestinationConfiguration();
$entity_array['process'] = $migration_plugin
->getProcess();
$entity_array['migration_dependencies'] = $migration_plugin
->getMigrationDependencies();
$migration_entity = Migration::create($entity_array);
return $migration_entity;
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DrupalMigrationGenerator:: |
protected | property | ID of the configuration entity for category migration. | |
DrupalMigrationGenerator:: |
protected | property | Configuration to guide our migration creation process. | |
DrupalMigrationGenerator:: |
protected | property | ID of the configuration entity for tag migration. | |
DrupalMigrationGenerator:: |
protected | property | Process plugin configuration for uid references. | |
DrupalMigrationGenerator:: |
protected | property | ID of the configuration entity for user migration. | |
DrupalMigrationGenerator:: |
protected | function | Setup the migration for a given WordPress content type. | |
DrupalMigrationGenerator:: |
protected static | function | Create a configuration entity from a core migration plugin's configuration. | |
DrupalMigrationGenerator:: |
public | function | Creates a set of WordPress import migrations based on configuration settings. | |
DrupalMigrationGenerator:: |
protected | function | Returns the first field referencing a given vocabulary. | |
DrupalMigrationGenerator:: |
public | function | Constructs a WordPress migration generator, using provided configuration. |