ca4fb6b563d7695b3283357594d2c8e751caa59d
[yaffs-website] / web / modules / contrib / media_entity / src / Plugin / Field / FieldFormatter / MediaThumbnailFormatter.php
1 <?php
2
3 namespace Drupal\media_entity\Plugin\Field\FieldFormatter;
4
5 use Drupal\Core\Field\FieldItemListInterface;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Session\AccountInterface;
8 use Drupal\image\Plugin\Field\FieldFormatter\ImageFormatter;
9 use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
10 use Drupal\Core\Render\RendererInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12 use Drupal\Core\Field\FieldDefinitionInterface;
13 use Drupal\Core\Entity\EntityStorageInterface;
14
15 /**
16  * Plugin implementation of the 'media_thumbnail' formatter.
17  *
18  * @FieldFormatter(
19  *   id = "media_thumbnail",
20  *   label = @Translation("Thumbnail"),
21  *   field_types = {
22  *     "entity_reference"
23  *   }
24  * )
25  */
26 class MediaThumbnailFormatter extends ImageFormatter {
27
28   /**
29    * The renderer service.
30    *
31    * @var \Drupal\Core\Render\RendererInterface
32    */
33   protected $renderer;
34
35   /**
36    * Constructs an ImageFormatter object.
37    *
38    * @param string $plugin_id
39    *   The plugin_id for the formatter.
40    * @param mixed $plugin_definition
41    *   The plugin implementation definition.
42    * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
43    *   The definition of the field to which the formatter is associated.
44    * @param array $settings
45    *   The formatter settings.
46    * @param string $label
47    *   The formatter label display setting.
48    * @param string $view_mode
49    *   The view mode.
50    * @param array $third_party_settings
51    *   Any third party settings settings.
52    * @param \Drupal\Core\Session\AccountInterface $current_user
53    *   The current user.
54    * @param \Drupal\Core\Render\RendererInterface $renderer
55    *   The renderer service.
56    */
57   public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountInterface $current_user, EntityStorageInterface $image_style_storage, RendererInterface $renderer) {
58     parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings, $current_user, $image_style_storage);
59     $this->renderer = $renderer;
60   }
61
62   /**
63    * {@inheritdoc}
64    */
65   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
66     return new static(
67       $plugin_id,
68       $plugin_definition,
69       $configuration['field_definition'],
70       $configuration['settings'],
71       $configuration['label'],
72       $configuration['view_mode'],
73       $configuration['third_party_settings'],
74       $container->get('current_user'),
75       $container->get('entity.manager')->getStorage('image_style'),
76       $container->get('renderer')
77     );
78   }
79
80   /**
81    * {@inheritdoc}
82    *
83    * This has to be overriden because FileFormatterBase expects $item to be
84    * of type \Drupal\file\Plugin\Field\FieldType\FileItem and calls
85    * isDisplayed() which is not in FieldItemInterface.
86    */
87   protected function needsEntityLoad(EntityReferenceItem $item) {
88     return !$item->hasNewEntity();
89   }
90
91   /**
92    * {@inheritdoc}
93    */
94   public function settingsForm(array $form, FormStateInterface $form_state) {
95     $element = parent::settingsForm($form, $form_state);
96
97     $link_types = [
98       'content' => $this->t('Content'),
99       'media' => $this->t('Media entity'),
100     ];
101     $element['image_link']['#options'] = $link_types;
102
103     return $element;
104   }
105
106   /**
107    * {@inheritdoc}
108    */
109   public function settingsSummary() {
110     $summary = parent::settingsSummary();
111
112     $link_types = [
113       'content' => $this->t('Linked to content'),
114       'media' => $this->t('Linked to media entity'),
115     ];
116     // Display this setting only if image is linked.
117     $image_link_setting = $this->getSetting('image_link');
118     if (isset($link_types[$image_link_setting])) {
119       $summary[] = $link_types[$image_link_setting];
120     }
121
122     return $summary;
123   }
124
125   /**
126    * {@inheritdoc}
127    */
128   public function viewElements(FieldItemListInterface $items, $langcode) {
129     $elements = [];
130     $media = $this->getEntitiesToView($items, $langcode);
131
132     // Early opt-out if the field is empty.
133     if (empty($media)) {
134       return $elements;
135     }
136
137     $url = NULL;
138     $image_link_setting = $this->getSetting('image_link');
139     // Check if the formatter involves a link.
140     if ($image_link_setting == 'content') {
141       $entity = $items->getEntity();
142       if (!$entity->isNew()) {
143         $url = $entity->toUrl();
144       }
145     }
146     elseif ($image_link_setting == 'media') {
147       $link_media = TRUE;
148     }
149
150     $image_style_setting = $this->getSetting('image_style');
151
152     /** @var \Drupal\media_entity\MediaInterface $media_item */
153     foreach ($media as $delta => $media_item) {
154       if (isset($link_media)) {
155         $url = $media_item->toUrl();
156       }
157
158       $elements[$delta] = [
159         '#theme' => 'image_formatter',
160         '#item' => $media_item->get('thumbnail'),
161         '#item_attributes' => [],
162         '#image_style' => $image_style_setting,
163         '#url' => $url,
164       ];
165
166       // Collect cache tags to be added for each item in the field.
167       $this->renderer->addCacheableDependency($elements[$delta], $media_item);
168     }
169
170     // Collect cache tags related to the image style setting.
171     $image_style = $this->imageStyleStorage->load($image_style_setting);
172     $this->renderer->addCacheableDependency($elements, $image_style);
173
174     return $elements;
175   }
176
177   /**
178    * {@inheritdoc}
179    */
180   public static function isApplicable(FieldDefinitionInterface $field_definition) {
181     // This formatter is only available for entity types that reference
182     // media entities.
183     $target_type = $field_definition->getFieldStorageDefinition()->getSetting('target_type');
184     return $target_type == 'media';
185   }
186
187 }