You are here

class MigrateProduct in Commerce Migrate 8.2

Same name and namespace in other branches
  1. 3.1.x src/EventSubscriber/MigrateProduct.php \Drupal\commerce_migrate\EventSubscriber\MigrateProduct
  2. 3.0.x src/EventSubscriber/MigrateProduct.php \Drupal\commerce_migrate\EventSubscriber\MigrateProduct

Handles product and product variation references.

@package \Drupal\commerce_migrate\EventSubscriber

Hierarchy

  • class \Drupal\commerce_migrate\EventSubscriber\MigrateProduct implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of MigrateProduct

1 string reference to 'MigrateProduct'
commerce_migrate.services.yml in ./commerce_migrate.services.yml
commerce_migrate.services.yml
1 service uses MigrateProduct
commerce_migrate.migrate_product in ./commerce_migrate.services.yml
Drupal\commerce_migrate\EventSubscriber\MigrateProduct

File

src/EventSubscriber/MigrateProduct.php, line 15

Namespace

Drupal\commerce_migrate\EventSubscriber
View source
class MigrateProduct implements EventSubscriberInterface {

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

  /**
   * Reacts to the PRE_ROW_DELETE event.
   *
   * For commerce_product rollbacks we want to preserve the product variations
   * and prevent the 'usual' commerce behavior of deleting all variations
   * belonging to a product when deleting that product. That is done by
   * unsetting the variations field on the product and saving the product
   * before it is deleted.
   *
   * @param \Drupal\migrate\Event\MigrateRowDeleteEvent $event
   *   The event.
   */
  public function onPreRowDelete(MigrateRowDeleteEvent $event) {
    $destination_config = $event
      ->getMigration()
      ->getDestinationConfiguration();
    if ($destination_config['plugin'] === "entity:commerce_product") {
      $product_ids = $event
        ->getDestinationIdValues();
      foreach ($product_ids as $product_id) {

        /** @var \Drupal\commerce_product\Entity\product $product */
        $product = Product::load($product_id);
        if ($product && $product
          ->hasVariations()) {

          // Save the product, keeping the variations.
          $product
            ->setVariations([]);
          $product
            ->save();
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MigrateProduct::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
MigrateProduct::onPreRowDelete public function Reacts to the PRE_ROW_DELETE event.