You are here

class MigratePrepareRow in Commerce Migrate 3.1.x

Same name and namespace in other branches
  1. 8.2 modules/commerce/src/EventSubscriber/MigratePrepareRow.php \Drupal\commerce_migrate_commerce\EventSubscriber\MigratePrepareRow
  2. 3.0.x modules/commerce/src/EventSubscriber/MigratePrepareRow.php \Drupal\commerce_migrate_commerce\EventSubscriber\MigratePrepareRow

Handles migrate_plus prepare row event.

@package Drupal\commerce_migrate_commerce\EventSubscriber

Hierarchy

  • class \Drupal\commerce_migrate_commerce\EventSubscriber\MigratePrepareRow implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of MigratePrepareRow

1 string reference to 'MigratePrepareRow'
commerce_migrate_commerce.services.yml in modules/commerce/commerce_migrate_commerce.services.yml
modules/commerce/commerce_migrate_commerce.services.yml
1 service uses MigratePrepareRow
commerce_migrate_commerce.migrate_prepare_row in modules/commerce/commerce_migrate_commerce.services.yml
Drupal\commerce_migrate_commerce\EventSubscriber\MigratePrepareRow

File

modules/commerce/src/EventSubscriber/MigratePrepareRow.php, line 16

Namespace

Drupal\commerce_migrate_commerce\EventSubscriber
View source
class MigratePrepareRow implements EventSubscriberInterface {

  /**
   * Product node types.
   *
   * @var array
   */
  protected $productTypes = [];

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[MigrateEvents::PREPARE_ROW][] = 'prepareRow';
    return $events;
  }

  /**
   * Responds to prepare row event.
   *
   * @param \Drupal\migrate_plus\Event\MigratePrepareRowEvent $event
   *   The event.
   */
  public function prepareRow(MigratePrepareRowEvent $event) {
    $migration = $event
      ->getMigration();
    $row = $event
      ->getRow();
    $source_plugin = $migration
      ->getSourcePlugin();

    // Field instance, field formatter, field instance widget settings.
    if (is_a($source_plugin, FieldInstance::class)) {

      // If this is node entity then set source property 'commerce1_entity_type'
      // to indicate if this is a product display node or not.
      $row
        ->setSourceProperty('commerce1_entity_type', $row
        ->getSourceProperty('entity_type'));
      if ($row
        ->getSourceProperty('entity_type') === 'node') {
        if (in_array($row
          ->getSourceProperty('bundle'), $this
          ->getProductTypes($event))) {
          $row
            ->setSourceProperty('commerce1_entity_type', 'product_display');
        }
      }
    }
  }

  /**
   * Helper to get the product types from the source database.
   *
   * @param \Drupal\migrate_plus\Event\MigratePrepareRowEvent $event
   *   The event.
   *
   * @return array
   *   An array of product type names.
   */
  protected function getProductTypes(MigratePrepareRowEvent $event) {
    if (!empty($this->productTypes)) {
      return $this->productTypes;
    }

    /** @var \Drupal\migrate\Plugin\Migration $migration */
    $migration = $event
      ->getMigration();

    /** @var \Drupal\migrate\Plugin\migrate\source\SqlBase $source_plugin */
    $source_plugin = $migration
      ->getSourcePlugin();
    if (method_exists($source_plugin, 'getDatabase')) {
      $connection = $source_plugin
        ->getDatabase();
      if ($connection
        ->schema()
        ->tableExists('node_type')) {
        $query = $connection
          ->select('commerce_product_type', 'pt')
          ->fields('pt', [
          'type',
        ]);
        $product_node_types = $query
          ->execute()
          ->fetchCol();
        return $product_node_types;
      }
    }
    return [];
  }

  /**
   * Get the fields that are commerce product attributes.
   *
   * Attributes use taxonomy term reference fields with options.
   *
   * @see https://drupalcommerce.org/user-guide/product-attributes-variations
   *
   * @return array
   *   An array of taxonomy vocabularies that are product attributes..
   */
  protected function getAttributes() {
    $source_plugin = MigrationDeriverTrait::getSourcePlugin('d7_field_instance');
    $attributes = [];
    foreach ($source_plugin as $row) {
      if ($row
        ->getSourceProperty('entity_type') == 'commerce_product' && $row
        ->getSourceProperty('type') == 'taxonomy_term_reference' && $row
        ->getSourceProperty('widget')['type'] == 'options_select') {
        $name = str_replace('field_', '', $row
          ->getSourceProperty('field_name'));
        $attributes[] = $name;
      }
    }
    return array_unique($attributes);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MigratePrepareRow::$productTypes protected property Product node types.
MigratePrepareRow::getAttributes protected function Get the fields that are commerce product attributes.
MigratePrepareRow::getProductTypes protected function Helper to get the product types from the source database.
MigratePrepareRow::getSubscribedEvents public static function
MigratePrepareRow::prepareRow public function Responds to prepare row event.