Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / templates / module / src / Plugin / Field / FieldFormatter / imageformatter.php.twig
1 {% extends "base/class.php.twig" %}
2
3 {% block file_path %}
4 \Drupal\{{ module }}\Plugin\Field\FieldFormatter\{{ class_name }}.
5 {% endblock %}
6
7 {% block namespace_class %}
8 namespace Drupal\{{ module }}\Plugin\Field\FieldFormatter;
9 {% endblock %}
10
11 {% block use_class %}
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;
18 use Drupal\Core\Url;
19 use Drupal\Core\Utility\LinkGeneratorInterface;
20 use Symfony\Component\DependencyInjection\ContainerInterface;
21 use Drupal\Core\Form\FormStateInterface;
22 use Drupal\Core\Cache\Cache;
23 {% endblock %}
24
25 {% block class_declaration %}
26 /**
27  * Plugin implementation of the '{{ plugin_id }}' formatter.
28  *
29  * @FieldFormatter(
30  *   id = "{{ plugin_id }}",
31  *   label = @Translation("{{ label }}"),
32  *   field_types = {
33  *     "image"
34  *   }
35  * )
36  */
37 class {{ class_name }} extends ImageFormatterBase implements ContainerFactoryPluginInterface {% endblock %}
38 {% block class_methods %}
39 /**
40  * The current user.
41  *
42  * @var \Drupal\Core\Session\AccountInterface
43  */
44  protected $currentUser;
45
46 /**
47  * The link generator.
48  *
49  * @var \Drupal\Core\Utility\LinkGeneratorInterface
50  */
51  protected $linkGenerator;
52
53 /**
54  * The image style entity storage.
55  *
56  * @var \Drupal\Core\Entity\EntityStorageInterface
57  */
58  protected $imageStyleStorage;
59
60 /**
61  * Constructs a new {{ class_name }} object.
62  *
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
74  *   The view mode.
75  * @param array $third_party_settings
76  *   Any third party settings settings.
77  * @param \Drupal\Core\Session\AccountInterface $current_user
78  *   The 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.
83  */
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;
89   }
90
91 /**
92  * {@inheritdoc}
93  */
94  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
95    return new static(
96      $plugin_id,
97      $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')
106    );
107  }
108
109 /**
110  * {@inheritdoc}
111  */
112  public static function defaultSettings() {
113    return [
114      'image_style' => '',
115      'image_link' => '',
116    ] + parent::defaultSettings();
117  }
118
119 /**
120  * {@inheritdoc}
121  */
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'),
126      '#type' => 'select',
127      '#default_value' => $this->getSetting('image_style'),
128      '#empty_option' => t('None (original image)'),
129      '#options' => $image_styles,
130      '#description' => [
131        '#markup' => $this->linkGenerator->generate($this->t('Configure Image Styles'), new Url('entity.image_style.collection')),
132        '#access' => $this->currentUser->hasPermission('administer image styles'),
133      ],
134    ];
135
136    return $element;
137  }
138
139 /**
140  * {@inheritdoc}
141  */
142  public function settingsSummary() {
143    $summary = [];
144    $image_styles = image_style_options(FALSE);
145
146    // Unset possible 'No defined styles' option.
147    unset($image_styles['']);
148
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]]);
154    }
155    else {
156      $summary[] = t('Original image');
157    }
158
159    return $summary;
160  }
161
162 /**
163  * {@inheritdoc}
164  */
165  public function viewElements(FieldItemListInterface $items, $langcode) {
166    $elements = [];
167     $files = $this->getEntitiesToView($items, $langcode);
168
169    // Early opt-out if the field is empty.
170    if (empty($files)) {
171      return $elements;
172    }
173
174    $url = NULL;
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();
181      }
182    }
183    elseif ($image_link_setting == 'file') {
184      $link_file = TRUE;
185    }
186
187    $image_style_setting = $this->getSetting('image_style');
188
189    // Collect cache tags to be added for each item in the field.
190    $cache_tags = [];
191    if (!empty($image_style_setting)) {
192      $image_style = $this->imageStyleStorage->load($image_style_setting);
193      $cache_tags = $image_style->getCacheTags();
194    }
195
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));
200      }
201
202      $cache_tags = Cache::mergeTags($cache_tags, $file->getCacheTags());
203
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);
209
210      $elements[$delta] = [
211        '#theme' => 'image_formatter',
212        '#item' => $item,
213        '#item_attributes' => $item_attributes,
214        '#image_style' => $image_style_setting,
215        '#url' => $url,
216        '#cache' => [
217          'tags' => $cache_tags,
218        ],
219      ];
220    }
221
222    return $elements;
223  }
224 {% endblock %}