You are here

protected function MediaEntityGenerator::initializeIterator in Migrate File Entities to Media Entities 8

Initializes the iterator with the source data.

Return value

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

Overrides SourcePluginBase::initializeIterator

File

src/Plugin/migrate/source/MediaEntityGenerator.php, line 155

Class

MediaEntityGenerator
Returns bare-bones information about every available file entity.

Namespace

Drupal\migrate_file_to_media\Plugin\migrate\source

Code

protected function initializeIterator() {
  $entityDefinition = $this->entityTypeManager
    ->getDefinition($this->configuration['entity_type']);
  $bundleKey = $entityDefinition
    ->getKey('bundle');
  $files_found = [];
  foreach ($this->sourceFields as $name => $source_field) {
    $query = $this->entityTypeManager
      ->getStorage($this->configuration['entity_type'])
      ->getQuery();
    if (!empty($bundleKey)) {
      $query
        ->condition($bundleKey, $this->configuration['bundle']);
    }
    $query
      ->condition("{$name}.target_id", 0, '>', $this->configuration['langcode']);

    // Also migrate unpublished and restricted entities.
    $query
      ->accessCheck(FALSE);
    $results = $query
      ->execute();
    if ($results) {
      $entitites = $this->entityTypeManager
        ->getStorage($this->configuration['entity_type'])
        ->loadMultiple($results);
      foreach ($entitites as $entity) {
        $original_entity = NULL;
        if ($entity
          ->hasTranslation($this->configuration['langcode'])) {
          $original_entity = $entity
            ->createDuplicate();
          $entity = $entity
            ->getTranslation($this->configuration['langcode']);
        }
        foreach ($entity->{$name}
          ->getValue() as $reference) {
          $data = [
            'nid' => $entity
              ->id(),
            'target_id' => $reference['target_id'],
            'alt' => isset($reference['alt']) ? $reference['alt'] : NULL,
            'title' => isset($reference['title']) ? $reference['title'] : NULL,
            'display' => isset($reference['display']) ? $reference['display'] : NULL,
            'description' => isset($reference['description']) ? $reference['description'] : NULL,
            'langcode' => $this->configuration['langcode'],
            'entity' => $entity,
          ];
          if ($original_entity) {
            $original_ref = $original_entity->{$name}
              ->getValue()[0];
            $data['source_language_target_id'] = $original_ref['target_id'];
          }
          $files_found[] = $data;
        }
      }
    }
  }
  return new \ArrayIterator($files_found);
}