You are here

class D7FileConfigDeriver in Media Migration 8

Deriver class for plain file to media config migrations.

"Plain file" refers to managed files whose "type" column's value (added to the "file_managed" table by the D7 contrib file_entity module) is empty ("") or "undefined"), OR for every managed file when there is no "type" column is defined in the "file_managed" table.

This deriver class should be used for deriving the required configuration migrations of the plain file to media migration.

Hierarchy

Expanded class hierarchy of D7FileConfigDeriver

5 string references to 'D7FileConfigDeriver'
d7_file_plain_formatter.yml in migrations/d7_file_plain_formatter.yml
migrations/d7_file_plain_formatter.yml
d7_file_plain_source_field.yml in migrations/d7_file_plain_source_field.yml
migrations/d7_file_plain_source_field.yml
d7_file_plain_source_field_config.yml in migrations/d7_file_plain_source_field_config.yml
migrations/d7_file_plain_source_field_config.yml
d7_file_plain_type.yml in migrations/d7_file_plain_type.yml
migrations/d7_file_plain_type.yml
d7_file_plain_widget.yml in migrations/d7_file_plain_widget.yml
migrations/d7_file_plain_widget.yml

File

src/Plugin/migrate/D7FileConfigDeriver.php, line 30

Namespace

Drupal\media_migration\Plugin\migrate
View source
class D7FileConfigDeriver extends DeriverBase implements ContainerDeriverInterface {
  use MigrationDeriverTrait;
  use StringTranslationTrait;

  /**
   * The file dealer plugin manager.
   *
   * @var \Drupal\media_migration\FileDealerManagerInterface
   */
  protected $fileDealerManager;

  /**
   * Constructs D7FileConfigDeriver.
   *
   * @param \Drupal\media_migration\FileDealerManagerInterface $file_dealer_manager
   *   The field type dealer plugin manager.
   */
  public function __construct(FileDealerManagerInterface $file_dealer_manager) {
    $this->fileDealerManager = $file_dealer_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, $base_plugin_id) {
    return new static($container
      ->get('plugin.manager.file_dealer'));
  }

  /**
   * {@inheritdoc}
   */
  public function getDerivativeDefinitions($base_plugin_definition) {
    $plain_file_types = static::getSourcePlugin('d7_file_plain_type');
    try {
      $plain_file_types
        ->checkRequirements();
    } catch (RequirementsException $e) {

      // The requirements of the "d7_file_plain_type" source plugin can fail if:
      // - The source database is not configured.
      // - The File module is not enabled on the source Drupal instance.
      // - The source database is not a Drupal 7 database.
      return $this->derivatives;
    }
    assert($plain_file_types instanceof DrupalSqlBase);
    try {
      foreach ($plain_file_types as $plain_file_type_row) {
        assert($plain_file_type_row instanceof Row);
        [
          'mimes' => $mimes,
          'schemes' => $schemes,
        ] = $plain_file_type_row
          ->getSource();
        $mime = explode(ConfigSourceBase::MULTIPLE_SEPARATOR, $mimes)[0];
        $scheme = explode(ConfigSourceBase::MULTIPLE_SEPARATOR, $schemes)[0];

        // The "fallback" plugin has to be instantiated for any kind of
        // combination.
        if (!($dealer_plugin = $this->fileDealerManager
          ->createInstanceFromSchemeAndMime($scheme, $mime))) {
          throw new \LogicException(sprintf('No FileDealer plugin applies for files with scheme "%s" and with mime type "%s/*"', $scheme, $mime));
        }
        $destination_media_type_id = $dealer_plugin
          ->getDestinationMediaTypeId();
        $source_plugin_id = $base_plugin_definition['source']['plugin'];
        $derivative_definition = $base_plugin_definition;

        // Create the migration derivative.
        $derivative_definition['migration_tags'][] = MediaMigration::MIGRATION_TAG_MAIN;
        $derivative_definition['migration_tags'][] = MediaMigration::MIGRATION_TAG_CONFIG;
        $derivative_definition['source']['mimes'] = $mimes;
        $derivative_definition['source']['schemes'] = $schemes;
        $derivative_definition['source']['destination_media_type_id'] = $destination_media_type_id;
        $derivative_definition['label'] = $this
          ->t('@label (@type)', [
          '@label' => $base_plugin_definition['label'],
          '@type' => $dealer_plugin
            ->getDestinationMediaTypeLabel(),
        ]);

        // Post-process migration dependencies: instead of depending on
        // migrations based on their base plugin ID, it is better to use the
        // corresponding derivatives where possible.
        $derived_migration_ids = [
          'd7_file_plain_type',
          'd7_file_plain_source_field',
          'd7_file_plain_source_field_config',
          'd7_file_plain_widget',
          'd7_file_plain_formatter',
        ];
        foreach ($derived_migration_ids as $derived_migration_base_id) {
          $required_dependencies = !empty($derivative_definition['migration_dependencies']['required']) ? $derivative_definition['migration_dependencies']['required'] : [];
          $dependency_key = array_search($derived_migration_base_id, $required_dependencies, TRUE);
          if ($dependency_key !== FALSE) {
            $derivative_definition['migration_dependencies']['required'][$dependency_key] .= PluginBase::DERIVATIVE_SEPARATOR . $destination_media_type_id;
          }
        }
        switch ($source_plugin_id) {
          case 'd7_file_plain_type':
            $dealer_plugin
              ->alterMediaTypeMigrationDefinition($derivative_definition, $plain_file_types
              ->getDatabase());
            break;
          case 'd7_file_plain_source_field_storage':
            $dealer_plugin
              ->alterMediaSourceFieldStorageMigrationDefinition($derivative_definition, $plain_file_types
              ->getDatabase());
            break;
          case 'd7_file_plain_source_field_instance':
            $dealer_plugin
              ->alterMediaSourceFieldInstanceMigrationDefinition($derivative_definition, $plain_file_types
              ->getDatabase());
            break;
          case 'd7_file_plain_field_widget':
            $dealer_plugin
              ->alterMediaSourceFieldWidgetMigrationDefinition($derivative_definition, $plain_file_types
              ->getDatabase());
            break;
          case 'd7_file_plain_field_formatter':
            $dealer_plugin
              ->alterMediaFieldFormatterMigrationDefinition($derivative_definition, $plain_file_types
              ->getDatabase());
            break;
        }
        $this->derivatives[$destination_media_type_id] = $derivative_definition;
      }
    } catch (DatabaseExceptionWrapper $e) {

      // Once we begin iterating the source plugin it is possible that the
      // source tables will not exist. This can happen when the
      // MigrationPluginManager gathers up the migration definitions but we do
      // not actually have a Drupal 7 source database.
    }
    return $this->derivatives;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
D7FileConfigDeriver::$fileDealerManager protected property The file dealer plugin manager.
D7FileConfigDeriver::create public static function Creates a new class instance. Overrides ContainerDeriverInterface::create
D7FileConfigDeriver::getDerivativeDefinitions public function Gets the definition of all derivatives of a base plugin. Overrides DeriverBase::getDerivativeDefinitions 1
D7FileConfigDeriver::__construct public function Constructs D7FileConfigDeriver.
DeriverBase::$derivatives protected property List of derivative definitions. 1
DeriverBase::getDerivativeDefinition public function Gets the definition of a derivative plugin. Overrides DeriverInterface::getDerivativeDefinition
MigrationDeriverTrait::getSourcePlugin public static function Returns a fully initialized instance of a source plugin.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.