-
Notifications
You must be signed in to change notification settings - Fork 0
/
expanding_formatter.module
327 lines (314 loc) · 11.7 KB
/
expanding_formatter.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<?php
/**
* @file
* expanding_formatter.module.
*
* Provides a formatter that allows text fields to expand the rest of the
* trimmed or summary with a customizable "expand" link.
*/
/**
* Implements hook_theme().
*/
function expanding_formatter_theme() {
$hooks['expanding_formatter_ellipsis'] = array(
'variables' => array(
'attributes' => array(),
'ellipsis' => '…',
'settings' => expanding_formatter_default_settings(),
),
);
return $hooks;
}
/**
* Returns HTML for an ellipsis in an expanding formatter field.
*
* @param array $variables
* An associative array containing:
* - attributes: (optional) An array of HTML attributes to apply.
* - ellipsis: The visible text to use, defaults to '…'.
* - settings: An associative array containing the formatter settings used.
*
* @see expanding_formatter_default_settings()
*/
function theme_expanding_formatter_ellipsis($variables) {
return '<span' . backdrop_attributes($variables['attributes']) . '>' . $variables['ellipsis'] . '</span>';
}
/**
* Provide default settings for the formatter.
*/
function expanding_formatter_default_settings() {
return array(
'trim_length' => 200,
'trim_ellipsis' => TRUE,
'effect' => 'slide',
'trigger_expanded_label' => t('Expand'),
'trigger_collapsed_label' => t('Collapse'),
'trigger_classes' => 'button',
'inline' => TRUE,
);
}
/**
* Implements hook_field_formatter_info().
*/
function expanding_formatter_field_formatter_info() {
$settings = expanding_formatter_default_settings();
return array(
'expanding_formatter_text_trimmed' => array(
'label' => t('Trimmed (expandable)'),
'field types' => array('text', 'text_long', 'text_with_summary'),
'settings' => $settings,
),
'expanding_formatter_text_summary_or_trimmed' => array(
'label' => t('Summary or trimmed (expandable)'),
'field types' => array('text_with_summary'),
'settings' => $settings,
),
);
}
/**
* Implements hook_field_formatter_info().
*
* Position the expanded formatters to appear after the related text formatters.
*/
function expanding_formatter_field_formatter_info_alter(&$info) {
// Save the original data.
$trimmed = $info['expanding_formatter_text_trimmed'];
unset($info['expanding_formatter_text_trimmed']);
$summary = $info['expanding_formatter_text_summary_or_trimmed'];
unset($info['expanding_formatter_text_summary_or_trimmed']);
// Find the text formatters.
$formatters = array();
foreach ($info as $name => $data) {
$formatters[$name] = $data;
switch ($name) {
case 'text_trimmed':
$formatters['expanding_formatter_text_trimmed'] = $trimmed;
break;
case 'text_summary_or_trimmed':
$formatters['expanding_formatter_text_summary_or_trimmed'] = $summary;
break;
}
}
// Add them to the end of the list if the text formatters were not found.
if (!isset($info['expanding_formatter_text_trimmed'])) {
$formatters['expanding_formatter_text_trimmed'] = $trimmed;
}
if (!isset($info['expanding_formatter_text_summary_or_trimmed'])) {
$formatters['expanding_formatter_text_summary_or_trimmed'] = $summary;
}
$info = $formatters;
}
/**
* Implements hook_field_formatter_settings_form().
*/
function expanding_formatter_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$settings += expanding_formatter_default_settings();
$element = array();
if (strpos($display['type'], '_trimmed') !== FALSE) {
$element['trim_length'] = array(
'#type' => 'textfield',
'#title' => t('Trim length'),
'#size' => 10,
'#default_value' => $settings['trim_length'],
'#element_validate' => array('element_validate_integer_positive'),
'#required' => TRUE,
);
$element['trim_ellipsis'] = array(
'#type' => 'checkbox',
'#title' => $display['type'] === 'expanding_formatter_text_summary_or_trimmed' ? t('Append ellipsis on summary or trimmed content') : t('Append ellipsis on trimmed content'),
'#default_value' => $settings['trim_ellipsis'],
);
$element['effect'] = array(
'#type' => 'select',
'#title' => t('Animation effect'),
'#default_value' => $settings['effect'],
'#empty_value' => '',
'#options' => array(
'fade' => t('Fade'),
'slide' => t('Slide'),
),
);
$element['trigger_expanded_label'] = array(
'#type' => 'textfield',
'#title' => t('Trigger expanded label'),
'#default_value' => $settings['trigger_expanded_label'],
'#required' => TRUE,
);
$element['trigger_collapsed_label'] = array(
'#type' => 'textfield',
'#title' => t('Trigger collapsed label'),
'#description' => t('Enter text to make the content collapsible. If empty, content will only expand.'),
'#default_value' => $settings['trigger_collapsed_label'],
);
$element['trigger_classes'] = array(
'#type' => 'textfield',
'#title' => t('Trigger classes'),
'#description' => t('Provide additional CSS classes separated by spaces.'),
'#default_value' => $settings['trigger_classes'],
);
$element['inline'] = array(
'#type' => 'checkbox',
'#title' => t('Display elements as inline'),
'#description' => t('If enabled, all elements inside the formatted display will appear as inline. Disable if needed or desired.'),
'#default_value' => $settings['inline'],
);
}
return $element;
}
/**
* Implements hook_field_formatter_settings_summary().
*/
function expanding_formatter_field_formatter_settings_summary($field, $instance, $view_mode) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$settings += expanding_formatter_default_settings();
$summary = array();
if (strpos($display['type'], '_trimmed') !== FALSE) {
$summary[t('Trim length')] = $settings['trim_length'] . ($settings['trim_ellipsis'] ? ' (' . t('with ellipsis') . ')' : '');
$summary[t('CSS animation effect')] = (!empty($settings['effect']) ? $settings['effect'] : t('None'));
if (!empty($settings['trigger_collapsed_label'])) {
$summary[t('Trigger expanded label')] = $settings['trigger_expanded_label'];
}
else {
$summary[t('Trigger label')] = $settings['trigger_expanded_label'];
}
if (!empty($settings['trigger_collapsed_label'])) {
$summary[t('Trigger collapsed label')] = $settings['trigger_collapsed_label'];
}
if (!empty($settings['trigger_classes'])) {
$summary[t('Trigger classes')] = $settings['trigger_classes'];
}
}
$output = '';
foreach ($summary as $label => $value) {
$output .= '<strong>' . $label . '</strong>: ' . check_plain($value) . '<br />';
}
return $output;
}
/**
* Implements hook_field_formatter_view().
*/
function expanding_formatter_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
if (($items && $display['type'] === 'expanding_formatter_text_trimmed' || $display['type'] === 'expanding_formatter_text_summary_or_trimmed')) {
// Get the settings and extend default ones that don't exist.
$settings = $display['settings'];
$settings += expanding_formatter_default_settings();
// Create a link for the toggle.
$trigger_classes = array();
if (!empty($settings['trigger_classes'])) {
$trigger_classes = array_unique(array_merge($trigger_classes, explode(' ', $settings['trigger_classes'])));
}
$trigger = array(
'#theme_wrappers' => array('container'),
'#attributes' => array(
'class' => array(
'expanding-formatter-trigger',
),
),
'link' => array(
'#theme' => 'link',
'#text' => t($settings['trigger_expanded_label']),
'#path' => '#',
'#options' => array(
'external' => TRUE,
'html' => FALSE,
'attributes' => array(
'class' => $trigger_classes,
),
),
),
);
// Iterate through each item in the field, for unlimited values.
$attributes = array();
$attributes['class'] = array('expanding-formatter');
if (!empty($settings['inline'])) {
$attributes['data-inline'] = $settings['inline'];
}
if (!empty($settings['effect'])) {
$attributes['data-effect'] = $settings['effect'];
}
if (!empty($settings['trigger_collapsed_label'])) {
$attributes['data-expanded-label'] = t($settings['trigger_expanded_label']);
$attributes['data-collapsed-label'] = t($settings['trigger_collapsed_label']);
}
// Attach the necessary resources.
$module_path = backdrop_get_path('module', 'expanding_formatter');
foreach ($items as $delta => $item) {
$element[$delta] = array(
'#theme_wrappers' => array('container'),
'#expanding_formatter' => TRUE,
'#attributes' => $attributes,
'#attached' => array(
'css' => array($module_path . '/css/expanding_formatter.css'),
'js' => array($module_path . '/js/expanding_formatter.js'),
),
);
$original = _text_sanitize($instance, $langcode, $item, 'value');
$original = str_replace(array("\r", "\n"), '', $original);
if ($display['type'] === 'expanding_formatter_text_summary_or_trimmed' && !empty($item['summary'])) {
$element[$delta]['summary'] = array(
'#theme_wrappers' => array('container'),
'#attributes' => array(
'class' => array('expanding-formatter-summary'),
),
'value' => array(
'#markup' => _text_sanitize($instance, $langcode, $item, 'summary'),
),
);
}
else {
$element[$delta]['summary'] = array(
'#theme_wrappers' => array('container'),
'#attributes' => array(
'class' => array('expanding-formatter-summary'),
),
'value' => array(
'#markup' => text_summary($original, $instance['settings']['text_processing'] ? $item['format'] : NULL, $settings['trim_length']),
),
);
}
if ($instance['settings']['text_processing']) {
$format = filter_format_load($item['format']);
$filters = $format->filters;
}
// Strip tags if HTML corrector filter is used.
if (!empty($filters['filter_htmlcorrector']->status)) {
$content = str_replace(strip_tags($element[$delta]['summary']['value']['#markup']), '', $original);
}
else {
$content = str_replace($element[$delta]['summary']['value']['#markup'], '', $original);
}
// Render expandable content.
if (!empty($content)) {
if ($settings['trim_ellipsis']) {
$element[$delta]['ellipsis'] = array(
'#theme' => 'expanding_formatter_ellipsis',
'#settings' => $settings,
'#attributes' => array(
'class' => array('expanding-formatter-ellipsis'),
),
);
}
$element[$delta]['content'] = array(
'#theme_wrappers' => array('container'),
'#attributes' => array(
'class' => array('expanding-formatter-content'),
),
'value' => array(
'#markup' => $content,
),
);
$element[$delta]['trigger'] = $trigger;
}
// Content is short enough, render entire original output.
else {
$element[$delta] = array(
'#markup' => $original,
);
}
}
return $element;
}
}