You are here

class FilePlainSourceFieldInstance in Media Migration 8

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

Plugin annotation


@MigrateSource(
  id = "d7_file_plain_source_field_instance",
  source_module = "file"
)

Hierarchy

Expanded class hierarchy of FilePlainSourceFieldInstance

File

src/Plugin/migrate/source/d7/FilePlainSourceFieldInstance.php, line 15

Namespace

Drupal\media_migration\Plugin\migrate\source\d7
View source
class FilePlainSourceFieldInstance extends FilePlainConfigSourceBase {

  /**
   * {@inheritdoc}
   */
  public function query() {
    $query = parent::query();
    $query
      ->addExpression($this
      ->getExtensionExpression(), 'file_extension');
    return $query;
  }

  /**
   * {@inheritdoc}
   */
  protected function initializeIterator() {
    $results = $this
      ->prepareQuery()
      ->execute()
      ->fetchAll();

    // Add the array of all instances using the same media type to each row.
    $rows = [];
    foreach ($results as $result) {
      [
        'mime' => $mime,
        'scheme' => $scheme,
        'file_extension' => $extension,
      ] = $result;
      if (!($dealer_plugin = $this->fileDealerManager
        ->createInstanceFromSchemeAndMime($scheme, $mime))) {
        continue;
      }
      $destination_media_type_id = $dealer_plugin
        ->getDestinationMediaTypeId();
      $source_values = $rows[$destination_media_type_id] ?? $result + [
        'mimes' => $mime,
        'schemes' => $scheme,
        'file_extensions' => $extension,
      ];
      $source_values['mimes'] = implode(static::MULTIPLE_SEPARATOR, array_unique(array_merge(explode(static::MULTIPLE_SEPARATOR, $source_values['mimes']), [
        $mime,
      ])));
      $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_filter(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();
      $rows[$destination_media_type_id] = $source_values;
      unset($source_values['file_extension']);
      unset($source_values['mime']);
      unset($source_values['scheme']);
      $rows[$destination_media_type_id] = $source_values;
    }
    return new \ArrayIterator($rows);
  }

  /**
   * {@inheritdoc}
   */
  public function prepareRow(Row $row) {
    [
      'mimes' => $mimes,
      'schemes' => $schemes,
    ] = $row
      ->getSource();
    $mime = explode(static::MULTIPLE_SEPARATOR, $mimes)[0];
    $scheme = explode(static::MULTIPLE_SEPARATOR, $schemes)[0];
    if (!($dealer_plugin = $this->fileDealerManager
      ->createInstanceFromSchemeAndMime($scheme, $mime))) {
      return FALSE;
    }
    $dealer_plugin
      ->prepareMediaSourceFieldInstanceRow($row, $this
      ->getDatabase());
    return parent::prepareRow($row);
  }

  /**
   * {@inheritdoc}
   */
  public function fields() {
    return [
      'file_extensions' => $this
        ->t('List of the enabled file extensions, separated by a whitespace'),
    ] + parent::fields();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigSourceBase::MULTIPLE_SEPARATOR constant
FilePlainConfigSourceBase::$fileDealerManager protected property The file dealer plugin manager.
FilePlainConfigSourceBase::$sourceHasFileEntities protected property Whether the source has (fieldable) file entities or not.
FilePlainConfigSourceBase::create public static function
FilePlainConfigSourceBase::getIds public function 1
FilePlainConfigSourceBase::prepareQuery protected function
FilePlainConfigSourceBase::__construct public function Constructs a plugin instance.
FilePlainSourceFieldInstance::fields public function Overrides FilePlainConfigSourceBase::fields
FilePlainSourceFieldInstance::initializeIterator protected function Overrides FilePlainConfigSourceBase::initializeIterator
FilePlainSourceFieldInstance::prepareRow public function
FilePlainSourceFieldInstance::query public function Overrides FilePlainConfigSourceBase::query
MediaMigrationDatabaseTrait::getExtensionExpression protected function Returns the file extension expression for the current DB.
MediaMigrationDatabaseTrait::getFileEntityBaseQuery protected function Returns a base query for file entity types.
MediaMigrationDatabaseTrait::getFilePlainBaseQuery protected function Returns a base query for plain files.
MediaMigrationDatabaseTrait::getMainMimeTypeExpression protected function Returns the main MIME type's expression for the current DB.
MediaMigrationDatabaseTrait::getSchemeExpression protected function Returns the expression for the DB for getting the URI scheme.
MediaMigrationDatabaseTrait::getUserPictureOnlyFidsQuery protected function Returns the subquery for the user picture-only file IDs.
MediaMigrationDatabaseTrait::getWebformOrUserPictureOnlyFidsQuery protected function Subquery for FIDs used only in webform submissions and/or by user entities.