You are here

public function MediaSourceBase::getSourceFieldValue in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/media/src/MediaSourceBase.php \Drupal\media\MediaSourceBase::getSourceFieldValue()

Get the primary value stored in the source field.

Parameters

MediaInterface $media: A media item.

Return value

mixed The source value, or NULL if the media item's source field is empty.

Throws

\RuntimeException If the source field for the media source is not defined.

Overrides MediaSourceInterface::getSourceFieldValue

1 call to MediaSourceBase::getSourceFieldValue()
OEmbed::getMetadata in core/modules/media/src/Plugin/media/Source/OEmbed.php
Gets the value for a metadata attribute for a given media item.

File

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

Class

MediaSourceBase
Base implementation of media source plugin.

Namespace

Drupal\media

Code

public function getSourceFieldValue(MediaInterface $media) {
  $source_field = $this->configuration['source_field'];
  if (empty($source_field)) {
    throw new \RuntimeException('Source field for media source is not defined.');
  }
  $items = $media
    ->get($source_field);
  if ($items
    ->isEmpty()) {
    return NULL;
  }
  $field_item = $items
    ->first();
  return $field_item->{$field_item
    ->mainPropertyName()};
}