You are here

class Attribute in Commerce Migrate 3.0.x

Same name in this branch
  1. 3.0.x modules/csv_example/src/Plugin/migrate/source/Attribute.php \Drupal\commerce_migrate_csv_example\Plugin\migrate\source\Attribute
  2. 3.0.x modules/ubercart/src/Plugin/migrate/source/Attribute.php \Drupal\commerce_migrate_ubercart\Plugin\migrate\source\Attribute
Same name and namespace in other branches
  1. 8.2 modules/csv_example/src/Plugin/migrate/source/Attribute.php \Drupal\commerce_migrate_csv_example\Plugin\migrate\source\Attribute
  2. 3.1.x modules/csv_example/src/Plugin/migrate/source/Attribute.php \Drupal\commerce_migrate_csv_example\Plugin\migrate\source\Attribute

Yields each attribute name and value pair.

There are four attribute name/attribute value pairs in the example source row. Create a new row for each non empty attribute name/value pair. See import_attribute for the column names.

Plugin annotation


@MigrateSource(
  id = "csv_example_attribute"
)

Hierarchy

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

Expanded class hierarchy of Attribute

File

modules/csv_example/src/Plugin/migrate/source/Attribute.php, line 18

Namespace

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

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

  /**
   * Prepares one row per attribute pair 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) {
      $new_row = [];
      for ($i = 1; $i < 5; $i++) {
        $new_row['attribute_name'] = trim($row["attribute_name{$i}"]);
        $new_row['attribute_value'] = trim($row["attribute_value{$i}"]);
        if (!empty($new_row['attribute_name']) && !empty($new_row['attribute_value'])) {
          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 = [];
    foreach ($unique_rows as $unique) {
      if ($unique['attribute_name'] === $row['attribute_name'] && $unique['attribute_value'] === $row['attribute_value']) {
        return FALSE;
      }
    }
    $unique_rows[] = $row;
    return TRUE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Attribute::getYield public function Prepares one row per attribute pair in the source row.
Attribute::initializeIterator public function
Attribute::rowUnique protected function Tests if the row is unique.