forked from mishac/paragraphs_pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iu_paragraphs.module
590 lines (527 loc) · 18.8 KB
/
iu_paragraphs.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
<?php
/**
* @file
* Contains iu_paragraphs.module.
*/
use Drupal\Core\Url;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\image\Entity\ImageStyle;
/**
* Implements hook_help().
*/
function iu_paragraphs_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.iu_paragraphs':
$text = file_get_contents(dirname(__FILE__) . '/README.md');
if (!\Drupal::moduleHandler()->moduleExists('markdown')) {
return '<pre>' . $text . '</pre>';
}
else {
// Use the Markdown filter to render the README.
$filter_manager = \Drupal::service('plugin.manager.filter');
$settings = \Drupal::configFactory()->get('markdown.settings')->getRawData();
$config = ['settings' => $settings];
$filter = $filter_manager->createInstance('markdown', $config);
return $filter->process($text, 'en');
}
}
return NULL;
}
/**
* Implements hook_theme().
*/
function iu_paragraphs_theme($existing, $type, $theme, $path) {
return [
'field__iu_banner_header' => [
'base hook' => 'field',
],
'field__iu_banner_subhead' => [
'base hook' => 'field',
],
'field__iu_callout_content' => [
'base hook' => 'field',
],
'field__iu_feature_carousel_title' => [
'base hook' => 'field',
],
'field__iu_feature_carousel_more_link' => [
'base hook' => 'field',
],
'field__iu_feature_link' => [
'base hook' => 'field',
],
'field__iu_feature_subhead' => [
'base hook' => 'field',
],
'field__iu_image_caption_title' => [
'base hook' => 'field',
],
'field__iu_panel_content' => [
'base hook' => 'field',
],
'field__iu_panel_header' => [
'base hook' => 'field',
],
'field__iu_quote_attribution' => [
'base hook' => 'field',
],
'field__iu_section_title' => [
'base hook' => 'field',
],
'field__iu_stat_content' => [
'base hook' => 'field',
],
'field__iu_stat_link' => [
'base hook' => 'field',
],
'field__iu_stat_number' => [
'base hook' => 'field',
],
'paragraph__iu_accordion' => [
'base hook' => 'paragraph',
],
'paragraph__iu_banner' => [
'base hook' => 'paragraph',
],
'paragraph__iu_callout_pullquote' => [
'base hook' => 'paragraph',
],
'paragraph__iu_carousel' => [
'base hook' => 'paragraph',
],
'paragraph__iu_cta' => [
'base hook' => 'paragraph',
],
'paragraph__iu_tabs' => [
'base hook' => 'paragraph',
],
'paragraph__iu_feature' => [
'base hook' => 'paragraph',
],
'paragraph__iu_feature_carousel' => [
'base hook' => 'paragraph',
],
'paragraph__iu_grid_builder' => [
'base hook' => 'paragraph',
],
'paragraph__iu_grid_item' => [
'base hook' => 'paragraph',
],
'paragraph__iu_image' => [
'base hook' => 'paragraph',
],
'paragraph__iu_panel' => [
'base hook' => 'paragraph',
],
'paragraph__iu_panel_interactive' => [
'base hook' => 'paragraph',
],
'paragraph__iu_section' => [
'base hook' => 'paragraph',
],
'paragraph__iu_stat' => [
'base hook' => 'paragraph',
],
'paragraph__iu_stats' => [
'base hook' => 'paragraph',
],
];
}
/**
* Prepares variables for `field--iu-banner-link.html.twig`.
*/
function iu_paragraphs_preprocess_field__iu_banner_link(&$variables) {
// Add the 'button' class to Feature link. In Drupal 8, we try to avoid
// adding classes in preprocess, however there is currently not a clean way
// to do this in the Twig template.
if (!empty($variables['items'][0])) {
$variables['items'][0]['content']['#options']['attributes']['class'][] = 'button';
}
}
/**
* Prepares variables for `field--iu-feature-carousel-more-link.html.twig`.
*/
function iu_paragraphs_preprocess_field__iu_feature_carousel_more_link(&$variables) {
// Add the 'more top' class to Feature Carousel more link. In Drupal 8, we
// try to avoid adding classes in preprocess, however there is currently
// not a clean way to do this in the Twig template for link fields.
if (!empty($variables['items'][0])) {
$variables['items'][0]['content']['#options']['attributes']['class'][] = 'more top';
}
}
/**
* Prepares variables for `field--iu-feature-link.html.twig`.
*/
function iu_paragraphs_preprocess_field__iu_feature_link(&$variables) {
// Add the 'button' class to Feature link. In Drupal 8, we try to avoid
// adding classes in preprocess, however there is currently not a clean way
// to do this in the Twig template.
if (!empty($variables['items'][0])) {
$variables['items'][0]['content']['#options']['attributes']['class'][] = 'button';
}
}
/**
* Prepares variables for `field--iu-stat-link.html.twig`.
*/
function iu_paragraphs_preprocess_field__iu_stat_link(&$variables) {
// Add the 'button' class to Stat link. In Drupal 8, we try to avoid adding
// classes in preprocess, however there is currently not a clean way to do
// this in the Twig template.
if (!empty($variables['items'][0])) {
$variables['items'][0]['content']['#options']['attributes']['class'][] = 'button';
}
}
/**
* Prepares variables for `paragraph--iu-banner.html.twig`.
*/
function iu_paragraphs_preprocess_paragraph__iu_banner(&$variables) {
$p = $variables['paragraph'];
// Setup background image variable.
if (!empty($p->iu_banner_image) && $entity = $p->iu_banner_image->entity) {
$type = $entity->getEntityTypeId();
$bundle = $entity->bundle();
if ($type == 'media') {
if ($bundle == 'image') {
$entity = $entity->field_media_image->first()->entity;
}
elseif ($bundle == 'svg') {
$entity = $entity->field_media_svg->first()->entity;
}
}
$uri = $entity->getFileUri();
$small = ImageStyle::load('iu_one_half_standard');
$medium = ImageStyle::load('iu_full_wide');
$large = ImageStyle::load('iu_banner');
$variables['bgimage_small'] = file_url_transform_relative($small->buildUrl($uri));
$variables['bgimage_medium'] = file_url_transform_relative($medium->buildUrl($uri));
$variables['bgimage_large'] = file_url_transform_relative($large->buildUrl($uri));
if ($bundle == 'svg') {
$variables['is_svg'] = TRUE;
$url = file_create_url($entity->getFileUri());
$url = file_url_transform_relative($url);
$variables['bgimage_small'] =
$variables['bgimage_medium'] =
$variables['bgimage_large'] = $url;
}
}
// Setup background color class name variable.
_iu_paragraphs_preprocess_paragraph__iu_section_background_color($variables);
}
/**
* Prepares variables for `paragraph--iu-callout-pullquote.html.twig`.
*/
function iu_paragraphs_preprocess_paragraph__iu_callout_pullquote(&$variables) {
$p = $variables['paragraph'];
// The value of the is_pullquote boolean variable is based on whether the
// user specified any text in the "Attribution" field. If attribution is
// left empty, then this is not a pullquote.
$attribution = $p->iu_quote_attribution->getValue();
$variables['is_pullquote'] = !empty($attribution[0]['value']);
// Setup has_image boolean variable.
$image = $p->iu_callout_image->entity;
$variables['has_image'] = !empty($image);
}
/**
* Prepares variables for `paragraph--iu-feature.html.twig`.
*/
function iu_paragraphs_preprocess_paragraph__iu_feature(&$variables) {
$p = $variables['paragraph'];
// Setup has_image boolean variable.
$image = $p->iu_feature_image->entity;
$variables['has_image'] = !empty($image);
}
/**
* Prepares variables for `paragraph--iu-panel.html.twig`.
*/
function iu_paragraphs_preprocess_paragraph__iu_panel(&$variables) {
$p = $variables['paragraph'];
// Default to H3 if header level field value is undefined.
$header_level = $p->iu_panel_header_level->getValue();
$header_level = !empty($header_level[0]['value']) ? $header_level[0]['value'] : 'h3';
$variables['header_level'] = $header_level;
}
/**
* Prepares variables for `paragraph--iu-panel-interactive.html.twig`.
*/
function iu_paragraphs_preprocess_paragraph__iu_panel_interactive(&$variables) {
$p = $variables['paragraph'];
// Prep the header_level logic from iu_panel preprocess.
iu_paragraphs_preprocess_paragraph__iu_panel($variables);
// Prep the interactive link_uri variable.
$link_uri = $p->iu_panel_interactive_link->getValue();
if (!empty($link_uri[0]['uri'])) {
$variables['link_url'] = Url::fromUri($link_uri[0]['uri'])->toString();
} else {
$variables['link_url'] = '';
}
}
/**
* Prepares variables for `paragraph--iu-section.html.twig`.
*/
function iu_paragraphs_preprocess_paragraph__iu_section(&$variables) {
$p = $variables['paragraph'];
// Setup background image variable.
$bgimage = $p->iu_section_bgimage->entity;
$variables['bgimage'] = !empty($bgimage) ? file_url_transform_relative(file_create_url($bgimage->getFileUri())) : FALSE;
// Setup background color class name variable.
_iu_paragraphs_preprocess_paragraph__iu_section_background_color($variables);
// Setup parallax variable.
$parallax = $p->iu_section_bgparallax->getValue();
$variables['parallax'] = !empty($parallax[0]['value']);
// Setup horizontal rule variable for template usage.
$hr = $p->iu_section_horizontal_rule->getValue();
$variables['hr'] = !empty($hr[0]['value']);
// Setup height variable for template usage.
$height = $p->iu_section_height->getValue();
$variables['height'] = !empty($height[0]['value']) ? $height[0]['value'] : 'standard';
// Setup width variable for template usage.
$width = $p->iu_section_width->getValue();
$variables['width'] = !empty($width[0]['value']) ? $width[0]['value'] : 'standard';
// Setup additional formatting classes variable.
$styles = [];
foreach ($p->iu_section_styles->getValue() as $style) {
$styles[] = $style['value'];
}
$variables['styles'] = implode(' ', $styles);
}
/**
* Helper function for section_background_color field.
*
* Creates a `bgcolor` template variable containing IU Framework classnames
* for templates that leverage the Section Background color field.
*/
function _iu_paragraphs_preprocess_paragraph__iu_section_background_color(&$variables) {
$p = $variables['paragraph'];
// Setup background color class name variable.
if (!empty($p->iu_section_background_color)) {
$bgcolor = $p->iu_section_background_color->getValue();
$colorname = !empty($bgcolor[0]['value']) ? $bgcolor[0]['value'] : 'none';
$variables['bgcolor'] = 'bg-' . $colorname;
if (drupal_get_path('theme', 'iu')) {
// Load IU color palette and display section background color options as
// color blocks. (Please excuse the tight coupling with the IU theme, it
// couldn't be helped to satisfy the IU Styleguide options).
require_once drupal_get_path('theme', 'iu') . '/iu.theme';
$colors = _iu_secondary_color_palette_options();
// Determine the real colorname of the secondary color based on the IU
// Theme settings to help us determine whether to add the bg-dark class
// below.
if ($colorname == 'secondary') {
$colorname = theme_get_setting('secondary_color', 'iu');
if (empty($colorname)) {
$colorname = 'none';
}
}
// Add bg-dark class for certain colors.
if (!empty($colors[$colorname]) && $colors[$colorname]['is_dark'] == TRUE) {
$variables['bgcolor'] .= ' bg-dark';
}
}
}
}
/**
* Implements hook_field_widget_form_alter().
*/
function iu_paragraphs_field_widget_form_alter(&$element, FormStateInterface $form_state, $context) {
// Improve dropdown default option of '- None -'.
$field_definition = $context['items']->getFieldDefinition();
if ($field_definition->getName() == 'iu_section_width') {
$element['#options']['_none'] = t('Standard width');
}
if ($field_definition->getName() == 'iu_section_height') {
$element['#options']['_none'] = t('Standard height');
}
}
/**
* Implements hook_field_widget_info_alter().
*/
function iu_paragraphs_field_widget_info_alter(array &$info) {
// Allow simple Drupal text fields to use the textarea widget,
// for multi-line content entry.
// (See https://www.drupal.org/project/textarea_widget_for_text)
$info['text_textarea']['field_types'][] = 'text';
}
/**
* Implements hook_field_widget_WIDGET_TYPE_form_alter().
*
* Widget : Paragraphs Experimental : paragraphs.
*/
function iu_paragraphs_field_widget_paragraphs_form_alter(array &$element, FormStateInterface &$form_state, $context) {
// Only process paragraphs that are fully loaded, not collapsed.
if (empty($element['#paragraph_type']) || empty($element['subform']['#entity_type'])) {
return;
}
switch ($element['#paragraph_type']) {
case 'iu_banner':
_iu_paragraphs_iu_banner_alter($element, $form_state, $context);
_iu_paragraphs_iu_section_background_color_alter($element, $form_state, $context);
break;
case 'iu_panel':
_iu_paragraphs_iu_panel_alter($element, $form_state, $context);
break;
case 'iu_section':
_iu_paragraphs_iu_section_background_color_alter($element, $form_state, $context);
break;
default:
break;
}
}
/**
* Implements hook_field_widget_WIDGET_TYPE_form_alter().
*
* Widget type : Paragraphs EXPERIMENTAL : entity_reference_paragraphs.
*/
function iu_paragraphs_field_widget_entity_reference_paragraphs_form_alter(&$element, &$form_state, $context) {
iu_paragraphs_field_widget_paragraphs_form_alter($element, $form_state, $context);
}
/**
* Implements hook_field_widget_WIDGET_TYPE_form_alter().
*
* Widget : Paragraphs Classic Asymmetric : paragraphs_classic_asymmetric.
*/
function iu_paragraphs_field_widget_paragraphs_classic_asymmetric_form_alter(&$element, &$form_state, $context) {
iu_paragraphs_field_widget_paragraphs_form_alter($element, $form_state, $context);
}
/**
* Implements hook_field_widget_WIDGET_TYPE_form_alter().
*
* Widget : Paragraphs Previewer & Paragraphs Classic :
* entity_reference_paragraphs_previewer.
*/
function iu_paragraphs_field_widget_entity_reference_paragraphs_previewer_form_alter(&$element, &$form_state, $context) {
iu_paragraphs_field_widget_paragraphs_form_alter($element, $form_state, $context);
}
/**
* Implements hook_field_widget_WIDGET_TYPE_form_alter().
*
* Widget : Paragraphs Previewer & Paragraphs EXPERIMENTAL :
* paragraphs_previwer.
*/
function iu_paragraphs_field_widget_paragraphs_previwer_form_alter(&$element, &$form_state, $context) {
iu_paragraphs_field_widget_paragraphs_form_alter($element, $form_state, $context);
}
/**
* Processor for hook_field_widget_WIDGET_TYPE_form_alter for Panels chunks.
*/
function _iu_paragraphs_iu_panel_alter(array &$element, FormStateInterface &$form_state, $context) {
$subform = &$element['subform'];
// Remove the "- None -" option from the header level dropdown. Why Drupal
// doesn't let you do this through configuration is beyond me.
if ($subform['iu_panel_header_level']['widget']['#default_value'] != '_none' && !empty($subform['iu_panel_header_level']['widget']['#options']['_none'])) {
unset($subform['iu_panel_header_level']['widget']['#options']['_none']);
}
}
/**
* Processor for hook_field_widget_WIDGET_TYPE_form_alter for Sections.
*/
function _iu_paragraphs_iu_section_background_color_alter(array &$element, FormStateInterface $form_state, $context) {
$subform = &$element['subform'];
// Handle missing field.
if (empty($subform['iu_section_background_color'])) {
return;
}
// Handle missing theme.
if (!drupal_get_path('theme', 'iu')) {
return;
}
// Load secondary color from default theme.
// @todo load secondary color from sub-theme of node from
// current context.
$secondary_color = theme_get_setting('secondary_color', 'iu');
if (empty($secondary_color)) {
$secondary_color = 'none';
}
// Load IU color palette and display section background color options as
// color blocks. (Please excuse the tight coupling with the IU theme, it
// couldn't be helped to satisfy the IU Styleguide options).
require_once drupal_get_path('theme', 'iu') . '/iu.theme';
$colors = _iu_secondary_color_palette_options();
$widget = &$subform['iu_section_background_color']['widget'];
$widget['#attributes']['class'][] = 'color-block-radios';
foreach ($widget['#options'] as $key => $markup) {
$class = $key;
$name = $markup->__toString();
if (!empty($colors[$key])) {
$class = $colors[$key]['class'];
$name = $colors[$key]['name'];
$details = t('Primary color');
}
elseif ($class == 'secondary') {
$class = $colors[$secondary_color]['class'];
$name = $colors[$secondary_color]['name'];
$details = $markup->__toString();
}
elseif ($class == '_none') {
$class = 'bg-white-default';
$name = t('None');
$details = t('Default (white)');
}
$color_block = '
<div class="color-block ' . $class . '">
<div class="color-name">' . $name . '</div>
<div class="color-details">' . $details . '</div>
</div>
';
$widget['#options'][$key] = $color_block;
}
if ($widget['#default_value'] == NULL) {
$widget['#default_value'] = '_none';
}
$subform['#attached']['library'][] = 'iu/color-block';
}
/**
* Implements hook_field_widget_WIDGET_TYPE_form_alter().
*
* Alters the Banner paragraph type subform widget.
*/
function _iu_paragraphs_iu_banner_alter(array &$element, FormStateInterface $form_state, $context) {
$subform = &$element['subform'];
// Hide or show certain fields based on the Banner Type field. Note:
// This feature currently requires a patch from issue #2946856 to the
// Paragraphs module for compatibility with Paragraphs Experimental widget.
$parents = $subform['#parents'];
$parents_input_name = array_shift($parents);
$parents_input_name .= '[' . implode('][', $parents) . ']';
$type_field_selector = ':input[name="' . $parents_input_name . '[iu_banner_type]"]';
$types = [
'image' => [
'iu_banner_image',
],
'text' => [
'iu_banner_header',
'iu_banner_subhead',
'iu_banner_content_position',
'iu_banner_link',
'iu_section_background_color',
],
'video' => [
'iu_banner_mp4',
'iu_banner_webm',
'iu_banner_image',
'iu_banner_transcript',
],
'text_overlay' => [
'iu_banner_content_position',
'iu_banner_image',
'iu_banner_header',
'iu_banner_subhead',
'iu_banner_link',
],
'text_image_50_50' => [
'iu_banner_image',
'iu_banner_content_position',
'iu_banner_header',
'iu_banner_subhead',
'iu_banner_link',
'iu_section_background_color',
],
];
foreach ($types as $type => $fields) {
foreach ($fields as $field) {
$subform[$field]['#states']['visible'][] = [
$type_field_selector => [['value' => $type]],
];
}
}
}