This repository has been archived by the owner on Nov 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
infinite.theme
585 lines (520 loc) · 21.3 KB
/
infinite.theme
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
<?php
use Drupal\Core\Template\Attribute;
use Drupal\block\Entity\Block;
use Drupal\Core\TypedData\Exception\MissingDataException;
use Drupal\infinite_media\MediaHelper;
use Drupal\taxonomy\TermInterface;
use Drupal\views\Views;
use Drupal\user\Entity\User;
use Drupal\media_entity\MediaInterface;
use Psy\Util\Json;
function infinite_preprocess(&$variables) {
$variables['environment'] = ['systemType' => isset($_ENV['AH_SITE_ENVIRONMENT']) ? $_ENV['AH_SITE_ENVIRONMENT'] : 'local'];
}
function infinite_theme_suggestions_paragraph_alter(array &$suggestions, array $variables, $hook) {
/* @var $paragraph \Drupal\paragraphs\Entity\Paragraph */
$paragraph = $variables['elements']['#paragraph'];
// TODO: find a better way to extract media bundle from paragraph item.
if (!empty($variables['elements']['field_media']['0']['#media'])) {
/* @var $media \Drupal\media_entity\Entity\Media */
$media = $variables['elements']['field_media']['0']['#media'];
$suggestions[] = $hook . '__' . $paragraph->bundle() . '__' . $media->getEntityTypeId() . '_' . $media->bundle();
$suggestions[] = $hook . '__' . $paragraph->bundle() . '__' . $media->getEntityTypeId() . '_' . $media->bundle() . '__default'; // TODO: Fix static view mode.
}
/* @var $parent \Drupal\Core\Entity\EntityInterface */
$parent = $paragraph->getParentEntity();
if ($parent) {
$suggestions[] = $hook . '__' . $paragraph->bundle() . '__' . $parent->getEntityTypeId();
$suggestions[] = $hook . '__' . $paragraph->bundle() . '__' . $parent->getEntityTypeId() . '__default';
$suggestions[] = $hook . '__' . $paragraph->bundle() . '__' . $parent->getEntityTypeId() . '__' . $parent->bundle();
$suggestions[] = $hook . '__' . $paragraph->bundle() . '__' . $parent->getEntityTypeId() . '__' . $parent->bundle() . '__default';
}
}
/**
* Implements hook_theme_suggestions_product_alter().
*/
function infinite_theme_suggestions_product_alter(array &$suggestions, array $variables, $hook) {
if (!empty($variables['elements']['#view_mode'])) {
$suggestions[] = $hook . '__' . $variables['elements']['#view_mode'];
}
}
function infinite_theme_suggestions_user_alter(array &$suggestions, array $variables, $hook) {
if (!empty($variables['elements']['#view_mode'])) {
$suggestions[] = $hook . '__' . $variables['elements']['#view_mode'];
}
}
function infinite_preprocess_page(&$variables) {
$variables['theme_infinite_path'] = '/' . drupal_get_path('theme', 'infinite');
$variables['site_name'] = \Drupal::config('system.site')->get('name');
}
function infinite_preprocess_block(&$variables) {
$variables['front_page'] = \Drupal::url('<front>');
$variables['logo'] = theme_get_setting('logo.url');
}
function infinite_preprocess_block__outbrain(&$variables) {
/* @var Drupal\block\Entity\Block $block */
$block = Block::load('advertisingslot_fba_article');
if (is_object($block)) {
$advertisingslot_fba_article = \Drupal::entityManager()
->getViewBuilder('block')
->view($block, 'default');
$variables['elements']['content']['advertisingslot_fba_article'] = $advertisingslot_fba_article;
$variables['content']['advertisingslot_fba_article'] = $advertisingslot_fba_article;
}
}
/**
* Implements template_preprocess_media().
*
* Provide extra variables for media full view mode of nexx videos.
*
* TODO: Refactor when update to core media.
*/
function infinite_preprocess_media(array &$variables) {
/* @var $media MediaInterface */
if ($variables['elements']['#view_mode'] == 'full' && $media = $variables['elements']['#media']) {
if ($media->bundle() == 'nexx_video') {
$variables['mid'] = $media->id();
$variables['channel_tid'] = 'all';
if (!$media->get('field_channel')->isEmpty()) {
$variables['channel_tid'] = (int) $media->get('field_channel')->target_id;
}
}
}
}
function infinite_preprocess_region(&$variables) {
$variables ['front_page'] = \Drupal::url('<front>');
$variables ['logo'] = theme_get_setting('logo.url');
}
function infinite_preprocess_html(&$variables) {
$current_path = \Drupal::service('path.current')->getPath();
$path_alias = \Drupal::service('path.alias_manager')->getAliasByPath($current_path);
$path_alias = ltrim($path_alias, '/');
$variables['attributes']['class'][] = \Drupal\Component\Utility\Html::cleanCssIdentifier($path_alias);
if (\Drupal::moduleHandler()->moduleExists('metatag')) {
/**
* TODO: Does metatag module provide some simpler api call to get metatags?
*/
$metatag_manager = \Drupal::service('metatag.manager');
// First, get defaults.
$metatags = metatag_get_default_tags();
if (!$metatags) {
return;
}
$entity = metatag_get_route_entity();
if (!empty($entity) && $entity instanceof ContentEntityInterface) {
$metatags = array_merge($metatags, $metatag_manager->tagsFromEntity($entity));
}
if (isset($metatags['fb_app_id'])) {
$variables['fb_vars'] = ['fb_app_id' => $metatags['fb_app_id']];
}
$variables['gtm_vars'] = ['gtm_id' => theme_get_setting('gtm_id')];
}
}
function infinite_preprocess_node(&$variables) {
/* @var Drupal\node\Entity\Node $node */
$node = $variables['node'];
$variables['nid'] = $node->id();
// Override TWIG url with absolute alias URL.
// Variable 'path' is used for lazy loading history API.
$variables['path'] = $variables['url'];
$variables['url'] = $node->url('canonical', ['absolute' => TRUE]);
// share buttons received from theme settings
$variables['facebook_share_button'] = theme_get_setting('facebook_share_button');
$variables['whatsapp_share_button'] = theme_get_setting('whatsapp_share_button');
$variables['pinterest_share_button'] = theme_get_setting('pinterest_share_button');
$variables['twitter_share_button'] = theme_get_setting('twitter_share_button');
$variables['twitter_share_via'] = theme_get_setting('twitter_share_via');
$variables['email_share_button'] = theme_get_setting('email_share_button');
$variables['whatsapp_share_text'] = theme_get_setting('whatsapp_share_text');
$variables['email_share_text'] = theme_get_setting('email_share_text');
$variables['email_subject'] = theme_get_setting('email_subject');
$bundle = $node->bundle();
if ($bundle == 'article' || $bundle == 'page') {
// todo: switch later to publish date.
$variables['datetime'] = \Drupal::service('date.formatter')
->format($node->created->value, 'html_datetime');
$variables['timestamp'] = $node->created->value;
$variables['share_img_url'] = MediaHelper::getImageUrlFromMediaReference(
$node,
'field_teaser_media',
theme_get_setting('share_image_style')
);
// Add 'show_contextual_links' variable.
/* @var User $user */
$user = Drupal::currentUser();
if ($user->hasPermission('access contextual links')) {
$variables['show_contextual_links'] = TRUE;
}
}
if ($bundle == 'article') {
$field_seo_title = !empty($node->field_seo_title->getValue()[0]) ? $node->field_seo_title->getValue()[0]['value'] : '';
// Replace double quotes.
// @toDo check, where we have this case!
$variables['node_seo_title'] = str_replace('"', '', $field_seo_title);
// Clean up classes array - all classes will be added in
$variables['attributes']['class'] = array();
// Instantiate 'wrapper_attributes' attribute object to avoid collisions
// with actual node attributes.
if ($variables['view_mode'] === 'full' || $variables['view_mode'] === 'lazyloading') {
$variables['wrapper_attributes'] = new Attribute();
}
$variables = _infinite_get_icon($node, $variables);
_infinite_add_amazon_id($variables);
}
if($bundle == 'ecommerce_slider') {
$data_slider = [];
$start_position = $node->get('field_slider_start_position')->value;
if(is_numeric($start_position)) {
$data_slider['initialSlide'] = $node->get('field_slider_start_position')->value - 1;
}
else {
$data_slider['initialSlide'] = 0;
}
$variables['data_slider'] = Json::encode($data_slider, JSON_NUMERIC_CHECK);
_infinite_add_amazon_id($variables);
}
}
/**
* Implements hook_preprocess_views().
*/
function infinite_preprocess_views_view(&$variables) {
// todo: replace with configurable sidebar views UI plugin.
/* @var Drupal\views\ViewExecutable $view */
$view = $variables['view'];
// Render title for all other views, because we aren't using pagetitle block.
// todo: check how to add pagetitle block as default?
if(isset($variables['title']) && is_array($variables['title'])){
$variables['title']['#markup'] = $view->getTitle();
}
else {
$variables['title'] = $view->getTitle();
}
}
/**
* Implements hook_preprocess_product().
*/
function infinite_preprocess_advertising_product(&$variables) {
/** @var \Drupal\advertising_products\Entity\AdvertisingProduct $product */
$product = $variables['elements']['#advertising_product'];
// Set the data attributes for tracking purposes
$data_attributes = new Drupal\Core\Template\Attribute;
$data_attributes->setAttribute('data-title', $product->product_name->value)
->setAttribute('data-shop', $product->product_shop->value)
->setAttribute('data-brand', $product->product_brand->value)
->setAttribute('data-price', $product->product_price->value)
->setAttribute('data-currency', $product->product_currency->value)
->setAttribute('data-uuid', $product->uuid->value)
->setAttribute('data-product-id', $product->product_id->value)
->setAttribute('data-sold-out', $product->product_sold_out->value);
// tracking_url
$tracking_url = $product->product_url->uri;
// This is needed only for tracdelight products
if ($product->getType() == 'advertising_product_tracdelight') {
$sub_id = 0;
if (Drupal::request()->attributes->has('node')) {
$node = Drupal::request()->attributes->get('node');
$sub_id = 'node-' . $node->id();
} elseif (Drupal::request()->attributes->has('taxonomy_term')) {
$term = Drupal::request()->attributes->get('taxonomy_term');
$sub_id = 'term-' . $term->id();
}
$tracking_url .= '&subid=' . $product->product_id->value;
}
$data_attributes->setAttribute('data-external-url', $tracking_url);
$variables['tracking_url'] = $tracking_url;
// original_price
$original_price = $product->product_original_price->value;
// price
$price = $product->product_price->value;
// provider
$provider = explode("_", $product->product_provider->value)[0];
$data_attributes->setAttribute('data-provider', $provider);
$variables['provider'] = $provider;
if ('tipser' === $provider) {
$data_attributes->setAttribute('data-tipser-url', $variables['tracking_url']);
$data_attributes->removeAttribute('data-external-url');
}
// sold_out
$sold_out = $product->product_sold_out->value;
if ($price < 0 || $sold_out) {
$variables['sold_out'] = TRUE;
}
if(!isset($variables['sold_out']) || !$variables['sold_out']) {
if($price < $original_price){
$variables['show_strikethrough_price'] = TRUE;
$variables['percentage_saving'] = round(($price - $original_price) / $original_price * 100);
}
if ('tipser' === $provider) {
// hack so we can track product category even though it uses another field
// for the category than e.g. Amazon products
$tipserFieldCategory = $product->get('field_category');
if ($tipserFieldCategory->count()) {
/** @var \Drupal\taxonomy\Entity\Term $term */
$term = \Drupal\taxonomy\Entity\Term::load($tipserFieldCategory->first()->getValue()['target_id']);
if ($term) {
$variables['tipser_product_category'] = $term->getName();
}
}
$data_attributes->setAttribute('data-shop', $product->product_shop->value);
}
}
$variables['data_attributes'] = $data_attributes;
}
/**
* Implements template_preprocess_paragraph().
*/
function infinite_preprocess_paragraph(&$variables) {
if (!empty($variables['elements']['field_media']['0']['#media'])) {
$media = $variables['elements']['field_media']['0']['#media'];
$variables['media_bundle'] = $media->bundle();
}
if ('spotlight' === $variables['paragraph']->bundle()) {
$entity = \Drupal::routeMatch()->getParameter('taxonomy_term');
if ($entity instanceof \Drupal\taxonomy\Entity\Term) {
$variables = infinite_inject_pinterest_image($variables, $entity);
}
}
// Initialise amazon id.
_infinite_add_amazon_id($variables);
// Overwrite the default amazon associates_id if set.
if ('products' === $variables['paragraph']->bundle()
|| 'advertising_products_paragraph' === $variables['paragraph']->bundle()
|| 'advertising_products_embed' === $variables['paragraph']->bundle()
|| 'ecommerce_slider_paragraph' === $variables['paragraph']->bundle()) {
$node_entity = \Drupal::routeMatch()->getParameter('node');
if (
$node_entity instanceof \Drupal\node\Entity\Node
&& $node_entity->hasField('field_amazon_tag')
&& strlen($node_entity->get('field_amazon_tag')->value) > 3
) {
$variables['#attached']['drupalSettings']['amazon']['associatesIdDefault'] = $variables['amazon_tag'];
$variables['amazon_tag'] = $node_entity->get('field_amazon_tag')->value;
$variables['#attached']['drupalSettings']['amazon']['associatesIdOverriden'] = $variables['amazon_tag'];
}
}
}
function infinite_preprocess_field(&$variables) {
// INREL-2807 Set all links in promotion to nofollow
if($variables['element']['#field_name'] === 'field_url' && $variables['element']['#bundle'] === 'promotion') {
/** @var \Drupal\link\Plugin\Field\FieldType\LinkItem $item */
foreach ($variables['items'] as &$item) {
$item['content']['#options']['attributes']['rel'] = 'nofollow';
}
}
if ($variables['element']['#field_name'] == 'field_tags') {
$items = &$variables['items'];
foreach ($items as $delta => &$item) {
$term = $item['content']['#options']['entity'];
$item['status'] = $term->get('status')->value;
}
}
}
function infinite_page_attachments_alter(array &$attachments) {
// foreach($attachments['#attached']['html_head'] as $key => $attachment) {
// if(isset($attachment[1]) && $attachment[1] == 'breakpoint_settings_loader') {
// unset($attachments['#attached']['html_head'][$key]);
// }
// }
}
function infinite_preprocess_container(&$variables) {
if (isset($variables['element']['#parent'])) {
if (isset($variables['element']['#parent']['#node'])) {
$entity = $variables['element']['#parent']['#node'];
}
elseif (isset($variables['element']['#parent']['#taxonomy_term'])) {
$entity = $variables['element']['#parent']['#taxonomy_term'];
}
elseif (isset($variables['element']['#parent']['#user'])) {
$entity = $variables['element']['#parent']['#user'];
}
}
if (isset($entity)) {
$variables = infinite_inject_pinterest_image($variables, $entity);
// use seo description for pinterest share description
if (method_exists($entity, 'bundle') && method_exists($entity, 'get') && method_exists($entity, 'hasField')) {
switch ($entity->bundle()) {
// field_teaser_text is misused as seo description on these content types
// this should be fixed in https://jira.burda.com/browse/INREL-4095
case 'article':
case 'magazine_products':
if ($entity->hasField('field_teaser_text')) {
$variables['pinterest_share_description'] = $entity->get('field_teaser_text')->value;
}
break;
default:
if ($entity->hasField('field_seo_description')) {
$variables['pinterest_share_description'] = $entity->get('field_seo_description')->value;
}
break;
}
}
// Set progress navigation.
if ($entity instanceof TermInterface) {
$variables['progress_navigation'] = [];
if (!$entity->field_paragraphs->isEmpty()) {
foreach ($entity->field_paragraphs->referencedEntities() as $index => $paragraph) {
if (isset($paragraph->getFields()['field_progress_navigation']) &&
!$paragraph->getFields()['field_progress_navigation']->isEmpty()) {
array_push(
$variables['progress_navigation'],
$paragraph->getFields()['field_progress_navigation']->value
);
}
}
} else {
foreach ($variables['element']['#children'][0]['#children'] as $index => $child) {
if (isset($child['#paragraph'])
&& !$child['#paragraph']->get('field_progress_navigation')->isEmpty()
) {
array_push(
$variables['progress_navigation'],
$child['#paragraph']->get('field_progress_navigation')->value
);
}
}
}
}
}
}
/**
* Implements hook_preprocess_responsive_image().
*/
function infinite_preprocess_responsive_image(&$variables) {
$variables['noscript_img_element'] = FALSE;
if (isset($variables['img_element']) && isset($variables['sources'][0]['data-srcset'])) {
// make a copy
$variables['noscript_img_element'] = $variables['img_element'];
// throw away stuff we don't need.
unset(
$variables['noscript_img_element']['#attached'],
$variables['noscript_img_element']['drupalSettings'],
$variables['noscript_img_element']['#attributes']['class'],
$variables['noscript_img_element']['#attributes']['data-src']
);
// set the uri to the first element of sources, it is the largest image.
$variables['noscript_img_element']['#uri'] = $variables['sources'][0]['data-srcset']->value();
}
}
/**
* Use dedicated pinterest image for sharing.
*
* @param $variables
* @param $entity
* @return mixed
*/
function infinite_inject_pinterest_image($variables, $entity)
{
$variables['pinterest_share_img_url'] = MediaHelper::getImageUrlFromMediaReference(
$entity,
'field_pinterest_image'
);
// as fallback, if no dedicated pinterest image is set
// use a 2:3 format of share image
if (empty($variables['pinterest_share_img_url'])) {
$variables['pinterest_share_img_url'] = MediaHelper::getImageUrlFromMediaReference(
$entity,
'field_teaser_media',
'pinterest_fallback_teaser_image'
);
}
return $variables;
}
/**
* Check which icon to display.
*
* @param $node
* @param $variables
* @return mixed
*/
function _infinite_get_icon($node, $variables) {
$variables['teaser_icon'] = FALSE;
if (!empty($paragraphs = $node->field_paragraphs->referencedEntities())) {
foreach ($paragraphs as $paragraph) {
switch ($paragraph->bundle()) {
case 'productdb_product':
case 'advertising_products_paragraph':
case 'advertising_products_embed':
case 'ecommerce_slider_paragraph':
case 'ecommerce_look':
// shopping overrides everything else
$variables['teaser_icon'] = 'shop-it';
break 2;
case 'gallery':
$variables['teaser_icon']['image'] = 'image';
break;
case 'media':
if (!empty($medias = $paragraph->field_media->referencedEntities())) {
foreach ($medias as $media) {
switch ($media->bundle()) {
case 'video':
case 'nexx_video':
$variables['teaser_icon']['video'] = 'video';
break;
}
}
}
break;
case 'video':
$variables['teaser_icon']['video'] = 'video';
break;
}
}
if (is_array($variables['teaser_icon'])) {
if (isset($variables['teaser_icon']['video'])) {
$variables['teaser_icon'] = 'video';
}
else if (isset($variables['teaser_icon']['image'])) {
$variables['teaser_icon'] = 'image';
}
}
}
return $variables;
}
/**
* Set infinite preprocess variables for User
*
* @param $variables
*
* @throws \Drupal\Core\TypedData\Exception\MissingDataException
*/
function infinite_preprocess_user(&$variables)
{
try {
$variables['has_spotlight_paragraph'] = infinite_get_user_paragraph_bundle($variables['elements']['#user']) === 'spotlight';
} catch (InvalidArgumentException $ex) {
$variables['has_spotlight_paragraph'] = false;
} catch (MissingDataException $ex) {
$variables['has_spotlight_paragraph'] = false;
}
}
/**
* Get the paragraph bundle
*
* @param User $user
* @param int $paragraphPosition
*
* @return string
*
* @throws InvalidArgumentException
* @throws \Drupal\Core\TypedData\Exception\MissingDataException
*/
function infinite_get_user_paragraph_bundle(User $user, int $paragraphPosition = 0): string
{
/** @var \Drupal\entity_reference_revisions\EntityReferenceRevisionsFieldItemList $fieldParagraphs */
$fieldParagraphs = $user->getFields(true)['field_paragraphs'];
/** @var \Drupal\entity_reference_revisions\Plugin\Field\FieldType\EntityReferenceRevisionsItem $currentParagraph */
$currentParagraph = $fieldParagraphs->get($paragraphPosition);
if (null === $currentParagraph) {
throw new InvalidArgumentException("Paragraph at position '$paragraphPosition' is not present");
}
return $currentParagraph->get('entity')->getTarget()->getValue()->bundle();
}
/**
* Helper function to add amazon id to variables and JS settings.
*/
function _infinite_add_amazon_id(&$variables) {
if (!isset($variables['amazon_tag'])) {
$variables['amazon_tag'] = \Drupal::config('amazon.settings')->get('associates_id');
$variables['#attached']['drupalSettings']['amazon']['associatesIdDefault'] = $variables['amazon_tag'];
}
}