You are here

protected function MediaDealerBase::getMediaSourceFieldStorage in Media Migration 8

Returns a media source field storage.

The returned field storage ("field_storage_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 storage entity.

The returned entity can be used for pre-populating the media type's source field's storage settings.

Return value

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

2 calls to MediaDealerBase::getMediaSourceFieldStorage()
MediaDealerBase::getMediaSourceFieldInstance in src/MediaDealerBase.php
Returns a media source field instance.
MediaDealerBase::prepareMediaSourceFieldStorageRow in src/MediaDealerBase.php

File

src/MediaDealerBase.php, line 324

Class

MediaDealerBase
Base implementation of media dealer plugins.

Namespace

Drupal\media_migration

Code

protected function getMediaSourceFieldStorage() {
  $source_field_name = $this
    ->getDestinationMediaSourceFieldName();
  $preexisting_field_storage = $this->entityTypeManager
    ->getStorage('field_storage_config')
    ->load(implode('.', [
    'media',
    $source_field_name,
  ]));
  if ($preexisting_field_storage) {
    assert($preexisting_field_storage instanceof FieldStorageConfigInterface);
    return $preexisting_field_storage;
  }
  $source_plugin_id = $this
    ->getDestinationMediaSourcePluginId();
  try {
    $media_source_plugin = $this->mediaSourceManager
      ->createInstance($source_plugin_id);
  } catch (PluginException $e) {

    // The specified plugin is invalid or missing.
    return NULL;
  }
  assert($media_source_plugin instanceof MediaSourceInterface);
  $source_plugin_definition = $media_source_plugin
    ->getPluginDefinition();
  $field_type = reset($source_plugin_definition['allowed_field_types']);
  $field_storage = FieldStorageConfig::create([
    'entity_type' => 'media',
    'field_name' => $source_field_name,
    'type' => $field_type,
  ]);
  assert($field_storage instanceof FieldStorageConfigInterface);
  $field_storage
    ->set('settings', $this->fieldTypeManager
    ->getDefaultStorageSettings($field_storage
    ->getType()));
  return $field_storage;
}