You are here

class FileEntitySourceFieldInstance in Media Migration 8

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

Plugin annotation


@MigrateSource(
  id = "d7_file_entity_source_field_instance",
  source_module = "file_entity"
)

Hierarchy

Expanded class hierarchy of FileEntitySourceFieldInstance

File

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

Namespace

Drupal\media_migration\Plugin\migrate\source\d7
View source
class FileEntitySourceFieldInstance extends FileEntityConfigSourceBase {

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

  /**
   * {@inheritdoc}
   */
  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);
  }

  /**
   * {@inheritdoc}
   */
  public function fields() {
    return [
      'file_extensions' => $this
        ->t('The allowed extensions of a media source field which base type is file, separated by "::"'),
    ] + parent::fields();
  }

  /**
   * {@inheritdoc}
   */
  public function getIds() {
    $ids['types']['type'] = 'string';
    $ids['schemes']['type'] = 'string';
    return $ids;
  }

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

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigSourceBase::MULTIPLE_SEPARATOR constant
FileEntityConfigSourceBase::$fileEntityDealerManager protected property The file entity dealer plugin manager.
FileEntityConfigSourceBase::create public static function
FileEntityConfigSourceBase::prepareQuery protected function
FileEntityConfigSourceBase::__construct public function Constructs a plugin instance.
FileEntitySourceFieldInstance::fields public function Overrides FileEntityConfigSourceBase::fields
FileEntitySourceFieldInstance::getIds public function Overrides FileEntityConfigSourceBase::getIds
FileEntitySourceFieldInstance::initializeIterator protected function Overrides FileEntityConfigSourceBase::initializeIterator
FileEntitySourceFieldInstance::prepareRow public function
FileEntitySourceFieldInstance::query public function Overrides FileEntityConfigSourceBase::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.