3 namespace Drupal\entity_browser\Plugin\EntityBrowser\FieldWidgetDisplay;
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;
15 * Displays image thumbnail.
17 * @EntityBrowserFieldWidgetDisplay(
19 * label = @Translation("Image thumbnail"),
20 * description = @Translation("Displays image files as thumbnails")
23 class ImageThumbnail extends FieldWidgetDisplayBase implements ContainerFactoryPluginInterface {
26 * Entity type manager service.
28 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
30 protected $entityTypeManager;
33 * Constructs widget plugin.
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.
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;
52 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
57 $container->get('entity_type.manager')
64 public function view(EntityInterface $entity) {
66 '#theme' => 'image_style',
67 '#style_name' => $this->configuration['image_style'],
68 '#title' => $entity->label(),
69 '#alt' => $entity->label(),
70 '#uri' => $entity->getFileUri(),
77 public function settingsForm(array $form, FormStateInterface $form_state) {
79 foreach ($this->entityTypeManager->getStorage('image_style')->loadMultiple() as $id => $image_style) {
80 $options[$id] = $image_style->label();
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,
97 public function isApplicable(EntityTypeInterface $entity_type) {
98 return $entity_type->isSubclassOf(FileInterface::class);
104 public function defaultConfiguration() {
106 'image_style' => 'thumbnail',
107 ] + parent::defaultConfiguration();
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();
118 return $dependencies;