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