diff --git a/migrations/wordpress_attachments.yml b/migrations/wordpress_attachments.yml index f24b361..9ba69cf 100644 --- a/migrations/wordpress_attachments.yml +++ b/migrations/wordpress_attachments.yml @@ -9,6 +9,8 @@ source: data_fetcher_plugin: http data_parser_plugin: xml item_selector: '/rss/channel/item[wp:post_type="attachment"]' + constants: + file_dest_uri: 'public://featured_images' fields: - name: title @@ -53,6 +55,27 @@ source: type: integer process: # uid mapping is dynamically generated. + destination_file: + plugin: callback + callable: + - '\Drupal\Core\File\FileSystem' + - basename + source: attachment_url + filename: '@destination_file' + destination_full_path: + - + plugin: concat + delimiter: / + source: + - constants/file_dest_uri + - '@destination_file' + - + plugin: urlencode + uri: + plugin: file_copy + source: + - attachment_url + - '@destination_full_path' created: plugin: callback source: post_date @@ -63,6 +86,5 @@ process: default_value: 1 destination: plugin: entity:file - source_path_property: attachment_url # Dependencies are dynamically generated. migration_dependencies: {} diff --git a/migrations/wordpress_content.yml b/migrations/wordpress_content.yml index 9865e47..13cace6 100644 --- a/migrations/wordpress_content.yml +++ b/migrations/wordpress_content.yml @@ -49,6 +49,10 @@ source: name: category label: Categories assigned to the content item selector: 'category[@domain="category"]/@nicename' + - + name: thumbnail_id + label: Thumbnail assigned to the content item + selector: wp:postmeta[wp:meta_key='_thumbnail_id']/wp:meta_value ids: post_id: type: integer diff --git a/src/WordPressMigrationGenerator.php b/src/WordPressMigrationGenerator.php index 26a8944..519d781 100644 --- a/src/WordPressMigrationGenerator.php +++ b/src/WordPressMigrationGenerator.php @@ -47,6 +47,13 @@ class WordPressMigrationGenerator { protected $categoriesID = ''; /** + * ID of the configuration entity for attachment migration. + * + * @var string + */ + protected $attachmentID = ''; + + /** * Constructs a WordPress migration generator, using provided configuration. * * @todo: Validate inputs (e.g., make sure post type exists). @@ -126,19 +133,14 @@ class WordPressMigrationGenerator { } // 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); + $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); - 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'; @@ -217,6 +219,14 @@ class WordPressMigrationGenerator { $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; diff --git a/wordpress_migrate_ui/src/Form/ImageSelectForm.php b/wordpress_migrate_ui/src/Form/ImageSelectForm.php new file mode 100644 index 0000000..b19649b --- /dev/null +++ b/wordpress_migrate_ui/src/Form/ImageSelectForm.php @@ -0,0 +1,69 @@ +getTemporaryValue('wizard'); + unset($cached_values['image_field']); + $form_state->setTemporaryValue('wizard', $cached_values); + + $form['overview'] = [ + '#markup' => $this->t('Here you may choose the Drupal image field to import Wordpress featured images into.'), + ]; + + $field_map = \Drupal::service('entity_field.manager')->getFieldMap(); + $options = ['' => $this->t('Do not import')]; + foreach($field_map as $entity_type => $fields) { + if ($entity_type == 'node') { + foreach($fields as $field_name => $field_settings) { + if ($field_settings['type'] == 'image') { + $options[$field_name] = $field_name; + } + } + } + } + + $form['image_field'] = [ + '#type' => 'select', + '#title' => $this->t('Import WordPress featured images in'), + '#options' => $options, + ]; + + return $form; + } + + /** + * {@inheritdoc} + */ + public function validateForm(array &$form, FormStateInterface $form_state) { + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + $cached_values = $form_state->getTemporaryValue('wizard'); + $cached_values['image_field'] = $form_state->getValue('image_field'); + $form_state->setTemporaryValue('wizard', $cached_values); + } + +} diff --git a/wordpress_migrate_ui/src/Wizard/ImportWizard.php b/wordpress_migrate_ui/src/Wizard/ImportWizard.php index 7f5c5ca..26e0905 100644 --- a/wordpress_migrate_ui/src/Wizard/ImportWizard.php +++ b/wordpress_migrate_ui/src/Wizard/ImportWizard.php @@ -29,6 +29,10 @@ class ImportWizard extends FormWizardBase { 'form' => 'Drupal\wordpress_migrate_ui\Form\ContentSelectForm', 'title' => $this->t('Content'), ], + 'image_select' => [ + 'form' => 'Drupal\wordpress_migrate_ui\Form\ImageSelectForm', + 'title' => $this->t('Featured images'), + ], ]; // Dynamically add the content migration(s) that have been configured by // ContentSelectForm.