/
d_p_tiles.module
103 lines (88 loc) · 2.88 KB
/
d_p_tiles.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
<?php
/**
* @file
* File containing d_p_tiles module.
*/
use Drupal\Core\Template\Attribute;
/**
* Implements hook_theme().
*/
function d_p_tiles_theme($existing, $type, $theme, $path) {
return [
'paragraph__d_p_tiles' => [
'base hook' => 'paragraph',
],
'field__paragraph__field_d_media_image__d_p_tiles' => [
'base hook' => 'field',
],
];
}
/**
* Implements hook_preprocess_paragraph().
*/
function d_p_tiles_preprocess_paragraph(&$variables) {
$paragraph = $variables['paragraph'];
$bundle = $paragraph->bundle();
switch ($bundle) {
case 'd_p_tiles':
// Attach libraries.
$variables['#attached']['library'][] = 'd_p_tiles/masonry';
$variables['#attached']['library'][] = 'd_p_tiles/d_p_tiles';
break;
}
}
/**
* Implements hook_preprocess_field().
*/
function d_p_tiles_preprocess_field(&$variables) {
if ($variables['element']['#entity_type'] == 'paragraph') {
if ($variables['element']['#bundle'] == 'd_p_tiles') {
switch ($variables['field_name']) {
case 'field_d_main_title':
case 'field_d_long_text':
// Add container class to title and description.
$variables['attributes']['class'][] = 'container';
break;
case 'field_d_media_image':
$variables['wrapper_attributes'] = new Attribute();
break;
}
}
}
}
/**
* Implements hook_preprocess_colorbox_formatter().
*/
function d_p_tiles_preprocess_colorbox_formatter(&$variables) {
// Add standard size to each field.
$variables['attributes']['class'][] = 'd-tiles-item';
// Detect custom sizes in ALTs.
$fd = $variables['item']->getFieldDefinition();
/** @var \Drupal\d_p\Service\ParentParagraphService $parentEntityService */
$parentEntityService = Drupal::service('d_p.entity_parent_paragraph');
$paragraphFieldId = $parentEntityService->getParentParagraphFieldId($variables['entity']);
if ($fd->id() == 'paragraph.d_p_tiles.field_d_media_image'
|| ($paragraphFieldId && $paragraphFieldId == 'paragraph.d_p_tiles.field_d_media_image')) {
$alt = $variables['image']['#alt'];
// In 1x1 just remove the text.
if (strstr($alt, '1x1')) {
$alt = trim(str_replace('1x1', '', $alt));
}
// In 2x2 change class and image style.
if (strstr($alt, '2x2')) {
$alt = trim(str_replace('2x2', '', $alt));
$variables['image']['#style_name'] = 'tiles_thumbnail_2x2';
$variables['attributes']['class'][] = 'd-tiles-item-2x2';
}
$variables['image']['#alt'] = $alt;
$variables['attributes']['title'] = '';
$variables['attributes']['data-cbox-img-attrs'] = json_encode(['alt' => $alt]);
$variables['attributes']['data-cbox-title'] = $alt;
}
}
/**
* Implements hook_d_p_centered_ckeditor_widget_paragraphs().
*/
function d_p_tiles_d_p_centered_ckeditor_widget_paragraphs(array &$paragraph_types) {
$paragraph_types[] = 'd_p_tiles';
}