84377e5f5b3ceb5629ef75cccfe99af00792cf21
[yaffs-website] / web / modules / contrib / media_entity_slideshow / src / Plugin / MediaEntity / Type / Slideshow.php
1 <?php
2
3 namespace Drupal\media_entity_slideshow\Plugin\MediaEntity\Type;
4
5 use Drupal\media_entity\MediaInterface;
6 use Drupal\media_entity\MediaTypeBase;
7 use Drupal\Core\Form\FormStateInterface;
8
9 /**
10  * Provides media type plugin for Slideshows.
11  *
12  * @MediaType(
13  *   id = "slideshow",
14  *   label = @Translation("Slideshow"),
15  *   description = @Translation("Provides business logic and metadata for slideshows.")
16  * )
17  */
18 class Slideshow extends MediaTypeBase {
19
20   /**
21    * {@inheritdoc}
22    */
23   public function providedFields() {
24     $fields = array(
25       'length' => $this->t('Slideshow length'),
26     );
27
28     return $fields;
29   }
30
31   /**
32    * {@inheritdoc}
33    */
34   public function getField(MediaInterface $media, $name) {
35     $source_field = $this->configuration['source_field'];
36
37     switch ($name) {
38       case 'length':
39         return $media->{$source_field}->count();
40     }
41
42     return FALSE;
43   }
44
45   /**
46    * {@inheritdoc}
47    */
48   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
49     /** @var MediaBundleInterface $bundle */
50     $bundle = $form_state->getFormObject()->getEntity();
51     $options = [];
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();
59         }
60       }
61     }
62
63     $form['source_field'] = [
64       '#type' => 'select',
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,
69     ];
70
71     return $form;
72   }
73
74   /**
75    * {@inheritdoc}
76    */
77   public function attachConstraints(MediaInterface $media) {
78     parent::attachConstraints($media);
79
80     $source_field_name = $this->configuration['source_field'];
81     // Validate slideshow items count.
82     $media->getTypedData()->getDataDefinition()->addConstraint('ItemsCount', array('sourceFieldName' => $source_field_name));
83   }
84
85   /**
86    * {@inheritdoc}
87    */
88   public function getDefaultThumbnail() {
89     return $this->config->get('icon_base') . '/slideshow.png';
90   }
91
92   /**
93    * {@inheritdoc}
94    */
95   public function thumbnail(MediaInterface $media) {
96     $source_field = $this->configuration['source_field'];
97
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();
102     }
103
104     /** @var \Drupal\media_entity\MediaBundleInterface $bundle */
105     $bundle = $this->entityTypeManager->getStorage('media_bundle')->load($slideshow_item->bundle());
106     if (!$bundle) {
107       return $this->getDefaultThumbnail();
108     }
109
110     $thumbnail = $bundle->getType()->thumbnail($slideshow_item);
111     if (!$thumbnail) {
112       return $this->getDefaultThumbnail();
113     }
114
115     return $thumbnail;
116   }
117
118   /**
119    * {@inheritdoc}
120    */
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')]);
129     }
130
131     return parent::getDefaultName($media);
132   }
133
134 }