VarbaseMediaGallery.php in Varbase Media 9.0.x
File
src/Plugin/media/Source/VarbaseMediaGallery.php
View source
<?php
namespace Drupal\varbase_media\Plugin\media\Source;
use Drupal\media\MediaInterface;
use Drupal\media\MediaSourceBase;
use Drupal\media\MediaSourceEntityConstraintsInterface;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
class VarbaseMediaGallery extends MediaSourceBase implements MediaSourceEntityConstraintsInterface {
public function getMetadataAttributes() {
$attributes = [
'length' => $this
->t('Gallery length'),
];
return $attributes;
}
public function getMetadata(MediaInterface $media, $name) {
$source_field = $this->configuration['source_field'];
switch ($name) {
case 'default_name':
$length = $this
->getMetadata($media, 'length');
if (!empty($length)) {
return $this
->formatPlural($length, '1 media item, created on @date', '@count media items, 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':
return parent::getMetadata($media, 'thumbnail_uri');
default:
return parent::getMetadata($media, $name);
}
}
public function getEntityConstraints() {
$source_field = $this->configuration['source_field'];
return [
'MediaItemsCount' => [
'sourceFieldName' => $source_field,
],
];
}
}