11<?php
22
3- /**
4- * @file
5- * Contains \Drupal\file_entity\Plugin\Field\FieldFormatter\FileImageFormatter.
6- */
7-
83namespace Drupal \file_entity \Plugin \Field \FieldFormatter ;
94
105use Drupal \Core \Cache \Cache ;
11- use Drupal \Core \Field \EntityReferenceFieldItemListInterface ;
6+ use Drupal \Core \Entity \EntityFieldManagerInterface ;
7+ use Drupal \Core \Entity \EntityStorageInterface ;
8+ use Drupal \Core \Field \FieldConfigInterface ;
9+ use Drupal \Core \Field \FieldDefinitionInterface ;
1210use Drupal \Core \Field \FieldItemListInterface ;
1311use Drupal \Core \Form \FormStateInterface ;
12+ use Drupal \Core \Session \AccountInterface ;
1413use Drupal \image \Plugin \Field \FieldFormatter \ImageFormatter ;
14+ use Symfony \Component \DependencyInjection \ContainerInterface ;
1515
1616/**
1717 * Implementation of the 'image' formatter for the file_entity files.
2626 */
2727class FileImageFormatter extends ImageFormatter {
2828
29+ /**
30+ * The entity field manager.
31+ *
32+ * @var \Drupal\Core\Entity\EntityFieldManagerInterface
33+ */
34+ protected $ entityFieldManager ;
35+
36+ /**
37+ * Constructs on FileImageFormatter object.
38+ *
39+ * @param string $plugin_id
40+ * The plugin_id for the formatter.
41+ * @param mixed $plugin_definition
42+ * The plugin implementation definition.
43+ * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
44+ * The definition of the field to which the formatter is associated.
45+ * @param array $settings
46+ * The formatter settings.
47+ * @param string $label
48+ * The formatter label display setting.
49+ * @param string $view_mode
50+ * The view mode.
51+ * @param array $third_party_settings
52+ * Any third party settings settings.
53+ * @param \Drupal\Core\Session\AccountInterface $current_user
54+ * The current user.
55+ * @param \Drupal\Core\Entity\EntityStorageInterface $image_style_storage
56+ * The image style entity storage class.
57+ * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
58+ * The entity field manager.
59+ */
60+ public function __construct ($ plugin_id , $ plugin_definition , FieldDefinitionInterface $ field_definition , array $ settings , $ label , $ view_mode , array $ third_party_settings , AccountInterface $ current_user , EntityStorageInterface $ image_style_storage , EntityFieldManagerInterface $ entity_field_manager ) {
61+ parent ::__construct ($ plugin_id , $ plugin_definition , $ field_definition , $ settings , $ label , $ view_mode , $ third_party_settings , $ current_user , $ image_style_storage );
62+ $ this ->entityFieldManager = $ entity_field_manager ;
63+ }
64+
65+ /**
66+ * {@inheritdoc}
67+ */
68+ public static function create (ContainerInterface $ container , array $ configuration , $ plugin_id , $ plugin_definition ) {
69+ return new static (
70+ $ plugin_id ,
71+ $ plugin_definition ,
72+ $ configuration ['field_definition ' ],
73+ $ configuration ['settings ' ],
74+ $ configuration ['label ' ],
75+ $ configuration ['view_mode ' ],
76+ $ configuration ['third_party_settings ' ],
77+ $ container ->get ('current_user ' ),
78+ $ container ->get ('entity.manager ' )->getStorage ('image_style ' ),
79+ $ container ->get ('entity_field.manager ' )
80+ );
81+ }
82+
83+ /**
84+ * {@inheritdoc}
85+ */
86+ public static function defaultSettings () {
87+ return [
88+ 'title ' => 'field_image_title_text ' ,
89+ 'alt ' => 'field_image_alt_text ' ,
90+ ] + parent ::defaultSettings ();
91+ }
92+
93+ /**
94+ * {@inheritdoc}
95+ */
96+ public function settingsSummary () {
97+ $ summary = parent ::settingsSummary ();
98+ if ($ this ->getSetting ('title ' ) == '_none ' ) {
99+ $ summary [] = $ this ->t ('Title attribute is hidden. ' );
100+ } else {
101+ $ summary [] = $ this ->t ('Field used for the image title attribute: @title ' , ['@title ' => $ this ->getSetting ('title ' )]);
102+ }
103+ if ($ this ->getSetting ('alt ' ) == '_none ' ) {
104+ $ summary [] = $ this ->t ('Alt attribute is hidden. ' );
105+ } else {
106+ $ summary [] = $ this ->t ('Field used for the image alt attribute: @alt ' , ['@alt ' => $ this ->getSetting ('alt ' )]);
107+ }
108+
109+ return $ summary ;
110+ }
111+
29112 /**
30113 * {@inheritdoc}
31114 */
@@ -60,7 +143,12 @@ public function viewElements(FieldItemListInterface $items, $langcode) {
60143 'tags ' => $ cache_tags ,
61144 ],
62145 ];
63-
146+ foreach (['title ' , 'alt ' ] as $ element_name ) {
147+ $ field_name = $ this ->getSetting ($ element_name );
148+ if ($ field_name !== '_none ' && $ file ->hasField ($ field_name )) {
149+ $ elements [0 ]['# ' . $ element_name ] = $ file ->$ field_name ->value ;
150+ }
151+ }
64152 return $ elements ;
65153 }
66154
@@ -75,6 +163,34 @@ public function prepareView(array $entities_items) {}
75163 public function settingsForm (array $ form , FormStateInterface $ form_state ) {
76164 $ element = parent ::settingsForm ($ form , $ form_state );
77165 unset($ element ['image_link ' ]);
166+ $ available_fields = $ this ->entityFieldManager ->getFieldDefinitions (
167+ $ form ['#entity_type ' ],
168+ $ form ['#bundle ' ]
169+ );
170+ $ options = [];
171+ foreach ($ available_fields as $ field_name => $ field_definition ) {
172+ if ($ field_definition instanceof FieldConfigInterface && $ field_definition ->getType () == 'string ' ) {
173+ $ options [$ field_name ] = $ field_definition ->label ();
174+ }
175+ }
176+ $ element ['title ' ] = [
177+ '#title ' => $ this ->t ('Image title field ' ),
178+ '#description ' => $ this ->t ('The field that is used as source for the image title attribute. ' ),
179+ '#type ' => 'select ' ,
180+ '#options ' => $ options ,
181+ '#default_value ' => $ this ->getSetting ('title ' ),
182+ '#empty_option ' => $ this ->t ('No title attribute ' ),
183+ '#empty_value ' => '_none ' ,
184+ ];
185+ $ element ['alt ' ] = [
186+ '#title ' => $ this ->t ('Image alt field ' ),
187+ '#description ' => $ this ->t ('The field that is used as source for the image alt attribute. ' ),
188+ '#type ' => 'select ' ,
189+ '#options ' => $ options ,
190+ '#default_value ' => $ this ->getSetting ('alt ' ),
191+ '#empty_option ' => $ this ->t ('No alt attribute ' ),
192+ '#empty_value ' => '_none ' ,
193+ ];
78194 return $ element ;
79195 }
80196
0 commit comments