Attribute.php in Commerce Migrate 3.0.x
File
modules/csv_example/src/Plugin/migrate/source/Attribute.php
View source
<?php
namespace Drupal\commerce_migrate_csv_example\Plugin\migrate\source;
use Drupal\migrate_source_csv\Plugin\migrate\source\CSV;
class Attribute extends CSV {
public function initializeIterator() {
$file = parent::initializeIterator();
return $this
->getYield($file);
}
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);
}
}
}
}
}
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;
}
}
Classes
Name |
Description |
Attribute |
Yields each attribute name and value pair. |