You are here

class Image 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
  2. 8.2 modules/magento/src/Plugin/migrate/source/magento2/Image.php \Drupal\commerce_migrate_magento\Plugin\migrate\source\magento2\Image
Same name and namespace in other branches
  1. 3.1.x modules/csv_example/src/Plugin/migrate/source/Image.php \Drupal\commerce_migrate_csv_example\Plugin\migrate\source\Image
  2. 3.0.x modules/csv_example/src/Plugin/migrate/source/Image.php \Drupal\commerce_migrate_csv_example\Plugin\migrate\source\Image

Yields each image and sku.

There are several columns for image names in the example source. Three are images of the product variation, three are for thumbnails for videos, and two are for the CTA image. Create a new row for image and SKU where both are non empty. See import_image for the column names.

Plugin annotation


@MigrateSource(
  id = "csv_example_image"
)

Hierarchy

  • class \Drupal\commerce_migrate_csv_example\Plugin\migrate\source\Image extends \Drupal\migrate_source_csv\Plugin\migrate\source\CSV

Expanded class hierarchy of Image

1 string reference to 'Image'
FieldInstanceTest::testFieldInstances in modules/commerce/tests/src/Kernel/Migrate/commerce1/FieldInstanceTest.php
Tests migrating D7 field instances to field_config entities.

File

modules/csv_example/src/Plugin/migrate/source/Image.php, line 19

Namespace

Drupal\commerce_migrate_csv_example\Plugin\migrate\source
View source
class Image extends CSV {

  /**
   * {@inheritdoc}
   */
  public function initializeIterator() {
    $file = parent::initializeIterator();
    return $this
      ->getYield($file);
  }

  /**
   * Prepare one row per image file in the source row.
   *
   * @param \Generator $file
   *   The source CSV file object.
   *
   * @codingStandardsIgnoreStart
   *
   * @return \Generator
   *   A new row, one for each filename in the source image column.
   *
   * @codingStandardsIgnoreEnd
   */
  public function getYield(\Generator $file) {
    foreach ($file as $row) {
      if (!empty($row['sku'])) {

        // There is a SKU so let's check for images.
        $new_row = [];
        $new_row['sku'] = trim($row['sku']);

        // Product variation images.
        for ($i = 1; $i < 4; $i++) {
          $new_row['image'] = trim($row["image{$i}"]);
          if (!empty($new_row['image'])) {
            (yield $new_row);
          }
        }

        // Video thumbnails.
        for ($i = 1; $i < 4; $i++) {
          $new_row['image'] = trim($row["thumbnail{$i}"]);
          if (!empty($new_row['image'])) {
            (yield $new_row);
          }
        }

        // Call to action images.
        for ($i = 1; $i < 3; $i++) {
          $new_row['image'] = trim($row["cta_image{$i}"]);
          if (!empty($new_row['image'])) {
            (yield $new_row);
          }
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Image::getYield public function Prepare one row per image file in the source row.
Image::initializeIterator public function