This repository has been archived by the owner on Jun 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparagraphs_types.module
286 lines (247 loc) · 8.45 KB
/
paragraphs_types.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
<?php
/**
* @file
* Contains paragraphs_types.module..
*/
use Drupal\Core\Routing\RouteMatchInterface;
use \Drupal\file\Entity\File;
use Drupal\file\FileInterface;
use Drupal\image\Entity\ImageStyle;
use Drupal\Core\Render\Element;
use Drupal\paragraphs\Entity\Paragraph;
/**
* Implements hook_help().
*/
function paragraphs_types_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the paragraphs_types module.
case 'help.page.paragraphs_types':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('This is a collection of paragraphs types for Drupal 8.') . '</p>';
$output .= '<p>' . t('Go to <a href="/admin/structure/paragraphs_type"><code>admin/structure/paragraphs_type</code></a> to see all paragraphs.') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_theme().
*
* Call the base hook from Paragraphs to allow preprocess hooks to work.
*/
function paragraphs_types_theme() {
return array(
'paragraph__text' => array(
'render element' => 'element',
'template' => 'paragraph--text',
'base hook' => 'paragraph',
),
'paragraph__image_featured_text' => array(
'render element' => 'element',
'template' => 'paragraph--image-featured-text',
'base hook' => 'paragraph',
),
'paragraph__video' => array(
'render element' => 'element',
'template' => 'paragraph--video',
'base hook' => 'paragraph',
),
'paragraph__gallery' => array(
'render element' => 'element',
'template' => 'paragraph--gallery',
'base hook' => 'paragraph',
),
'paragraph__image' => array(
'render element' => 'element',
'template' => 'paragraph--image',
'base hook' => 'paragraph',
),
'paragraph__blockquote' => array(
'render element' => 'element',
'template' => 'paragraph--blockquote',
'base hook' => 'paragraph',
),
'field__field_blockquote_footer' => array(
'render element' => 'element',
'template' => 'fields/field--field-blockquote-footer',
'base hook' => 'field',
),
'field__field_figcaption' => array(
'render element' => 'element',
'template' => 'fields/field--field-figcaption',
'base hook' => 'field',
),
);
}
/**
* Implements hook_page_attachments().
*
* Attach CSS/JS libs, if config says so.
*/
function paragraphs_types_page_attachments(array &$page) {
$config = \Drupal::config('paragraphs_types.settings');
if ($config->get('load_gallery_assets')) {
$page['#attached']['library'][] = 'paragraphs_types/gallery';
$page['#attached']['library'][] = 'paragraphs_types/photoswipe';
}
}
/**
* Implements hook_preprocess_HOOK() for paragraph--image-featured-text.html.twig.
*
* Convert field render array to single value variable.
*/
function paragraphs_types_preprocess_paragraph__image_featured_text(&$variables
) {
// field is required, so no empty check needed
/** @var Paragraph $paragraph */
$paragraph = $variables['paragraph'];
$variables['image_text_layout'] = $paragraph->get('field_image_text_layout')->value;
}
/**
* Implements hook_preprocess_HOOK() for paragraph--video.html.twig.
*/
function paragraphs_types_preprocess_paragraph__video(&$variables) {
$videoUrl = $variables['elements']['field_url'][0]['#url']->getUri();
$parsedUrl = parse_url($videoUrl);
$videoHost = NULL;
$videoID = NULL;
// YOUTUBE
if (strpos($parsedUrl['host'], 'youtube') !== FALSE) {
$videoHost = 'youtube';
parse_str($parsedUrl['query'], $query);
if (array_key_exists('v', $query)) {
$videoID = $query['v'];
}
}
// VIMEO
if (strpos($parsedUrl['host'], 'vimeo') !== FALSE) {
$videoHost = 'vimeo';
$videoID = substr(parse_url($videoUrl, PHP_URL_PATH), 1);
}
$variables['video_id'] = $videoID;
$variables['video_host'] = $videoHost;
}
/**
* Implements hook_preprocess_HOOK() for paragraph--gallery.html.twig.
*/
function paragraphs_types_preprocess_paragraph__gallery(&$variables) {
/** @var Paragraph $paragraph */
$paragraph = $variables['paragraph'];
if (!$paragraph->hasField('field_images')) {
return;
}
$images = $paragraph->get('field_images');
$variables['images'] = [];
$variables['#attached']['drupalSettings']['ps_images'] = [];
foreach ($images as $item) {
/* @var $file FileInterface */
$file = File::load($item->target_id);
if($file == null) continue;
$image = [
'url' => file_url_transform_relative(file_create_url($file->getFileUri())),
'width' => $item->width,
'height' => $item->height,
'sizes' => _load_image_sizes($file, ['medium', 'large'])
];
$variables['images'][] = $image;
$variables['paragraph_id'] = $paragraph->id();
$variables['#attached']['drupalSettings']['ps_images'][$paragraph->id()][] = [
'src' => _load_image_sizes($file)['large'],
'w' => 0,
'h' => 0
];
}
}
/**
* Implements hook_preprocess_HOOK() for paragraph--image.html.twig.
*/
function paragraphs_types_preprocess_paragraph__image(&$variables) {
/** @var Paragraph $paragraph */
$paragraph = $variables['paragraph'];
if (!$paragraph->hasField('field_image')) {
return;
}
$imageField = $paragraph->get('field_image')->first();
if(!$imageField instanceof ImageItem || (int) $imageField->target_id <= 0 ) {
return FALSE;
}
/* @var $file FileInterface */
$file = File::load($imageField->target_id);
if (!$file) {
return FALSE;
}
$caption = $paragraph->get('field_figcaption')->value;
$variables['image'] = [
'url' => file_create_url($file->getFileUri()),
'width' => $imageField->width,
'height' => $imageField->height,
'caption' => $caption,
'sizes' => _load_image_sizes($file)
];
}
/**
* @param FileInterface $image
* @return array
*/
function _load_image_sizes(FileInterface $image, array $sizes = []) {
$fileUri = $image->getFileUri();
$urls = [];
if (empty($sizes)) {
$imageStyles = ImageStyle::loadMultiple();
}
else {
$imageStyles = ImageStyle::loadMultiple($sizes);
}
foreach ($imageStyles as $image_style) {
$urls[$image_style->getName()] = file_url_transform_relative($image_style->buildUrl($fileUri));
}
return $urls;
}
function paragraphs_types_uninstall() {
$files = [
'paragraphs.paragraphs_type.text',
'paragraphs.paragraphs_type.blockquote',
'paragraphs.paragraphs_type.video',
'paragraphs.paragraphs_type.image',
'paragraphs.paragraphs_type.gallery',
'paragraphs.paragraphs_type.image_featured_text',
'field.field.paragraph.text.field_text',
'field.field.paragraph.quote.field_body',
'field.field.paragraph.video.field_url',
'field.field.paragraph.quote.field_name',
'field.field.paragraph.image.field_image',
'field.field.paragraph.image.field_figcaption',
'field.field.paragraph.gallery.field_images',
'field.field.paragraph.gallery.field_gallery_headline',
'field.field.paragraph.quote.field_blockquote_footer',
'field.field.paragraph.image_featured_text.field_figcaption',
'field.field.paragraph.image_featured_text.field_image',
'field.field.paragraph.image_featured_text.field_text',
'field.field.paragraph.image_featured_text.field_image_text_layout',
'field.storage.paragraph.field_quote_author',
'field.storage.paragraph.field_images',
'field.storage.paragraph.field_figcaption',
'field.storage.paragraph.field_image',
'field.storage.paragraph.field_name',
'field.storage.paragraph.field_body',
'field.storage.paragraph.field_text',
'field.storage.paragraph.field_url',
'field.storage.paragraph.field_image_text_layout',
'field.storage.paragraph.field_gallery_headline',
'core.entity_view_display.paragraph.gallery.default',
'core.entity_view_display.paragraph.image.default',
'core.entity_view_display.paragraph.blockquote.default',
'core.entity_view_display.paragraph.image_featured_text.default',
'core.entity_view_display.paragraph.text.default',
'core.entity_view_display.paragraph.video.default',
'core.entity_form_display.paragraph.gallery.default',
'core.entity_form_display.paragraph.image.default',
'core.entity_form_display.paragraph.quote.default',
'core.entity_form_display.paragraph.image_featured_text.default',
'core.entity_form_display.paragraph.text.default',
'core.entity_form_display.paragraph.video.default',
];
foreach ($files as $file) {
//\Drupal::configFactory()->getEditable($file)->delete();
}
}