-
Notifications
You must be signed in to change notification settings - Fork 0
/
field_formatter_tag.module
231 lines (195 loc) · 8.54 KB
/
field_formatter_tag.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
/**
* @file
* Provides custom HTML class settings for field formatters.
*/
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
/**
* Implements hook_field_formatter_settings_summary_alter().
*/
function field_formatter_tag_field_formatter_settings_summary_alter(&$summary, $context) {
foreach ([
'tag' => 'Tag',
'tag_class' => 'Tag Class',
'label_tag' => 'Label Tag',
'label_tag_class' => 'Label Tag Class',
'wrapper_class' => 'Field Wrapper Class',
'wrapper_item_class' => 'Field Wrapper Item Class',
] as $param => $label) {
$setting = $context['formatter']->getThirdPartySetting('field_formatter_tag', $param);
if (!empty($setting)) {
$summary[] = t('@label: @param', [
'@label' => $label,
'@param' => Xss::filter($setting, []),
]);
}
}
}
/**
* Implements hook_field_formatter_third_party_settings_form().
*
* Adds a 'class' textfield to all formatters.
*/
function field_formatter_tag_field_formatter_third_party_settings_form(\Drupal\Core\Field\FormatterInterface $plugin, \Drupal\Core\Field\FieldDefinitionInterface $field_definition, $view_mode, $form, \Drupal\Core\Form\FormStateInterface $form_state) {
$element['tag'] = [
'#type' => 'select',
'#options' => [NULL => t('None')] + array_combine(Xss::getAdminTagList(), Xss::getAdminTagList()),
'#title' => t('Field Formatter Tag'),
'#default_value' => $plugin->getThirdPartySetting('field_formatter_tag', 'tag'),
];
$element['tag_class'] = [
'#type' => 'textfield',
'#title' => t('Field Formatter Tag Class'),
'#default_value' => $plugin->getThirdPartySetting('field_formatter_tag', 'tag_class', ''),
'#placeholder' => 'tag-class-wrapper',
];
$element['label_tag'] = [
'#type' => 'select',
'#options' => [NULL => t('None')] + array_combine(Xss::getAdminTagList(), Xss::getAdminTagList()),
'#title' => t('Field Formatter Label Tag'),
'#default_value' => $plugin->getThirdPartySetting('field_formatter_tag', 'label_tag'),
];
$element['label_tag_class'] = [
'#type' => 'textfield',
'#title' => t('Field Formatter Label Class'),
'#default_value' => $plugin->getThirdPartySetting('field_formatter_tag', 'label_tag_class', ''),
'#placeholder' => 'label-tag-class-wrapper',
];
$element['wrapper_class'] = [
'#type' => 'textfield',
'#title' => t('Field Wrapper Class'),
'#default_value' => $plugin->getThirdPartySetting('field_formatter_tag', 'wrapper_class', ''),
'#placeholder' => 'field-wrapper-items',
];
$element['wrapper_item_class'] = [
'#type' => 'textarea',
'#title' => t('Field Wrapper Item Class'),
'#description' => t('Add a class per line if you would like to have multiple containers, ex div.class_line_1 > div.class_line_2'),
'#default_value' => $plugin->getThirdPartySetting('field_formatter_tag', 'wrapper_item_class', ''),
'#placeholder' => 'field-wrapper-item',
];
return $element;
}
/**
* Implements hook_preprocess_HOOK() for field templates.
*
* Adds a custom class to the field's class attribute
* according to the field formatter settings.
*/
function field_formatter_tag_preprocess_field(&$variables) {
// Get a few convenient handles.
$entity = $variables['element']['#object'];
$field_name = $variables['element']['#field_name'];
$view_mode = $variables['element']['#view_mode'];
// Drill down to field formatter settings.
$render_display = EntityViewDisplay::collectRenderDisplay($entity, $view_mode);
$field_display = $render_display->getComponent($field_name);
// Add class for whole field.
if (isset($field_display['third_party_settings']['field_formatter_tag'])) {
if (!empty($field_display['third_party_settings']['field_formatter_tag']['tag'])) {
foreach (\Drupal\Core\Render\Element::children($variables['items']) as $child) {
$variables['items'][$child]['content']['#theme_wrappers']['container_tag'] = [
'#tag' => Html::escape($field_display['third_party_settings']['field_formatter_tag']['tag']),
'#attributes' => [
'class' => [
Html::escape(isset($field_display['third_party_settings']['field_formatter_tag']['tag_class']) ? $field_display['third_party_settings']['field_formatter_tag']['tag_class'] : 'tag-class-wrapper'),
],
],
];
}
}
elseif (!empty($field_display['third_party_settings']['field_formatter_tag']['tag_class'])) {
foreach (\Drupal\Core\Render\Element::children($variables['items']) as $child) {
switch (TRUE) {
case isset($variables['items'][$child]['content']['#options']['attributes']):
$variables['items'][$child]['content']['#options']['attributes']['class'][] = Html::escape($field_display['third_party_settings']['field_formatter_tag']['tag_class']);
break;
case isset($variables['items'][$child]['content']['#attributes']):
$variables['items'][$child]['content']['#attributes']['class'][] = Html::escape($field_display['third_party_settings']['field_formatter_tag']['tag_class']);
break;
default:
$variables['items'][$child]['attributes']->addClass(Html::escape($field_display['third_party_settings']['field_formatter_tag']['tag_class']));
break;
}
}
}
if (!empty($field_display['third_party_settings']['field_formatter_tag']['label_tag'])) {
$variables['label'] = [
'#type' => 'html_tag',
'#tag' => $field_display['third_party_settings']['field_formatter_tag']['label_tag'],
'#value' => $variables['label'],
'#attributes' => [
'class' => [
Html::escape(isset($field_display['third_party_settings']['field_formatter_tag']['label_tag_class']) ? $field_display['third_party_settings']['field_formatter_tag']['label_tag_class'] : 'label-tag-class-wrapper'),
],
],
];
}
if (!empty($field_display['third_party_settings']['field_formatter_tag']['wrapper_class'])) {
$variables['attributes']['class'][] = $field_display['third_party_settings']['field_formatter_tag']['wrapper_class'];
$item_class = [];
foreach (explode(' ', $field_display['third_party_settings']['field_formatter_tag']['wrapper_class']) as $part) {
$item_class[] = implode('-', [
$part,
'item',
]);
}
$item_class = implode(' ', $item_class);
if (!empty($field_display['third_party_settings']['field_formatter_tag']['wrapper_item_class'])) {
$item_class = $field_display['third_party_settings']['field_formatter_tag']['wrapper_item_class'];
}
_field_formatter_tag_add_item_wrappers($item_class, $variables['items']);
}
else {
if (!empty($field_display['third_party_settings']['field_formatter_tag']['wrapper_item_class'])) {
_field_formatter_tag_add_item_wrappers($field_display['third_party_settings']['field_formatter_tag']['wrapper_item_class'], $variables['items']);
}
}
}
}
/**
* @param string $class
* @param array $items
*/
function _field_formatter_tag_add_item_wrappers($class, array &$items) {
$item_class = explode("\r\n", $class);
switch (TRUE) {
case count($item_class) > 1:
foreach (\Drupal\Core\Render\Element::children($items) as $child) {
$items[$child]['attributes']->addClass(reset($item_class));
$items[$child]['content'] = _field_formatter_tag_build_item_wrappers(array_slice($item_class, 1), $items[$child]['content']);
}
break;
default:
foreach (\Drupal\Core\Render\Element::children($items) as $child) {
$items[$child]['attributes']->addClass($item_class);
}
break;
}
}
/**
* @param array $classes_array
* @param array $item_child
*
* @return array
*/
function _field_formatter_tag_build_item_wrappers(array $classes_array, array $item_child) {
$build = [
'#type' => 'container',
'#attributes' => [
'class' => [
reset($classes_array),
],
],
];
if (empty(array_slice($classes_array, 1))) {
$build['child'] = $item_child;
return $build;
}
$next_classes = array_slice($classes_array, 1);
$next_key = reset($next_classes);
$build[$next_key] = _field_formatter_tag_build_item_wrappers($next_classes, $item_child);
return $build;
}