3519803a59c776a08b89167a619aa42501426ad0
[yaffs-website] / Plugin / EntityBrowser / FieldWidgetDisplay / ImageThumbnail.php
1 <?php
2
3 namespace Drupal\entity_browser\Plugin\EntityBrowser\FieldWidgetDisplay;
4
5 use Drupal\Core\Entity\EntityInterface;
6 use Drupal\Core\Entity\EntityTypeManagerInterface;
7 use Drupal\Core\Entity\EntityTypeInterface;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
10 use Drupal\file\FileInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12 use Drupal\entity_browser\FieldWidgetDisplayBase;
13
14 /**
15  * Displays image thumbnail.
16  *
17  * @EntityBrowserFieldWidgetDisplay(
18  *   id = "thumbnail",
19  *   label = @Translation("Image thumbnail"),
20  *   description = @Translation("Displays image files as thumbnails")
21  * )
22  */
23 class ImageThumbnail extends FieldWidgetDisplayBase implements ContainerFactoryPluginInterface {
24
25   /**
26    * Entity type manager service.
27    *
28    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
29    */
30   protected $entityTypeManager;
31
32   /**
33    * Constructs widget plugin.
34    *
35    * @param array $configuration
36    *   A configuration array containing information about the plugin instance.
37    * @param string $plugin_id
38    *   The plugin_id for the plugin instance.
39    * @param mixed $plugin_definition
40    *   The plugin implementation definition.
41    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
42    *   Entity manager service.
43    */
44   public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager) {
45     parent::__construct($configuration, $plugin_id, $plugin_definition);
46     $this->entityTypeManager = $entity_type_manager;
47   }
48
49   /**
50    * {@inheritdoc}
51    */
52   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
53     return new static(
54       $configuration,
55       $plugin_id,
56       $plugin_definition,
57       $container->get('entity_type.manager')
58     );
59   }
60
61   /**
62    * {@inheritdoc}
63    */
64   public function view(EntityInterface $entity) {
65     return [
66       '#theme' => 'image_style',
67       '#style_name' => $this->configuration['image_style'],
68       '#title' => $entity->label(),
69       '#alt' => $entity->label(),
70       '#uri' => $entity->getFileUri(),
71     ];
72   }
73
74   /**
75    * {@inheritdoc}
76    */
77   public function settingsForm(array $form, FormStateInterface $form_state) {
78     $options = [];
79     foreach ($this->entityTypeManager->getStorage('image_style')->loadMultiple() as $id => $image_style) {
80       $options[$id] = $image_style->label();
81     }
82
83     return [
84       'image_style' => [
85         '#type' => 'select',
86         '#title' => $this->t('Image style'),
87         '#description' => $this->t('Select image style to be used to display thumbnails.'),
88         '#default_value' => $this->configuration['image_style'],
89         '#options' => $options,
90       ],
91     ];
92   }
93
94   /**
95    * {@inheritdoc}
96    */
97   public function isApplicable(EntityTypeInterface $entity_type) {
98     return $entity_type->isSubclassOf(FileInterface::class);
99   }
100
101   /**
102    * {@inheritdoc}
103    */
104   public function defaultConfiguration() {
105     return [
106       'image_style' => 'thumbnail',
107     ] + parent::defaultConfiguration();
108   }
109
110   /**
111    * {@inheritdoc}
112    */
113   public function calculateDependencies() {
114     $dependencies = parent::calculateDependencies();
115     if ($image_style = $this->entityTypeManager->getStorage('image_style')->load($this->configuration['image_style'])) {
116       $dependencies[$image_style->getConfigDependencyKey()][] = $image_style->getConfigDependencyName();
117     }
118     return $dependencies;
119   }
120
121 }