Commit bfe514a0 authored by larowlan's avatar larowlan

Issue #2934966 by benjifisher, starshaped: Make it easier to get source field...

Issue #2934966 by benjifisher, starshaped: Make it easier to get source field values from media items
parent 0c1de8e4
......@@ -319,6 +319,20 @@ protected function getSourceFieldName() {
return $id;
}
/**
* {@inheritdoc}
*/
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.');
}
/** @var \Drupal\Core\Field\FieldItemInterface $field_item */
$field_item = $media->get($source_field)->first();
return $field_item->{$field_item->mainPropertyName()};
}
/**
* {@inheritdoc}
*/
......
......@@ -177,4 +177,18 @@ public function prepareViewDisplay(MediaTypeInterface $type, EntityViewDisplayIn
*/
public function prepareFormDisplay(MediaTypeInterface $type, EntityFormDisplayInterface $display);
/**
* Get the primary value stored in the source field.
*
* @param MediaInterface $media
* A media item.
*
* @return mixed
* The source value.
*
* @throws \RuntimeException
* If the source field for the media source is not defined.
*/
public function getSourceFieldValue(MediaInterface $media);
}
......@@ -140,6 +140,20 @@ public function testMetadataMapping() {
$this->assertEquals('Snowball', $media->get($field_name)->value, 'Metadata attribute was not mapped to the field.');
}
/**
* Tests the getSourceFieldValue() method.
*/
public function testGetSourceFieldValue() {
/** @var \Drupal\media\MediaInterface $media */
$media = Media::create([
'bundle' => $this->testMediaType->id(),
'field_media_test' => 'some_value',
]);
$media->save();
$media_source = $media->getSource();
$this->assertSame('some_value', $media_source->getSourceFieldValue($media));
}
/**
* Tests the thumbnail functionality.
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment