class MigrateProduct in Commerce Migrate 8.2
Same name and namespace in other branches
- 3.1.x src/EventSubscriber/MigrateProduct.php \Drupal\commerce_migrate\EventSubscriber\MigrateProduct
- 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'
1 service uses MigrateProduct
File
- src/EventSubscriber/ MigrateProduct.php, line 15 
Namespace
Drupal\commerce_migrate\EventSubscriberView 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
| Name   | Modifiers | Type | Description | Overrides | 
|---|---|---|---|---|
| MigrateProduct:: | public static | function | Returns an array of event names this subscriber wants to listen to. | |
| MigrateProduct:: | public | function | Reacts to the PRE_ROW_DELETE event. | 
