3 namespace Drupal\media_entity_slideshow\Plugin\MediaEntity\Type;
5 use Drupal\media_entity\MediaInterface;
6 use Drupal\media_entity\MediaTypeBase;
7 use Drupal\Core\Form\FormStateInterface;
10 * Provides media type plugin for Slideshows.
14 * label = @Translation("Slideshow"),
15 * description = @Translation("Provides business logic and metadata for slideshows.")
18 class Slideshow extends MediaTypeBase {
23 public function providedFields() {
25 'length' => $this->t('Slideshow length'),
34 public function getField(MediaInterface $media, $name) {
35 $source_field = $this->configuration['source_field'];
39 return $media->{$source_field}->count();
48 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
49 /** @var MediaBundleInterface $bundle */
50 $bundle = $form_state->getFormObject()->getEntity();
52 $allowed_field_types = ['entity_reference'];
53 /** @var \Drupal\Core\Field\FieldDefinitionInterface $field */
54 foreach ($this->entityFieldManager->getFieldDefinitions('media', $bundle->id()) as $field_name => $field) {
55 if (in_array($field->getType(), $allowed_field_types)) {
56 $storage = $field->getFieldStorageDefinition();
57 if (!$storage->isBaseField() && $storage->getSetting('target_type') == 'media') {
58 $options[$field_name] = $field->getLabel();
63 $form['source_field'] = [
65 '#title' => $this->t('Field with source information'),
66 '#description' => $this->t('Field on media entity that stores slideshow items. You can create a bundle without selecting a value for this dropdown initially. This dropdown can be populated after adding fields to the bundle.'),
67 '#default_value' => empty($this->configuration['source_field']) ? NULL : $this->configuration['source_field'],
68 '#options' => $options,
77 public function attachConstraints(MediaInterface $media) {
78 parent::attachConstraints($media);
80 $source_field_name = $this->configuration['source_field'];
81 // Validate slideshow items count.
82 $media->getTypedData()->getDataDefinition()->addConstraint('ItemsCount', array('sourceFieldName' => $source_field_name));
88 public function getDefaultThumbnail() {
89 return $this->config->get('icon_base') . '/slideshow.png';
95 public function thumbnail(MediaInterface $media) {
96 $source_field = $this->configuration['source_field'];
98 /** @var \Drupal\media_entity\MediaInterface $slideshow_item */
99 $slideshow_item = $this->entityTypeManager->getStorage('media')->load($media->{$source_field}->target_id);
100 if (!$slideshow_item) {
101 return $this->getDefaultThumbnail();
104 /** @var \Drupal\media_entity\MediaBundleInterface $bundle */
105 $bundle = $this->entityTypeManager->getStorage('media_bundle')->load($slideshow_item->bundle());
107 return $this->getDefaultThumbnail();
110 $thumbnail = $bundle->getType()->thumbnail($slideshow_item);
112 return $this->getDefaultThumbnail();
121 public function getDefaultName(MediaInterface $media) {
122 // The default name will be the timestamp + number of slides.
123 $length = $this->getField($media, 'length');
124 if (!empty($length)) {
125 return $this->formatPlural($length,
126 '1 slide, created on @date',
127 '@count slides, created on @date',
128 ['@date' => \Drupal::service('date.formatter')->format($media->getCreatedTime(), 'custom', 'd/M/Y - H:i:s')]);
131 return parent::getDefaultName($media);