function MediaSourceBase::getSourceFieldName

Same name and namespace in other branches
  1. 9 core/modules/media/src/MediaSourceBase.php \Drupal\media\MediaSourceBase::getSourceFieldName()
  2. 8.9.x core/modules/media/src/MediaSourceBase.php \Drupal\media\MediaSourceBase::getSourceFieldName()
  3. 10 core/modules/media/src/MediaSourceBase.php \Drupal\media\MediaSourceBase::getSourceFieldName()

Determine the name of the source field.

Return value

string The source field name. If one is already stored in configuration, it is returned. Otherwise, a new, unused one is generated.

1 call to MediaSourceBase::getSourceFieldName()
MediaSourceBase::createSourceFieldStorage in core/modules/media/src/MediaSourceBase.php
Creates the source field storage definition.
2 methods override MediaSourceBase::getSourceFieldName()
TestDifferentDisplays::getSourceFieldName in core/modules/media/tests/modules/media_test_source/src/Plugin/media/Source/TestDifferentDisplays.php
Determine the name of the source field.
TestWithHiddenSourceField::getSourceFieldName in core/modules/media/tests/modules/media_test_source/src/Plugin/media/Source/TestWithHiddenSourceField.php
Determine the name of the source field.

File

core/modules/media/src/MediaSourceBase.php, line 303

Class

MediaSourceBase
Base implementation of media source plugin.

Namespace

Drupal\media

Code

protected function getSourceFieldName() {
    // If the Field UI module is installed, and has a specific prefix
    // configured, use that. Otherwise, just default to using 'field_' as
    // a prefix, which is the default that Field UI ships with.
    $prefix = $this->configFactory
        ->get('field_ui.settings')
        ->get('field_prefix') ?? 'field_';
    // Some media sources are using a deriver, so their plugin IDs may contain
    // a separator (usually ':') which is not allowed in field names.
    $base_id = $prefix . 'media_' . str_replace(static::DERIVATIVE_SEPARATOR, '_', $this->getPluginId());
    $tries = 0;
    $storage = $this->entityTypeManager
        ->getStorage('field_storage_config');
    // Iterate at least once, until no field with the generated ID is found.
    do {
        $id = $base_id;
        // If we've tried before, increment and append the suffix.
        if ($tries) {
            $id .= '_' . $tries;
        }
        $field = $storage->load('media.' . $id);
        $tries++;
    } while ($field);
    return $id;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.