function 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.
File
-
core/
modules/ media/ src/ MediaSourceBase.php, line 304
Class
- MediaSourceBase
- Base implementation of media source plugin.
Namespace
Drupal\mediaCode
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 {
// Limit the base field name to the maximum allowed length.
$id = strlen($base_id) > EntityTypeInterface::ID_MAX_LENGTH ? substr($base_id, 0, EntityTypeInterface::ID_MAX_LENGTH) : $base_id;
// If we've tried before, increment and append the suffix.
if ($tries) {
$id .= '_' . $tries;
// Ensure the suffixed field name does not exceed the maximum allowed length.
if (strlen($id) > EntityTypeInterface::ID_MAX_LENGTH) {
$id = substr($base_id, 0, EntityTypeInterface::ID_MAX_LENGTH - strlen('_' . $tries)) . '_' . $tries;
}
}
$field = $storage->load('media.' . $id);
$tries++;
} while ($field);
return $id;
}