You are here

protected function MediaMigrateGenerator::interact in Migrate File Entities to Media Entities 8

File

src/Generators/MediaMigrateGenerator.php, line 44

Class

MediaMigrateGenerator
Automatically generates yml files for migrations.

Namespace

Drupal\migrate_file_to_media\Generators

Code

protected function interact(InputInterface $input, OutputInterface $output) {

  /** @var \Symfony\Component\Console\Question\Question[] $questions */
  $questions = Utils::defaultPluginQuestions() + [
    'migration_group' => new Question('Migration Group', 'media'),
    'entity_type' => new Question('Entity Type', 'node'),
    'source_bundle' => new Question('Source Bundle', ''),
    'source_field_name' => new Question('Source Field Names (comma separated)', 'field_image'),
    'target_bundle' => new Question('Target Media Type', 'image'),
    'target_field' => new Question('Target Field', 'field_media_image'),
    'lang_code' => new Question('Language Code', 'en'),
    'translation_languages' => new Question('Translation languages (comma separated)', 'none'),
  ];
  $questions['plugin_id']
    ->setValidator([
    MediaMigrateGenerator::class,
    'validatePluginId',
  ]);
  $vars =& $this
    ->collectVars($input, $output, $questions);
  $vars['translation_language'] = NULL;
  if ($vars['translation_languages']) {
    $translation_languages = array_map('trim', array_unique(explode(',', strtolower($vars['translation_languages']))));

    // Validate the default language was not included in the translation languages
    foreach ($translation_languages as $key => $language) {
      if ($language == $vars['lang_code']) {
        unset($translation_languages[$key]);
      }
    }
    $vars['translation_languages'] = $translation_languages;
  }
  if ($vars['source_field_name']) {
    $vars['source_field_name'] = array_map('trim', explode(',', strtolower($vars['source_field_name'])));
  }

  // ID Key for the entity type (nid for node, id for paragraphs).
  $entityType = $this->entityTypeManager
    ->getDefinition($vars['entity_type']);
  $vars['id_key'] = $entityType
    ->getKey('id');
  $this
    ->addFile()
    ->path('config/install/migrate_plus.migration.{plugin_id}_step1.yml')
    ->template('media-migration-step1.yml.twig')
    ->vars($vars);

  // Validates if there are translation languages and includes a new variable to add translations or not
  $vars['has_translation'] = count($vars['translation_languages']) > 0 && $vars['translation_languages'][0] != 'none';
  $this
    ->addFile()
    ->path('config/install/migrate_plus.migration.{plugin_id}_step2.yml')
    ->template('media-migration-step2.yml.twig')
    ->vars($vars);
  foreach ($vars['translation_languages'] as $language) {
    if ($language == 'none' || $language == $vars['lang_code']) {
      continue;
    }
    $vars['source_lang_code'] = $vars['lang_code'];
    $vars['translation_language'] = $vars['lang_code'] = $language;
    $this
      ->addFile()
      ->path("config/install/migrate_plus.migration.{plugin_id}_step1_{$language}.yml")
      ->template('media-migration-step1.yml.twig')
      ->vars($vars);
  }
}