Skip to content
Snippets Groups Projects

Issue #3435726 by tolstoydotcom, msielski, hongpong, ressa: Automated Drupal...

Files
11
@@ -2,13 +2,16 @@
namespace Drupal\wordpress_migrate;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\File\FileUrlGeneratorInterface;
use Drupal\migrate\Plugin\MigrationPluginManagerInterface;
use Drupal\migrate_plus\Entity\Migration;
use Drupal\migrate_plus\Entity\MigrationGroup;
/**
* Construct WordPress migrations from broad configuration settings.
*/
class WordPressMigrationGenerator {
class WordPressMigrationGenerator implements WordPressMigrationGeneratorInterface {
/**
* Configuration to guide our migration creation process.
@@ -53,7 +56,16 @@ class WordPressMigrationGenerator {
protected $attachmentID = '';
/**
* Constructs a WordPress migration generator, using provided configuration.
* Constructs a WordPress migration generator.
*/
public function __construct(
private readonly EntityFieldManagerInterface $entityFieldManager,
private readonly MigrationPluginManagerInterface $pluginManagerMigration,
private readonly FileUrlGeneratorInterface $fileUrlGenerator,
) {}
/**
* Creates a set of WordPress import migrations, using provided configuration.
*
* @param array $configuration
* An associative array:
@@ -70,18 +82,13 @@ class WordPressMigrationGenerator {
*
* @todo Validate inputs (e.g., make sure post type exists).
* @link https://www.drupal.org/node/2742283
*/
public function __construct(array $configuration) {
$this->configuration = $configuration;
}
/**
* Creates a set of WordPress import migrations based on config settings.
*
* @throws \Drupal\Core\Entity\EntityStorageException
* @throws \Exception
*/
public function createMigrations() {
public function createMigrations(array $configuration): void {
$this->configuration = $configuration;
// Create the migration group.
$group_configuration = [
'id' => $this->configuration['group_id'],
@@ -124,7 +131,7 @@ class WordPressMigrationGenerator {
}
else {
$this->authorID = $this->configuration['prefix'] . 'wordpress_authors';
$migration = static::createEntityFromPlugin('wordpress_authors', $this->authorID);
$migration = $this->createEntityFromPlugin('wordpress_authors', $this->authorID);
$migration->set('migration_group', $this->configuration['group_id']);
$migration->save();
$this->uidMapping = [
@@ -136,7 +143,7 @@ class WordPressMigrationGenerator {
// Set up the attachment migration.
$this->attachmentID = $this->configuration['prefix'] . 'wordpress_attachments';
$migration = static::createEntityFromPlugin('wordpress_attachments', $this->attachmentID);
$migration = $this->createEntityFromPlugin('wordpress_attachments', $this->attachmentID);
$migration->set('migration_group', $this->configuration['group_id']);
$process = $migration->get('process');
$process['uid'] = $this->uidMapping;
@@ -146,7 +153,7 @@ class WordPressMigrationGenerator {
// 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 = $this->createEntityFromPlugin('wordpress_tags', $this->tagsID);
$migration->set('migration_group', $this->configuration['group_id']);
$process = $migration->get('process');
$process['vid'] = [
@@ -158,7 +165,7 @@ class WordPressMigrationGenerator {
}
if ($this->configuration['category_vocabulary']) {
$this->categoriesID = $this->configuration['prefix'] . 'wordpress_categories';
$migration = static::createEntityFromPlugin('wordpress_categories', $this->categoriesID);
$migration = $this->createEntityFromPlugin('wordpress_categories', $this->categoriesID);
$migration->set('migration_group', $this->configuration['group_id']);
$process = $migration->get('process');
$process['vid'] = [
@@ -189,12 +196,12 @@ class WordPressMigrationGenerator {
protected function createContentMigration($wordpress_type) {
$dependencies = [];
$content_id = $this->configuration['prefix'] . 'wordpress_content_' . $wordpress_type;
$migration = static::createEntityFromPlugin('wordpress_content', $content_id);
$migration = $this->createEntityFromPlugin('wordpress_content', $content_id);
$migration->set('migration_group', $this->configuration['group_id']);
$source = $migration->get('source');
$source = $migration->get('source');
$source['item_selector'] .= '[wp:post_type="' . $wordpress_type . '"]';
$migration->set('source', $source);
$process = $migration->get('process');
$process = $migration->get('process');
// Path alias generator, if it is set in form.
if ($this->configuration['base_url'] !== '') {
$process = $this->processUrlPathAliases($process);
@@ -247,24 +254,24 @@ class WordPressMigrationGenerator {
// 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']);
$all_fields = $this->entityFieldManager->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 = $this->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] = [
$process['entity_id'][0] = [
'plugin' => 'migration_lookup',
'source' => 'post_id',
'migration' => $content_id,
];
$process['comment_type'][0]['default_value'] = $storage->getSetting('comment_type');
$process['pid'][0] = [
$process['pid'][0] = [
'plugin' => 'migration_lookup',
'source' => 'comment_parent',
'migration' => $id,
@@ -291,7 +298,7 @@ class WordPressMigrationGenerator {
*/
protected function termField($bundle, $vocabulary) {
/** @var \Drupal\Core\Field\FieldDefinitionInterface[] $all_fields */
$all_fields = \Drupal::service('entity_field.manager')->getFieldDefinitions('node', $bundle);
$all_fields = $this->entityFieldManager->getFieldDefinitions('node', $bundle);
foreach ($all_fields as $field_name => $field_definition) {
if ($field_definition->getType() == 'entity_reference') {
$storage = $field_definition->getFieldStorageDefinition();
@@ -324,10 +331,9 @@ class WordPressMigrationGenerator {
* when there's a migrate_plus release containing it we can have a dependency
* on.
*/
protected static function createEntityFromPlugin($plugin_id, $new_plugin_id) {
protected 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);
$migration_plugin = $this->pluginManagerMigration->createInstance($plugin_id);
$entity_array['id'] = $new_plugin_id;
$entity_array['migration_tags'] = $migration_plugin->getMigrationTags();
$entity_array['label'] = $migration_plugin->label();
Loading