You are here

public function Field::initializeIterator in Commerce Migrate 8.2

Same name in this branch
  1. 8.2 modules/ubercart/src/Plugin/migrate/source/uc7/Field.php \Drupal\commerce_migrate_ubercart\Plugin\migrate\source\uc7\Field::initializeIterator()
  2. 8.2 modules/commerce/src/Plugin/migrate/source/commerce1/Field.php \Drupal\commerce_migrate_commerce\Plugin\migrate\source\commerce1\Field::initializeIterator()
Same name and namespace in other branches
  1. 3.1.x modules/ubercart/src/Plugin/migrate/source/uc7/Field.php \Drupal\commerce_migrate_ubercart\Plugin\migrate\source\uc7\Field::initializeIterator()
  2. 3.0.x modules/ubercart/src/Plugin/migrate/source/uc7/Field.php \Drupal\commerce_migrate_ubercart\Plugin\migrate\source\uc7\Field::initializeIterator()

Initializes the iterator with the source data.

Return value

\Iterator Returns an iteratable object of data for this source.

Overrides SqlBase::initializeIterator

File

modules/ubercart/src/Plugin/migrate/source/uc7/Field.php, line 26

Class

Field
Ubercart 7 field source from database.

Namespace

Drupal\commerce_migrate_ubercart\Plugin\migrate\source\uc7

Code

public function initializeIterator() {
  $this->productTypes = $this
    ->getProductTypes();
  $results = $this
    ->prepareQuery()
    ->execute();
  $rows = [];
  foreach ($results as $result) {

    // Get all the instances of this field.
    $field_name = $result['field_name'];

    // Get all the instances of this field.
    $query = $this
      ->select('field_config_instance', 'fci')
      ->fields('fci', [
      'bundle',
    ])
      ->condition('fc.active', 1)
      ->condition('fc.storage_active', 1)
      ->condition('fc.deleted', 0)
      ->condition('fci.deleted', 0)
      ->condition('fci.entity_type', 'node');
    $query
      ->join('field_config', 'fc', 'fci.field_id = fc.id');
    $query
      ->condition('fci.field_name', $field_name);
    $node_bundles = $query
      ->execute()
      ->fetchCol();

    // Determine if the field is on both a product type and node, or just one
    // of product type or node.
    $product_node_count = 0;
    foreach ($node_bundles as $bundle) {
      if (in_array($bundle, $this->productTypes)) {
        $product_node_count++;
      }
    }
    $node_count = 0;
    foreach ($node_bundles as $bundle) {
      if ($bundle === 'node') {
        $node_count++;
      }
    }
    $result['commerce_product'] = 0;
    if ($product_node_count > 0) {

      // If all bundles for this field are product types, then add the
      // commerce product flag to indicate this is a product node.
      if ($product_node_count == count($node_bundles)) {
        $result['commerce_product'] = 1;
      }
      else {

        // This field is on both a product node and a non product node so add
        // a new row to create the field storage on the commerce_product
        // entity.
        $add_row = $result;
        $add_row['commerce_product'] = 1;
        $rows[] = $add_row;
      }
    }
    $rows[] = $result;
  }
  return new \ArrayIterator($rows);
}