You are here

protected function FileEntitySourceFieldInstance::initializeIterator in Media Migration 8

Overrides FileEntityConfigSourceBase::initializeIterator

File

src/Plugin/migrate/source/d7/FileEntitySourceFieldInstance.php, line 30

Class

FileEntitySourceFieldInstance
Drupal 7 media source field instances source based on source database.

Namespace

Drupal\media_migration\Plugin\migrate\source\d7

Code

protected function initializeIterator() {
  $results = $this
    ->prepareQuery()
    ->execute()
    ->fetchAll();
  $rows = [];
  foreach ($results as $result) {
    [
      'type' => $type,
      'scheme' => $scheme,
      'file_extension' => $extension,
    ] = $result;
    if (!($dealer_plugin = $this->fileEntityDealerManager
      ->createInstanceFromTypeAndScheme($type, $scheme))) {
      continue;
    }
    $destination_media_type_id = $dealer_plugin
      ->getDestinationMediaTypeId();
    $source_values = $rows[$destination_media_type_id] ?? $result + [
      'types' => $type,
      'schemes' => $scheme,
      'file_extensions' => $extension,
    ];
    $source_values['types'] = implode(static::MULTIPLE_SEPARATOR, array_unique(array_merge(explode(static::MULTIPLE_SEPARATOR, $source_values['types']), [
      $type,
    ])));
    $source_values['schemes'] = implode(static::MULTIPLE_SEPARATOR, array_unique(array_merge(explode(static::MULTIPLE_SEPARATOR, $source_values['schemes']), [
      $scheme,
    ])));
    $source_values['file_extensions'] = implode(static::MULTIPLE_SEPARATOR, array_unique(array_merge(explode(static::MULTIPLE_SEPARATOR, $source_values['file_extensions']), [
      $extension,
    ])));
    $source_values['bundle'] = $destination_media_type_id;
    $source_values['bundle_label'] = $dealer_plugin
      ->getDestinationMediaTypeLabel();
    $source_values['source_plugin_id'] = $dealer_plugin
      ->getDestinationMediaSourcePluginId();
    $source_values['source_field_name'] = $dealer_plugin
      ->getDestinationMediaSourceFieldName();
    $source_values['source_field_label'] = $dealer_plugin
      ->getDestinationMediaTypeSourceFieldLabel();
    unset($source_values['type']);
    unset($source_values['scheme']);
    unset($source_values['extension']);
    $rows[$destination_media_type_id] = $source_values;
  }
  return new \ArrayIterator($rows);
}