You are here

public function Slideshow::getMetadata in Media entity slideshow 8.2

Gets the value for a metadata attribute for a given media item.

Parameters

\Drupal\media\MediaInterface $media: A media item.

string $attribute_name: Name of the attribute to fetch.

Return value

mixed|null Metadata attribute value or NULL if unavailable.

Overrides MediaSourceBase::getMetadata

File

src/Plugin/media/Source/Slideshow.php, line 37

Class

Slideshow
Provides media type plugin for Slideshows.

Namespace

Drupal\media_entity_slideshow\Plugin\media\Source

Code

public function getMetadata(MediaInterface $media, $name) {
  $source_field = $this->configuration['source_field'];
  switch ($name) {
    case 'default_name':

      // The default name will be the timestamp + number of slides.
      $length = $this
        ->getMetadata($media, 'length');
      if (!empty($length)) {
        return $this
          ->formatPlural($length, '1 slide, created on @date', '@count slides, created on @date', [
          '@date' => \Drupal::service('date.formatter')
            ->format($media
            ->getCreatedTime(), 'custom', DateTimeItemInterface::DATETIME_STORAGE_FORMAT),
        ]);
      }
      return parent::getMetadata($media, 'default_name');
    case 'length':
      return $media->{$source_field}
        ->count();
    case 'thumbnail_uri':
      $source_field = $this->configuration['source_field'];
      if (empty($media->{$source_field}->target_id)) {
        return parent::getMetadata($media, 'thumbnail_uri');
      }

      /** @var \Drupal\media\MediaInterface $slideshow_item */
      $slideshow_item = $this->entityTypeManager
        ->getStorage('media')
        ->load($media->{$source_field}->target_id);
      if (!$slideshow_item) {
        return parent::getMetadata($media, 'thumbnail_uri');
      }

      /** @var \Drupal\media\MediaTypeInterface $bundle */
      $bundle = $this->entityTypeManager
        ->getStorage('media_type')
        ->load($slideshow_item
        ->bundle());
      if (!$bundle) {
        return parent::getMetadata($media, 'thumbnail_uri');
      }
      $thumbnail = $bundle
        ->getSource()
        ->getMetadata($slideshow_item, 'thumbnail_uri');
      if (!$thumbnail) {
        return parent::getMetadata($media, 'thumbnail_uri');
      }
      return $thumbnail;
    default:
      return parent::getMetadata($media, $name);
  }
}