You are here

class Category in Commerce Migrate 8.2

Same name and namespace in other branches
  1. 3.1.x modules/magento/src/Plugin/migrate/source/magento2/Category.php \Drupal\commerce_migrate_magento\Plugin\migrate\source\magento2\Category
  2. 3.0.x modules/magento/src/Plugin/migrate/source/magento2/Category.php \Drupal\commerce_migrate_magento\Plugin\migrate\source\magento2\Category

Yields each taxonomy vocabulary.

The cell containing the Magento categories is a comma separated list of the categories assigned to the product variation in this row. Each category is a string of categories with a forward slash delimiter. The first one is the top level category and it is used as the taxonomy vocabulary. The following categories are terms in that vocabulary listed in hierarchical order.

Consider this example.


Default Category/Gear/Bags,Special Category/Collections/New Yoga Collection

In this case, 'Default Category' and 'Special Category' are the top level and will be migrated to vocabularies, with machine name 'default_category' and 'special_category' respectively.

Plugin annotation


@MigrateSource(
  id = "magento2_category_csv"
)

Hierarchy

  • class \Drupal\commerce_migrate_magento\Plugin\migrate\source\magento2\Category extends \Drupal\migrate_source_csv\Plugin\migrate\source\CSV

Expanded class hierarchy of Category

3 string references to 'Category'
FieldInstanceTest::testFieldInstances in modules/commerce/tests/src/Kernel/Migrate/commerce1/FieldInstanceTest.php
Tests migrating D7 field instances to field_config entities.
ProductTest::setUp in modules/csv_example/tests/src/Kernel/Migrate/ProductTest.php
TaxonomyTermTest::setUp in modules/csv_example/tests/src/Kernel/Migrate/TaxonomyTermTest.php

File

modules/magento/src/Plugin/migrate/source/magento2/Category.php, line 28

Namespace

Drupal\commerce_migrate_magento\Plugin\migrate\source\magento2
View source
class Category extends CSV {

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

  /**
   * Prepare one row per taxonomy vocabulary in the source data.
   *
   * @param \Generator $file
   *   The source CSV file object.
   *
   * @codingStandardsIgnoreStart
   *
   * @return \Generator
   *   A new row, one for each unique vocabulary.
   *
   * @codingStandardsIgnoreEnd
   */
  public function getYield(\Generator $file) {
    foreach ($file as $row) {
      $new_row = [];
      $categoryGroup = explode(',', $row['categories']);
      foreach ($categoryGroup as $category) {
        $new_row['vocabulary'] = strstr($category, '/', TRUE);
        if ($this
          ->rowUnique($new_row)) {
          (yield $new_row);
        }
      }
    }
  }

  /**
   * Tests if the row is unique.
   *
   * @param array $row
   *   An array of attribute_name and attribute_value for the current row.
   *
   * @return bool
   *   Return TRUE if the row is unique, FALSE if it is not unique.
   */
  protected function rowUnique(array $row) {
    static $unique_rows = [];
    if (in_array($row['vocabulary'], $unique_rows)) {
      return FALSE;
    }
    array_push($unique_rows, $row['vocabulary']);
    return TRUE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Category::getYield public function Prepare one row per taxonomy vocabulary in the source data.
Category::initializeIterator public function
Category::rowUnique protected function Tests if the row is unique.