You are here

class Fallback in Media Migration 8

Same name in this branch
  1. 8 src/Plugin/media_migration/file_entity/Fallback.php \Drupal\media_migration\Plugin\media_migration\file_entity\Fallback
  2. 8 src/Plugin/media_migration/file/Fallback.php \Drupal\media_migration\Plugin\media_migration\file\Fallback

General plugin for any kind of file.

Plugin annotation


@FileDealer(
  id = "fallback"
)

Hierarchy

Expanded class hierarchy of Fallback

File

src/Plugin/media_migration/file/Fallback.php, line 16

Namespace

Drupal\media_migration\Plugin\media_migration\file
View source
class Fallback extends FileDealerBase {

  /**
   * {@inheritdoc}
   */
  public function getDestinationMediaSourcePluginId() {
    switch ($this->configuration['mime']) {
      case 'audio':
        return 'audio_file';
      case 'image':
        return 'image';
      case 'video':
        return 'video_file';
      default:
        return 'file';
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getDestinationMediaTypeIdBase() {
    switch ($this->configuration['mime']) {
      case 'audio':
      case 'image':
      case 'video':
        return $this->configuration['mime'];
      default:
        return 'document';
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getDestinationMediaSourceFieldName() {

    // Document's field name should be field_media_document[_private].
    if ($this
      ->getDestinationMediaTypeIdBase() === 'document') {
      return implode('_', array_filter([
        'field',
        'media',
        'document',
        $this->configuration['scheme'] === 'public' ? NULL : $this->configuration['scheme'],
      ]));
    }
    return parent::getDestinationMediaSourceFieldName();
  }

  /**
   * {@inheritdoc}
   */
  public function getDestinationMediaTypeSourceFieldLabel() {
    switch ($this
      ->getDestinationMediaSourcePluginId()) {
      case 'audio_file':
        return 'Audio file';
      case 'video_file':
        return 'Video file';
    }
    return parent::getDestinationMediaTypeSourceFieldLabel();
  }

  /**
   * {@inheritdoc}
   */
  public function alterMediaEntityMigrationDefinition(array &$migration_definition, Connection $connection) : void {
    parent::alterMediaEntityMigrationDefinition($migration_definition, $connection);
    $source_field_name = $this
      ->getDestinationMediaSourceFieldName();
    $migration_definition['process'][$source_field_name . '/display'] = 'display';
    $migration_definition['process'][$source_field_name . '/description'] = 'description';
  }

  /**
   * {@inheritdoc}
   */
  public function prepareMediaSourceFieldInstanceRow(Row $row, Connection $connection) : void {
    parent::prepareMediaSourceFieldInstanceRow($row, $connection);
    $show_description_field = FALSE;
    foreach ($this
      ->getFileFieldData($connection, FALSE) as $data) {
      if (!empty($data['field_instance_data']['settings']['description_field'])) {
        $show_description_field = TRUE;
        break 1;
      }
    }
    $row
      ->setSourceProperty('settings/description_field', $show_description_field);
  }

  /**
   * {@inheritdoc}
   */
  public function prepareMediaSourceFieldFormatterRow(Row $row, Connection $connection) : void {
    parent::prepareMediaSourceFieldFormatterRow($row, $connection);
    $original_options = $row
      ->getSourceProperty('options') ?? [];
    switch ($this
      ->getDestinationMediaSourcePluginId()) {
      case 'audio_file':
        $options = [
          'type' => 'file_audio',
          'settings' => [
            'controls' => TRUE,
            'autoplay' => FALSE,
            'loop' => FALSE,
            'multiple_file_display_type' => 'tags',
          ],
        ] + $original_options;
        $row
          ->setSourceProperty('options', $options);
        break;
      case 'video_file':
        $options = [
          'type' => 'file_video',
          'settings' => [
            'muted' => FALSE,
            'width' => 640,
            'height' => 480,
          ],
        ] + $original_options;
        $row
          ->setSourceProperty('options', $options);
        break;
      case 'image':
        $original_options['settings'] = [
          'image_style' => 'large',
        ];
        $row
          ->setSourceProperty('options', $original_options);
        break;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function prepareMediaEntityRow(Row $row, Connection $connection) : void {
    parent::prepareMediaEntityRow($row, $connection);
    foreach ($this
      ->getFileData($connection, $row
      ->getSourceProperty('fid')) as $data_key => $data_value) {
      $row
        ->setSourceProperty($data_key, $data_value);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
Fallback::alterMediaEntityMigrationDefinition public function Alters the definition of the media entity migration. Overrides FileDealerBase::alterMediaEntityMigrationDefinition
Fallback::getDestinationMediaSourceFieldName public function Overrides MediaDealerBase::getDestinationMediaSourceFieldName
Fallback::getDestinationMediaSourcePluginId public function Overrides MediaDealerBase::getDestinationMediaSourcePluginId
Fallback::getDestinationMediaTypeIdBase public function Overrides MediaDealerBase::getDestinationMediaTypeIdBase
Fallback::getDestinationMediaTypeSourceFieldLabel public function Overrides MediaDealerBase::getDestinationMediaTypeSourceFieldLabel
Fallback::prepareMediaEntityRow public function Overrides MediaDealerBase::prepareMediaEntityRow
Fallback::prepareMediaSourceFieldFormatterRow public function Overrides MediaDealerBase::prepareMediaSourceFieldFormatterRow
Fallback::prepareMediaSourceFieldInstanceRow public function Overrides MediaDealerBase::prepareMediaSourceFieldInstanceRow
FileDealerBase::getFileData protected function Returns display and description properties of the specified file.
FileDealerBase::getFileFieldData protected function Get the name of the file fields from the source database.
MediaDealerBase::$entityTypeManager protected property The entity type manager.
MediaDealerBase::$fieldTypeManager protected property The field type plugin manager service.
MediaDealerBase::$mediaSourceManager protected property The media source plugin manager.
MediaDealerBase::alterMediaFieldFormatterMigrationDefinition public function
MediaDealerBase::alterMediaSourceFieldInstanceMigrationDefinition public function
MediaDealerBase::alterMediaSourceFieldStorageMigrationDefinition public function
MediaDealerBase::alterMediaSourceFieldWidgetMigrationDefinition public function
MediaDealerBase::alterMediaTypeMigrationDefinition public function
MediaDealerBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
MediaDealerBase::getDestinationMediaTypeId public function 1
MediaDealerBase::getDestinationMediaTypeLabel public function 3
MediaDealerBase::getImageData protected function Returns alt, title, with and height properties of the specified file.
MediaDealerBase::getImageFieldData protected function Get the names of the image type fields from the source database.
MediaDealerBase::getMediaSourceFieldInstance protected function Returns a media source field instance.
MediaDealerBase::getMediaSourceFieldStorage protected function Returns a media source field storage.
MediaDealerBase::prepareMediaSourceFieldStorageRow public function 1
MediaDealerBase::prepareMediaSourceFieldWidgetRow public function
MediaDealerBase::prepareMediaTypeRow public function
MediaDealerBase::__construct public function Constructs a new plugin instance. Overrides PluginBase::__construct
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
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.