You are here

protected function DrupalMigrationGenerator::createContentMigration in Drupal-to-Drupal data migration 8.3

Setup the migration for a given WordPress content type.

Parameters

$wordpress_type: WordPress content type - 'post' or 'page'.

1 call to DrupalMigrationGenerator::createContentMigration()
DrupalMigrationGenerator::createMigrations in src/DrupalMigrationGenerator.php
Creates a set of WordPress import migrations based on configuration settings.

File

src/DrupalMigrationGenerator.php, line 161

Class

DrupalMigrationGenerator
Functionality to construct Drupal migrations from broad configuration settings.

Namespace

Drupal\migrate_d2d

Code

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;
    }
  }
}