1 {% extends "base/class.php.twig" %}
4 \Drupal\{{ module }}\Plugin\Field\FieldFormatter\{{ class_name }}.
7 {% block namespace_class %}
8 namespace Drupal\{{ module }}\Plugin\Field\FieldFormatter;
12 use Drupal\image\Plugin\Field\FieldFormatter\ImageFormatterBase;
13 use Drupal\Core\Entity\EntityStorageInterface;
14 use Drupal\Core\Field\FieldItemListInterface;
15 use Drupal\Core\Field\FieldDefinitionInterface;
16 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
17 use Drupal\Core\Session\AccountInterface;
19 use Drupal\Core\Utility\LinkGeneratorInterface;
20 use Symfony\Component\DependencyInjection\ContainerInterface;
21 use Drupal\Core\Form\FormStateInterface;
22 use Drupal\Core\Cache\Cache;
25 {% block class_declaration %}
27 * Plugin implementation of the '{{ plugin_id }}' formatter.
30 * id = "{{ plugin_id }}",
31 * label = @Translation("{{ label }}"),
37 class {{ class_name }} extends ImageFormatterBase implements ContainerFactoryPluginInterface {% endblock %}
38 {% block class_methods %}
42 * @var \Drupal\Core\Session\AccountInterface
44 protected $currentUser;
49 * @var \Drupal\Core\Utility\LinkGeneratorInterface
51 protected $linkGenerator;
54 * The image style entity storage.
56 * @var \Drupal\Core\Entity\EntityStorageInterface
58 protected $imageStyleStorage;
61 * Constructs a new {{ class_name }} object.
63 * @param string $plugin_id
64 * The plugin_id for the formatter.
65 * @param mixed $plugin_definition
66 * The plugin implementation definition.
67 * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
68 * The definition of the field to which the formatter is associated.
69 * @param array $settings
70 * The formatter settings.
71 * @param string $label
72 * The formatter label display setting.
73 * @param string $view_mode
75 * @param array $third_party_settings
76 * Any third party settings settings.
77 * @param \Drupal\Core\Session\AccountInterface $current_user
79 * @param \Drupal\Core\Utility\LinkGeneratorInterface $link_generator
80 * The link generator service.
81 * @param \Drupal\Core\Entity\EntityStorageInterface $image_style_storage
82 * The entity storage for the image.
84 public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountInterface $current_user, LinkGeneratorInterface $link_generator, EntityStorageInterface $image_style_storage) {
85 parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
86 $this->currentUser = $current_user;
87 $this->linkGenerator = $link_generator;
88 $this->imageStyleStorage = $image_style_storage;
94 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
98 $configuration['field_definition'],
99 $configuration['settings'],
100 $configuration['label'],
101 $configuration['view_mode'],
102 $configuration['third_party_settings'],
103 $container->get('current_user'),
104 $container->get('link_generator'),
105 $container->get('entity_type.manager')->getStorage('image_style')
112 public static function defaultSettings() {
116 ] + parent::defaultSettings();
122 public function settingsForm(array $form, FormStateInterface $form_state) {
123 $image_styles = image_style_options(FALSE);
124 $element['image_style'] = [
125 '#title' => t('Image style'),
127 '#default_value' => $this->getSetting('image_style'),
128 '#empty_option' => t('None (original image)'),
129 '#options' => $image_styles,
131 '#markup' => $this->linkGenerator->generate($this->t('Configure Image Styles'), new Url('entity.image_style.collection')),
132 '#access' => $this->currentUser->hasPermission('administer image styles'),
142 public function settingsSummary() {
144 $image_styles = image_style_options(FALSE);
146 // Unset possible 'No defined styles' option.
147 unset($image_styles['']);
149 // Styles could be lost because of enabled/disabled modules that defines
150 // their styles in code.
151 $image_style_setting = $this->getSetting('image_style');
152 if (isset($image_styles[$image_style_setting])) {
153 $summary[] = t('Image style: @style', ['@style' => $image_styles[$image_style_setting]]);
156 $summary[] = t('Original image');
165 public function viewElements(FieldItemListInterface $items, $langcode) {
167 $files = $this->getEntitiesToView($items, $langcode);
169 // Early opt-out if the field is empty.
175 $image_link_setting = $this->getSetting('image_link');
176 // Check if the formatter involves a link.
177 if ($image_link_setting == 'content') {
178 $entity = $items->getEntity();
179 if (!$entity->isNew()) {
180 $url = $entity->toUrl();
183 elseif ($image_link_setting == 'file') {
187 $image_style_setting = $this->getSetting('image_style');
189 // Collect cache tags to be added for each item in the field.
191 if (!empty($image_style_setting)) {
192 $image_style = $this->imageStyleStorage->load($image_style_setting);
193 $cache_tags = $image_style->getCacheTags();
196 foreach ($files as $delta => $file) {
197 if (isset($link_file)) {
198 $image_uri = $file->getFileUri();
199 $url = Url::fromUri(file_create_url($image_uri));
202 $cache_tags = Cache::mergeTags($cache_tags, $file->getCacheTags());
204 // Extract field item attributes for the theme function, and unset them
205 // from the $item so that the field template does not re-render them.
206 $item = $file->_referringItem;
207 $item_attributes = $item->_attributes;
208 unset($item->_attributes);
210 $elements[$delta] = [
211 '#theme' => 'image_formatter',
213 '#item_attributes' => $item_attributes,
214 '#image_style' => $image_style_setting,
217 'tags' => $cache_tags,