You are here

protected function MediaDealerBase::getMediaSourceFieldInstance in Media Migration 8

Returns a media source field instance.

The returned field instance ("field_config") entity that matches the media source plugin ID. When the destination media type does not exist, this is a new, unsaved media source field instance.

The returned entity can be used for pre-populating the media type's source field's instance settings, e.g. for keeping every, previously allowed file extensions.

Return value

\Drupal\field\FieldConfigInterface|null A matching field instance config for the destination media type, or NULL if it cannot be instantiated.

1 call to MediaDealerBase::getMediaSourceFieldInstance()
MediaDealerBase::prepareMediaSourceFieldInstanceRow in src/MediaDealerBase.php

File

src/MediaDealerBase.php, line 252

Class

MediaDealerBase
Base implementation of media dealer plugins.

Namespace

Drupal\media_migration

Code

protected function getMediaSourceFieldInstance() {
  $preexisting_field_instance = $this->entityTypeManager
    ->getStorage('field_config')
    ->load(implode('.', [
    'media',
    $this
      ->getDestinationMediaTypeId(),
    $this
      ->getDestinationMediaSourceFieldName(),
  ]));
  if ($preexisting_field_instance) {
    assert($preexisting_field_instance instanceof FieldConfigInterface);
    return $preexisting_field_instance;
  }
  if (!($storage = $this
    ->getMediaSourceFieldStorage())) {
    return NULL;
  }
  $field_config = FieldConfig::create([
    'field_storage' => $storage,
    'bundle' => $this
      ->getDestinationMediaTypeId(),
    'label' => $this
      ->getDestinationMediaTypeLabel(),
    'required' => TRUE,
  ]);
  if (!$field_config instanceof FieldConfigInterface) {
    return NULL;
  }
  $default_settings = $this->fieldTypeManager
    ->getDefaultFieldSettings($field_config
    ->getType());
  $extensions = explode(' ', $default_settings['file_extensions'] ?? '');
  switch ($this
    ->getDestinationMediaSourcePluginId()) {
    case 'audio_file':

      // Using the same defaults what the AudioFile source plugin defines.
      // @see \Drupal\media\Plugin\media\Source\AudioFile::createSourceField()
      $extensions = [
        'mp3',
        'wav',
        'aac',
      ];
      break;
    case 'image':

      // Using the same defaults what the Image source plugin defines.
      // @see \Drupal\media\Plugin\media\Source\Image::createSourceField()
      break;
    case 'video_file':

      // Using the same defaults what the VideoFile source plugin defines.
      // @see \Drupal\media\Plugin\media\Source\VideoFile::createSourceField()
      $extensions = [
        'mp4',
      ];
      break;
    case 'file':

      // Using the same defaults what the File source plugin defines.
      // @see \Drupal\media\Plugin\media\Source\File::createSourceField()
      $extensions = [
        'txt',
        'doc',
        'docx',
        'pdf',
      ];
      break;
  }
  $default_settings['file_extensions'] = implode(' ', $extensions);
  $field_config
    ->set('settings', $default_settings);
  return $field_config;
}