You are here

public function OrderItemDeriver::getDerivativeDefinitions in Commerce Migrate 8.2

Same name and namespace in other branches
  1. 3.1.x modules/commerce/src/Plugin/migrate/OrderItemDeriver.php \Drupal\commerce_migrate_commerce\Plugin\migrate\OrderItemDeriver::getDerivativeDefinitions()
  2. 3.0.x modules/commerce/src/Plugin/migrate/OrderItemDeriver.php \Drupal\commerce_migrate_commerce\Plugin\migrate\OrderItemDeriver::getDerivativeDefinitions()

Gets the definition of all derivatives of a base plugin.

Parameters

array $base_plugin_definition: The definition array of the base plugin.

Return value

array An array of full derivative definitions keyed on derivative id.

Overrides DeriverBase::getDerivativeDefinitions

See also

getDerivativeDefinition()

File

modules/commerce/src/Plugin/migrate/OrderItemDeriver.php, line 72

Class

OrderItemDeriver
Deriver for Commerce 1 line items based on line item types.

Namespace

Drupal\commerce_migrate_commerce\Plugin\migrate

Code

public function getDerivativeDefinitions($base_plugin_definition) {

  // @todo: Convert to new trait. See https://www.drupal.org/node/2951550.
  $order_item_types = static::getSourcePlugin('commerce1_order_item_type');
  try {
    $order_item_types
      ->checkRequirements();
  } catch (RequirementsException $e) {

    // If the d7_order_item_type requirements failed, that means we do not
    // have a  Drupal source database configured - there is nothing to
    // generate.
    return parent::getDerivativeDefinitions($base_plugin_definition);
  }
  $fields = [];
  try {
    $source_plugin = static::getSourcePlugin('d7_field_instance');
    $source_plugin
      ->checkRequirements();

    // Read all field instance definitions in the source database.
    foreach ($source_plugin as $row) {
      if ($row
        ->getSourceProperty('entity_type') == 'commerce_line_item') {
        $fields[$row
          ->getSourceProperty('bundle')][$row
          ->getSourceProperty('field_name')] = $row
          ->getSource();
      }
    }
  } catch (RequirementsException $e) {

    // If checkRequirements() failed then the field module did not exist and
    // we do not have any fields. Therefore, $fields will be empty and below
    // we'll create a migration just for the line item properties.
  }
  try {
    foreach ($order_item_types as $row) {
      $line_item_type = $row
        ->getSourceProperty('type');

      // Ignore shipping line items because they become order adjustments.
      if ($line_item_type !== 'shipping' && $line_item_type !== 'commerce_discount') {
        $values = $base_plugin_definition;
        $values['label'] = t('@label (@type)', [
          '@label' => $values['label'],
          '@type' => $row
            ->getSourceProperty('name'),
        ]);
        $values['source']['line_item_type'] = $line_item_type;
        $values['destination']['default_bundle'] = $line_item_type;

        /** @var \Drupal\migrate\Plugin\MigrationInterface $migration */
        $migration = \Drupal::service('plugin.manager.migration')
          ->createStubMigration($values);
        $this->fieldDiscovery
          ->addBundleFieldProcesses($migration, 'commerce_line_item', $line_item_type);
        $this->derivatives[$line_item_type] = $migration
          ->getPluginDefinition();
      }
    }
  } catch (DatabaseExceptionWrapper $e) {

    // Once we begin iterating the source plugin it is possible that the
    // source tables will not exist. This can happen when the
    // MigrationPluginManager gathers up the migration definitions but we do
    // not actually have a Drupal 7 source database.
  }
  return parent::getDerivativeDefinitions($base_plugin_definition);
}