You are here

function commerce_migrate_commerce_migration_plugins_alter in Commerce Migrate 3.1.x

Same name and namespace in other branches
  1. 8.2 modules/commerce/commerce_migrate_commerce.module \commerce_migrate_commerce_migration_plugins_alter()
  2. 3.0.x modules/commerce/commerce_migrate_commerce.module \commerce_migrate_commerce_migration_plugins_alter()

Implements hook_migration_plugins_alter().

File

modules/commerce/commerce_migrate_commerce.module, line 51
Contains commerce_migrate_commerce.module.

Code

function commerce_migrate_commerce_migration_plugins_alter(array &$migrations) {
  foreach ($migrations as $key => &$migration) {

    // Do not alter a migration that is already configured.
    if (strstr($key, 'migration_config_deriver:')) {
      continue;
    }

    /** @var \Drupal\migrate\Plugin\MigratePluginManager $migration_plugin_manager */
    $migration_plugin_manager = \Drupal::service('plugin.manager.migration');
    $migration_stub = $migration_plugin_manager
      ->createStubMigration($migration);

    /** @var \Drupal\migrate\Plugin\MigrateSourcePluginManager $source_plugin_manager */
    $source_plugin_manager = \Drupal::service('plugin.manager.migrate.source');
    $source = NULL;
    $configuration = $migration['source'];
    $source = $source_plugin_manager
      ->createInstance($migration['source']['plugin'], $configuration, $migration_stub);
    if ($source) {
      if (is_a($source, Node::class)) {

        // This is a node or node revision migration.
        if (isset($migration['source']['node_type'])) {

          // Unset migrations for nodes that are product displays.
          $node_type = $migration['source']['node_type'];
          $product_node_types = _commerce_migrate_commerce_get_product_node_types($migrations);
          if (in_array($node_type, $product_node_types)) {
            unset($migrations[$key]);
          }
        }
      }
      if (is_a($migration['class'], FieldMigration::class, TRUE)) {

        // Field storage.
        if (is_a($source, Field::class)) {
          $migration['source']['plugin'] = 'commerce1_field';
          $migration['process']['entity_type'] = _commerce_migrate_commerce_get_entity_type('commerce1_entity_type');
          $migration['migration_dependencies']['required'][] = 'commerce1_product_type';
          $migration['migration_dependencies']['required'][] = 'commerce1_product_variation_type';
          $migration['process']['field_name'] = [
            'plugin' => 'commerce1_field_name',
          ];
          $migration['process']['settings/target_type'] = [
            'plugin' => 'commerce1_attribute_target_type',
          ];
        }

        // Field instance.
        if (get_class($source) === FieldInstance::class) {
          $migration['process']['entity_type'] = _commerce_migrate_commerce_get_entity_type('commerce1_entity_type');
          $migration['process']['entity_type'][0]['map']['commerce_customer_profile'] = 'skip';
          $migration['process']['field_name'] = _commerce_migrate_commerce_get_field_name();

          // Add process to set the target_bundles setting for attributes.  This
          // must be after the process for the settings field.
          $attributes_process['settings/handler_settings'] = [
            'plugin' => 'commerce1_attribute_handler_setting',
          ];
          $migration['process'] += $attributes_process;
          $migration['migration_dependencies']['required'][] = 'commerce1_product_type';
          $migration['migration_dependencies']['required'][] = 'commerce1_product_variation_type';
        }

        // Field instance widget settings.
        if (is_a($source, FieldInstancePerFormDisplay::class)) {
          $migration['process']['entity_type'] = _commerce_migrate_commerce_get_entity_type('commerce1_entity_type');
          $migration['process']['entity_type'][0]['map']['commerce_customer_profile'] = 'skip';
          $migration['process']['field_name'] = _commerce_migrate_commerce_get_field_name();
          $migration['process']['workaround'] = [
            [
              'plugin' => 'static_map',
              'source' => '@options/type',
              'bypass' => 'true',
              'map' => [
                'inline_entity_form' => 'skip',
              ],
            ],
            [
              'plugin' => 'skip_on_value',
              'value' => 'skip',
              'method' => 'row',
            ],
          ];
        }

        // Field formatter.
        if (is_a($source, FieldInstancePerViewMode::class)) {
          $migration['process']['entity_type'] = _commerce_migrate_commerce_get_entity_type('commerce1_entity_type');
          $migration['process']['entity_type'][0]['map']['commerce_customer_profile'] = 'skip';
          $migration['process']['field_name'] = _commerce_migrate_commerce_get_field_name();
          $migration['process']['workaround'] = [
            [
              'plugin' => 'static_map',
              'source' => '@options/type',
              'bypass' => 'true',
              'map' => [
                'taxonomy_term_reference_plain' => 'skip',
                'image_delta' => 'skip',
                'cloud_zoom' => 'skip',
                'field_extractor' => 'skip',
                'commerce_cart_add_to_cart_form' => 'skip',
                'commerce_fancy_attributes_color' => 'skip',
                'title_linked' => 'skip',
              ],
            ],
            [
              'plugin' => 'skip_on_value',
              'value' => 'skip',
              'method' => 'row',
            ],
          ];
        }
      }

      // View mode.
      if (is_a($source, ViewMode::class)) {
        $migration['source']['plugin'] = 'commerce1_view_mode';

        // Add map for the destination entity type.
        // Use the source entity type here because the source plugin,
        // 'commerce1_view_mode', will add rows for the product displays.
        $migration['process']['targetEntityType'] = _commerce_migrate_commerce_get_entity_type('entity_type');
        $migration['process']['targetEntityType'][0]['map']['commerce_customer_profile'] = 'skip';
      }

      // Taxonomy Terms.
      if (is_a($source, Term::class) && !is_a($source, TermLocalizedTranslation::class)) {
        $attributes = _commerce_migrate_commerce_get_attributes();
        if (isset($migration['source']['bundle'])) {
          if (in_array($migration['source']['bundle'], $attributes)) {

            // This is a product attribute.
            $migration['process']['attribute'] = '@vid';
            $migration['destination']['plugin'] = 'entity:commerce_product_attribute_value';
          }
        }
      }

      // The field migration uses the source field name as the destination name
      // but for profiles the destination name is 'address'.
      if (is_a($source, Profile::class)) {
        if (isset($migration['process']['commerce_customer_address'])) {
          $migration['process']['address'] = $migration['process']['commerce_customer_address'];
          unset($migration['process']['commerce_customer_address']);
        }
      }
    }
  }
}