View source
<?php
namespace Drupal\wordpress_migrate;
use Drupal\migrate_plus\Entity\Migration;
use Drupal\migrate_plus\Entity\MigrationGroup;
class WordPressMigrationGenerator {
protected $configuration = [];
protected $uidMapping = [];
protected $authorID = '';
protected $tagsID = '';
protected $categoriesID = '';
protected $attachmentID = '';
public function __construct(array $configuration) {
$this->configuration = $configuration;
}
public function createMigrations() {
$group_configuration = [
'id' => $this->configuration['group_id'],
'label' => 'Imports from WordPress site',
'source_type' => 'WordPress',
'shared_configuration' => [
'source' => [
'namespaces' => [
'wp' => 'http://wordpress.org/export/1.2/',
'excerpt' => 'http://wordpress.org/export/1.2/excerpt/',
'content' => 'http://purl.org/rss/1.0/modules/content/',
'wfw' => 'http://wellformedweb.org/CommentAPI/',
'dc' => 'http://purl.org/dc/elements/1.1/',
],
'urls' => [
$this->configuration['file_uri'],
],
],
],
];
MigrationGroup::create($group_configuration)
->save();
if ($this->configuration['default_author']) {
$account = user_load_by_name($this->configuration['default_author']);
if ($account) {
$this->uidMapping = [
'plugin' => 'default_value',
'default_value' => $account
->id(),
];
}
else {
throw new \Exception('Username @name does not exist.', [
'@name' => $this->configuration['default_author'],
]);
}
}
else {
$this->authorID = $this->configuration['prefix'] . 'wordpress_authors';
$migration = static::createEntityFromPlugin('wordpress_authors', $this->authorID);
$migration
->set('migration_group', $this->configuration['group_id']);
$migration
->save();
$this->uidMapping = [
'plugin' => 'migration_lookup',
'migration' => $this->authorID,
'source' => 'creator',
];
}
$this->attachmentID = $this->configuration['prefix'] . 'wordpress_attachments';
$migration = static::createEntityFromPlugin('wordpress_attachments', $this->attachmentID);
$migration
->set('migration_group', $this->configuration['group_id']);
$process = $migration
->get('process');
$process['uid'] = $this->uidMapping;
$migration
->set('process', $process);
$migration
->save();
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();
}
foreach ([
'post',
'page',
] as $wordpress_type) {
if (!empty($this->configuration[$wordpress_type]['type'])) {
$this
->createContentMigration($wordpress_type);
}
}
}
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']) {
$term_field = $this
->termField($this->configuration[$wordpress_type]['type'], $this->configuration['tag_vocabulary']);
if ($term_field) {
$process[$term_field] = [
'plugin' => 'migration_lookup',
'migration' => $this->tagsID,
'source' => 'post_tag',
];
$dependencies[] = $this->tagsID;
}
}
if ($this->configuration['category_vocabulary']) {
$term_field = $this
->termField($this->configuration[$wordpress_type]['type'], $this->configuration['category_vocabulary']);
if ($term_field) {
$process[$term_field] = [
'plugin' => 'migration_lookup',
'migration' => $this->categoriesID,
'source' => 'category',
];
$dependencies[] = $this->categoriesID;
}
}
if ($this->configuration['image_field']) {
$process[$this->configuration['image_field']] = [
'plugin' => 'migration',
'migration' => $this->attachmentID,
'source' => 'thumbnail_id',
];
$dependencies[] = $this->attachmentID;
}
$migration
->set('process', $process);
if (!empty($this->authorID)) {
$dependencies[] = $this->authorID;
}
$migration
->set('migration_dependencies', [
'required' => $dependencies,
]);
$migration
->save();
$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;
}
}
}
protected function termField($bundle, $vocabulary) {
$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 '';
}
protected static function createEntityFromPlugin($plugin_id, $new_plugin_id) {
$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
->getMigrationTags();
$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;
}
}