You are here

protected function MediaEntityGeneratorD7::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 SqlBase::initializeIterator

File

src/Plugin/migrate/source/MediaEntityGeneratorD7.php, line 200

Class

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

Namespace

Drupal\migrate_file_to_media\Plugin\migrate\source

Code

protected function initializeIterator() {
  $query_files = $this
    ->select('file_managed', 'f')
    ->fields('f')
    ->condition('uri', 'temporary://%', 'NOT LIKE')
    ->orderBy('f.timestamp');
  $all_files = $query_files
    ->execute()
    ->fetchAllAssoc('fid');
  $files_found = [];
  foreach ($this->sourceFields as $name => $source_field) {
    $parent_iterator = parent::initializeIterator();
    foreach ($parent_iterator as $entity) {
      $nid = $entity['nid'];
      $vid = $entity['vid'];
      $langcode = !empty($this->configuration['langcode']) ? $this->configuration['langcode'] : NULL;
      $field_value = $this
        ->getFieldValues('node', $name, $nid, $vid, $langcode);
      foreach ($field_value as $reference) {

        // Support remote file urls.
        $file_url = $all_files[$reference['fid']]['uri'];
        if (!empty($this->configuration['d7_file_url'])) {
          $file_url = str_replace('public://', '', $file_url);
          $file_path = UrlHelper::encodePath($file_url);
          $file_url = $this->configuration['d7_file_url'] . $file_path;
        }
        if (!empty($all_files[$reference['fid']]['uri'])) {
          $files_found[] = [
            'nid' => $entity['nid'],
            'target_id' => $reference['fid'],
            '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,
            'file_name' => $all_files[$reference['fid']]['filename'],
            'file_path' => $file_url,
          ];
        }
      }
    }
  }
  return new \ArrayIterator($files_found);
}