You are here

public function Image::getYield in Commerce Migrate 8.2

Same name in this branch
  1. 8.2 modules/csv_example/src/Plugin/migrate/source/Image.php \Drupal\commerce_migrate_csv_example\Plugin\migrate\source\Image::getYield()
  2. 8.2 modules/magento/src/Plugin/migrate/source/magento2/Image.php \Drupal\commerce_migrate_magento\Plugin\migrate\source\magento2\Image::getYield()
Same name and namespace in other branches
  1. 3.1.x modules/magento/src/Plugin/migrate/source/magento2/Image.php \Drupal\commerce_migrate_magento\Plugin\migrate\source\magento2\Image::getYield()
  2. 3.0.x modules/magento/src/Plugin/migrate/source/magento2/Image.php \Drupal\commerce_migrate_magento\Plugin\migrate\source\magento2\Image::getYield()

Prepare one row per image file in the source row.

@codingStandardsIgnoreStart

@codingStandardsIgnoreEnd

Parameters

\Generator $file: The source CSV file object.

Return value

\Generator A new row, one for each filename in the source image column.

1 call to Image::getYield()
Image::initializeIterator in modules/magento/src/Plugin/migrate/source/magento2/Image.php

File

modules/magento/src/Plugin/migrate/source/magento2/Image.php, line 40

Class

Image
Yields each image and sku.

Namespace

Drupal\commerce_migrate_magento\Plugin\migrate\source\magento2

Code

public function getYield(\Generator $file) {

  // The Magento 2 CSV has 4 sets of columns for single images.
  $image_types = [
    [
      'base_image',
      'base_image_label',
    ],
    [
      'small_image',
      'small_image_label',
    ],
    [
      'thumbnail_image',
      'thumbnail_image_label',
    ],
    [
      'swatch_image',
      'swatch_image_label',
    ],
  ];
  foreach ($file as $row) {
    foreach ($image_types as $image_type) {
      $new_row = [];

      // The sku is a key.
      $new_row['sku'] = trim($row['sku']);

      // Base image.
      if (!empty($row[$image_type[0]])) {

        // The image is a key.
        $new_row['image'] = trim($row[$image_type[0]]);
        $new_row['label'] = trim($row[$image_type[1]]);
        (yield $new_row);
      }
    }

    // The additional images column may have more that one image per cell.
    $additional_images = explode(',', $row['additional_images']);
    $additional_image_labels = explode(',', $row['additional_image_labels']);
    foreach ($additional_images as $index => $value) {
      $new_row = [];
      $new_row['sku'] = trim($row['sku']);

      // The image is a key.
      $new_row['image'] = trim($value);
      if (isset($additional_image_labels[$index])) {
        $new_row['label'] = trim($additional_image_labels[$index]);
      }
      (yield $new_row);
    }
  }
}