class ProductAttribute in Commerce Migrate 3.1.x
Same name and namespace in other branches
- 8.2 modules/magento/src/Plugin/migrate/source/magento2/ProductAttribute.php \Drupal\commerce_migrate_magento\Plugin\migrate\source\magento2\ProductAttribute
- 3.0.x modules/magento/src/Plugin/migrate/source/magento2/ProductAttribute.php \Drupal\commerce_migrate_magento\Plugin\migrate\source\magento2\ProductAttribute
Yields each product attribute.
The cell containing the Magento attributes is a comma separated list of all the attributes assigned to this product variation. Each cell contains any number of attribute sets as described in the example below.
Consider this example.
activity="Gym"|"Hiking"|"Trail"|"Urban",erin_recommends="Yes"
In this case, 'activity' is an attribute and 'Gym', 'Hiking',, 'Trail' and 'Urban' are it's attribute options. Also, 'erin_recommends' is an attribute with a 'Yes' option. There may be more options for an attribute in other rows.
Plugin annotation
@MigrateSource(
  id = "magento2_product_attribute_csv"
)
Hierarchy
- class \Drupal\commerce_migrate_magento\Plugin\migrate\source\magento2\ProductAttribute extends \Drupal\migrate_source_csv\Plugin\migrate\source\CSV
Expanded class hierarchy of ProductAttribute
File
- modules/magento/ src/ Plugin/ migrate/ source/ magento2/ ProductAttribute.php, line 27 
Namespace
Drupal\commerce_migrate_magento\Plugin\migrate\source\magento2View source
class ProductAttribute extends CSV {
  /**
   * {@inheritdoc}
   */
  public function initializeIterator() {
    $file = parent::initializeIterator();
    return $this
      ->getYield($file);
  }
  /**
   * Prepare one row per attribute.
   *
   * @param \Generator $file
   *   The source CSV file object.
   *
   * @codingStandardsIgnoreStart
   *
   * @return \Generator
   *   A new row, one for each attribute and option pair.
   *
   * @codingStandardsIgnoreEnd
   */
  public function getYield(\Generator $file) {
    foreach ($file as $row) {
      $new_row = [];
      $attributeSet = explode(',', $row['additional_attributes']);
      foreach ($attributeSet as $set) {
        $new_row['attribute'] = strstr($set, '=', 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['attribute'], $unique_rows)) {
      return FALSE;
    }
    array_push($unique_rows, $row['attribute']);
    return TRUE;
  }
}Members
| Name   | Modifiers | Type | Description | Overrides | 
|---|---|---|---|---|
| ProductAttribute:: | public | function | Prepare one row per attribute. | |
| ProductAttribute:: | public | function | ||
| ProductAttribute:: | protected | function | Tests if the row is unique. | 
