X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Fmedia%2Fsrc%2FMediaSourceBase.php;fp=web%2Fcore%2Fmodules%2Fmedia%2Fsrc%2FMediaSourceBase.php;h=1edc8584508e693a6ee274ac328afaa7edaccfd7;hp=dea01402609c67a2e509897ffd1b745fbcef1518;hb=af6d1fb995500ae68849458ee10d66abbdcfb252;hpb=680c79a86e3ed402f263faeac92e89fb6d9edcc0 diff --git a/web/core/modules/media/src/MediaSourceBase.php b/web/core/modules/media/src/MediaSourceBase.php index dea014026..1edc85845 100644 --- a/web/core/modules/media/src/MediaSourceBase.php +++ b/web/core/modules/media/src/MediaSourceBase.php @@ -3,6 +3,8 @@ namespace Drupal\media; use Drupal\Component\Utility\NestedArray; +use Drupal\Core\Entity\Display\EntityFormDisplayInterface; +use Drupal\Core\Entity\Display\EntityViewDisplayInterface; use Drupal\Core\Entity\EntityFieldManagerInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Field\FieldTypePluginManagerInterface; @@ -317,4 +319,37 @@ abstract class MediaSourceBase extends PluginBase implements MediaSourceInterfac 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} + */ + public function prepareViewDisplay(MediaTypeInterface $type, EntityViewDisplayInterface $display) { + $display->setComponent($this->getSourceFieldDefinition($type)->getName()); + } + + /** + * {@inheritdoc} + */ + public function prepareFormDisplay(MediaTypeInterface $type, EntityFormDisplayInterface $display) { + // Make sure the source field is placed just after the "name" basefield. + $name_component = $display->getComponent('name'); + $source_field_weight = ($name_component && isset($name_component['weight'])) ? $name_component['weight'] + 5 : -50; + $display->setComponent($this->getSourceFieldDefinition($type)->getName(), [ + 'weight' => $source_field_weight, + ]); + } + }