You are here

protected function FileEntity::addRequiredMediaMigrationDependencies in Media Migration 8

Discovers the file type used in the field and adds their migration deps.

Parameters

\Drupal\migrate\Plugin\MigrationInterface $migration: The actual fieldable entity migration where the field belongs to.

array $field_data: The data (settings) of the field instance to check.

1 call to FileEntity::addRequiredMediaMigrationDependencies()
FileEntity::defineValueProcessPipeline in src/Plugin/migrate/field/FileEntity.php
Apply any custom processing to the field bundle migrations.

File

src/Plugin/migrate/field/FileEntity.php, line 140

Class

FileEntity
Field Plugin for file_entity to media migrations.

Namespace

Drupal\media_migration\Plugin\migrate\field

Code

protected function addRequiredMediaMigrationDependencies(MigrationInterface $migration, array $field_data) : void {
  $source_plugin = $migration
    ->getSourcePlugin();
  if (!$source_plugin instanceof DrupalSqlBase) {

    // If the source database is not a legacy Drupal database, we cannot do
    // anything.
    return;
  }
  $source_db = $source_plugin
    ->getDatabase();
  $used_file_types = NULL;

  // Let's check whether we have a type column in the file_managed table.
  $type_column_exists = $source_db
    ->schema()
    ->fieldExists('file_managed', 'type');
  if ($type_column_exists) {
    $field_name = $field_data['field_name'];
    $file_types_query = $source_db
      ->select("field_revision_{$field_name}", 'frd')
      ->distinct()
      ->fields('fm', [
      'type',
    ])
      ->condition('frd.entity_type', $field_data['entity_type'])
      ->condition('frd.bundle', $field_data['bundle'])
      ->condition('fm.status', TRUE)
      ->condition('fm.uri', 'temporary://%', 'NOT LIKE');
    $file_types_query
      ->innerJoin('file_managed', 'fm', "fm.fid = frd.{$field_name}_fid");
    try {
      $used_file_types = $file_types_query
        ->execute()
        ->fetchAllKeyed(0, 0);
    } catch (DatabaseExceptionWrapper $exception) {
    }
  }
  $extra_dependencies = [];
  if (is_null($used_file_types) || in_array('undefined', $used_file_types)) {
    $extra_dependencies[] = 'd7_file_plain';
  }
  if ($type_column_exists && !empty($used_file_types)) {
    foreach ($used_file_types as $type) {
      if ($type === 'undefined') {
        continue;
      }
      $extra_dependencies[] = "d7_file_entity:{$type}";
    }
  }

  // No additional dependencies are required for migrating the given field's
  // values.
  if (empty($extra_dependencies)) {
    return;
  }
  $migration_dependencies = $migration
    ->getMigrationDependencies();
  $migration_dependencies['required'] = array_unique(array_merge(array_values($migration_dependencies['required']), $extra_dependencies));
  $migration
    ->set('migration_dependencies', $migration_dependencies);
}