-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathezcontent_paragraphs.module
178 lines (154 loc) · 5.07 KB
/
ezcontent_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
<?php
/**
* @file
* Module file for ezcontent_paragraphs.
*/
use Drupal\Component\Utility\Crypt;
use Drupal\Core\Url;
// Define constants for route parameters in the .module file.
define('EZCONTENT_PARAGRAPH_PARAM', 'paragraph');
define('EZCONTENT_VIEW_MODE_PARAM', 'view_mode');
/**
* Implements hook_preprocess_paragraph__paragraph().
*/
function ezcontent_paragraphs_preprocess_paragraph__paragraph(&$variables) {
// Attach default libraries.
ezcontent_paragraphs_attach_libraries($variables, [
'ezcontent_paragraphs/ezcontent_gallery_css',
'ezcontent_paragraphs/ezcontent_embed_css',
'ezcontent_paragraphs/ezcontent_referenced_card_css',
]);
// Conditionally attach libraries based on view mode.
if ($variables['view_mode'] === "hero_media") {
ezcontent_paragraphs_attach_libraries($variables, [
'ezcontent_paragraphs/ezcontent_paragraphs.hero_media_display',
]);
}
}
/**
* Implements hook_theme().
*/
function ezcontent_paragraphs_theme($existing, $type, $theme, $path) {
return [
'ezcontent_colorbox_view_mode_formatter' => [
'variables' => [
'item' => NULL,
'content' => NULL,
'modal' => NULL,
'item_attributes' => NULL,
'entity' => NULL,
'field_name' => NULL,
'settings' => NULL,
],
],
];
}
/**
* Implements hook_preprocess_HOOK().
*/
function ezcontent_paragraphs_preprocess_ezcontent_colorbox_view_mode_formatter(&$variables) {
static $gallery_token = NULL;
/** @var \Drupal\Core\Entity\EntityInterface $item */
$item = $variables['item'];
/** @var \Drupal\Core\Entity\EntityInterface $entity */
$entity = $variables['entity'];
$field_name = $variables['field_name'];
$settings = $variables['settings'];
$config = \Drupal::config('colorbox.settings');
// Build the gallery id.
$entity_type = $entity->getEntityTypeId();
$entity_bundle = $entity->bundle();
$id = $entity->id();
$entity_id = !empty($id) ? $entity_bundle . '-' . $id : 'entity-id';
switch ($settings['colorbox_gallery']) {
case 'post':
$gallery_id = 'gallery-' . $entity_id;
break;
case 'page':
$gallery_id = 'gallery-all';
break;
case 'field_post':
$gallery_id = 'gallery-' . $entity_id . '-' . $field_name;
break;
case 'field_page':
$gallery_id = 'gallery-' . $field_name;
break;
case 'custom':
$token_service = \Drupal::token();
$gallery_id = $token_service->replace($settings['colorbox_gallery_custom'],
[$entity_type => $entity, 'entity' => $item], ['clear' => TRUE]);
break;
default:
$gallery_id = '';
}
// If gallery id is not empty add unique per-request token to avoid
// images being added manually to galleries.
if (!empty($gallery_id) && $config->get('advanced.unique_token')) {
// Check if gallery token has already been set, we need to reuse
// the token for the whole request.
if ($gallery_token === NULL) {
// We use a short token since randomness is not critical.
$gallery_token = Crypt::randomBytesBase64(8);
}
$gallery_id = $gallery_id . '-' . $gallery_token;
}
$variables['url'] = NULL;
$item_type = $item->getEntityTypeId();
if ($item_type === 'paragraph') {
$variables['url'] = Url::fromRoute('ezcontent_paragraphss.display')
->setRouteParameters([
EZCONTENT_PARAGRAPH_PARAM => $item->id(),
EZCONTENT_VIEW_MODE_PARAM => $variables['modal']['#view_mode'],
]);
}
if ($entity->hasField('field_title') && !$entity->get('field_title')->isEmpty()) {
$variables['attributes']['title'] = $entity->get('field_title')->value;
}
else {
$variables['attributes']['title'] = $entity->getParentEntity()->getTitle();
}
$variables['attributes']['data-colorbox-gallery'] = $gallery_id;
$variables['attributes']['class'] = ['colorbox'];
}
/**
* Implements hook_preprocess_views_view().
*/
function ezcontent_paragraphs_preprocess_views_view(&$variables) {
if ($variables['view']->id() === 'asset_browser') {
$variables['view_array']['#attached']['library'][] = 'media_entity_browser/view';
}
}
/**
* Implements hook_preprocess_HOOK().
*/
function ezcontent_paragraphs_preprocess_paragraph__card(&$variables) {
$variables['#attached']['library'][] = 'ezcontent_paragraphs/ezcontent_card_css';
}
/**
* @file
* Module file for ezcontent_demo.
*/
/**
* Implements hook_preprocess_HOOK().
*/
function ezcontent_paragraphs_preprocess_block(&$vars) {
if (!empty($vars['elements']['content']['#block_content'])) {
$bundle = $vars['elements']['content']['#block_content']->bundle();
if ($bundle === 'paragraph_block') {
$vars['#attached']['library'][] = 'ezcontent_paragraphs/ezcontent_embed_css';
}
}
}
/**
* Helper function to attach libraries to paragraphs.
*
* @param array &$variables
* The variables array where libraries will be attached.
* @param array $libraries
* An array of library names to attach.
*/
function ezcontent_paragraphs_attach_libraries(&$variables, array $libraries) {
foreach ($libraries as $library) {
$variables['#attached']['library'][] = $library;
}
}