3 namespace Drupal\media_entity\Plugin\Field\FieldFormatter;
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;
16 * Plugin implementation of the 'media_thumbnail' formatter.
19 * id = "media_thumbnail",
20 * label = @Translation("Thumbnail"),
26 class MediaThumbnailFormatter extends ImageFormatter {
29 * The renderer service.
31 * @var \Drupal\Core\Render\RendererInterface
36 * Constructs an ImageFormatter object.
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
50 * @param array $third_party_settings
51 * Any third party settings settings.
52 * @param \Drupal\Core\Session\AccountInterface $current_user
54 * @param \Drupal\Core\Render\RendererInterface $renderer
55 * The renderer service.
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;
65 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $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')
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.
87 protected function needsEntityLoad(EntityReferenceItem $item) {
88 return !$item->hasNewEntity();
94 public function settingsForm(array $form, FormStateInterface $form_state) {
95 $element = parent::settingsForm($form, $form_state);
98 'content' => $this->t('Content'),
99 'media' => $this->t('Media entity'),
101 $element['image_link']['#options'] = $link_types;
109 public function settingsSummary() {
110 $summary = parent::settingsSummary();
113 'content' => $this->t('Linked to content'),
114 'media' => $this->t('Linked to media entity'),
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];
128 public function viewElements(FieldItemListInterface $items, $langcode) {
130 $media = $this->getEntitiesToView($items, $langcode);
132 // Early opt-out if the field is empty.
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();
146 elseif ($image_link_setting == 'media') {
150 $image_style_setting = $this->getSetting('image_style');
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();
158 $elements[$delta] = [
159 '#theme' => 'image_formatter',
160 '#item' => $media_item->get('thumbnail'),
161 '#item_attributes' => [],
162 '#image_style' => $image_style_setting,
166 // Collect cache tags to be added for each item in the field.
167 $this->renderer->addCacheableDependency($elements[$delta], $media_item);
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);
180 public static function isApplicable(FieldDefinitionInterface $field_definition) {
181 // This formatter is only available for entity types that reference
183 $target_type = $field_definition->getFieldStorageDefinition()->getSetting('target_type');
184 return $target_type == 'media';