-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfinite_amp.theme
520 lines (461 loc) · 18.3 KB
/
infinite_amp.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
<?php
use Drupal\Core\GeneratedLink;
use Drupal\Core\Url;
use Drupal\infinite_media\MediaHelper;
use Drupal\node\NodeInterface;
use Drupal\views\ViewExecutable;
use Drupal\views\Views;
/**
* Implements template_theme_suggestions_HOOK_alter() for dfp_amp ad entities.
*/
function infinite_amp_theme_suggestions_dfp_amp_alter(array &$suggestions, array $variables, $hook) {
$ad_entity = $variables['ad_entity'];
$suggestions[] = $hook . '__' . $ad_entity->id();
}
function infinite_amp_preprocess(&$variables) {
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_app_id'] = $metatags['fb_app_id'];
}
}
$variables['environment'] = ['systemType' => isset($_ENV['AH_SITE_ENVIRONMENT']) ? $_ENV['AH_SITE_ENVIRONMENT'] : 'local'];
}
function infinite_amp_preprocess_node(&$variables) {
/* @var Node $node */
$node = $variables['node'];
// 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;
// Override TWIG url with absolute alias URL.
$variables['path'] = $variables['url']; // used for lazy loading history API.
$variables['url'] = $node->url('canonical', ['absolute' => TRUE]);
$variables['nid'] = $node->id();
$variables['share_img_url'] = MediaHelper::getImageUrlFromMediaReference(
$node,
'field_teaser_media',
theme_get_setting('share_image_style')
);
$variables['pinterest_share_img_url'] = MediaHelper::getImageUrlFromMediaReference(
$node,
'field_pinterest_image'
);
// 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();
_infinite_amp_add_amazon_id($variables);
}
}
if (isset($variables['content']['field_paragraphs'])) {
foreach (Drupal\Core\Render\Element::children($variables['content']['field_paragraphs']) as $key) {
$paragraph = $variables['content']['field_paragraphs'][$key]['#paragraph'];
if ($paragraph) {
if (in_array($paragraph->bundle(), ['recent_content', 'term_teaser'])) {
$variables['content']['field_paragraphs'][$key] = ['#markup' => ''];
}
}
}
}
if ($bundle == 'ecommerce_slider') {
if (($node_from_url = \Drupal::routeMatch()->getParameter('node')) && $node_from_url instanceof NodeInterface) {
if ($node_from_url->id() != $node->id()) {
$variables['parent_title'] = str_replace('"', '', $node_from_url->label());
$variables['parent_id'] = $node_from_url->id();
$variables['parent_type'] = 'node';
}
else {
$variables['parent_id'] = $node->id();
$variables['parent_type'] = 'node';
$variables['parent_title'] = str_replace('"', '', $node->label());
}
}
else if (($term_from_url = \Drupal::routeMatch()->getParameter('taxonomy_term')) && $term_from_url instanceof TermInterface) {
$variables['parent_id'] = $term_from_url->id();
$variables['parent_type'] = 'taxonomy_term';
$variables['parent_title'] = str_replace('"', '', $term_from_url->label());
}
_infinite_amp_add_amazon_id($variables);
}
}
/**
* Implements hook_preprocess_taxonomy_term().
*/
function infinite_amp_preprocess_taxonomy_term(&$variables) {
if (isset($variables['content']['field_paragraphs'])) {
foreach (Drupal\Core\Render\Element::children($variables['content']['field_paragraphs']) as $key) {
$paragraph = $variables['content']['field_paragraphs'][$key]['#paragraph'];
if ($paragraph) {
if (in_array($paragraph->bundle(), ['recent_content'])) {
$variables['content']['field_paragraphs'][$key] = ['#markup' => ''];
}
}
}
}
}
/**
* Implements hook_preprocess_advertising_product().
*/
function infinite_amp_preprocess_advertising_product(&$variables) {
$product = $variables['elements']['#advertising_product'];
$variables['advertising_product'] = $product;
$price = $product->product_price->value;
$sold_out = $product->product_sold_out->value;
if ($price < 0 || $sold_out) {
$variables['sold_out'] = TRUE;
}
$original_price = $product->product_original_price->value;
if (!isset($variables['sold_out']) || !$variables['sold_out']) {
if ($price < $original_price) {
$variables['show_strikethrough_price'] = TRUE;
}
}
// update links to tipser products
if ('advertising_product_tipser' === $product->bundle()) {
$tipserProductId = $product->get('product_id')->value;
// add request uri
$query = [
'article' => Url::fromUserInput($_SERVER['REQUEST_URI'])
->setOption('query', null)
->setAbsolute(true)
->toString(),
];
// add node title (if we are on a node or term page)
$entity = \Drupal::routeMatch()->getParameter('node');
if (null === $entity) {
$entity = \Drupal::routeMatch()->getParameter('taxonomy_term');
}
if ($entity) {
$query['title'] = $entity->get('field_seo_title')->value;
$query['parent_id'] = $entity->id();
$query['parent_type'] = $entity->getEntityTypeId();
}
$variables['tipser_url'] = Url::fromRoute('tipser_client.open_from_amp_page', [
'productId' => $tipserProductId,
], [
'query' => $query,
]);
$variables['link_target'] = '_self';
} else {
$variables['link_target'] = '_blank';
}
$provider = explode("_", $product->product_provider->value)[0];
$variables['provider'] = $provider;
}
/**
* Implements hook_preprocess_paragraphs().
*/
function infinite_amp_preprocess_paragraph(&$variables) {
if ($variables['paragraph']->getType() === 'recent_content') {
$items = 3;
$paragraph = $variables['paragraph'];
if ($paragraph->hasField('field_item_count') && !$paragraph->field_item_count->isEmpty()) {
$items = $paragraph->field_item_count->value;
}
$embed_view = paragraphs_starterkit_recent_content_views_embed_view('paragraph_recent_content', 'amp_recent_content', $items);
$recent_content_view = \Drupal::service('renderer')->render($embed_view);
$variables['content']['amp_recent_content_view'] = array(
'#markup' => $recent_content_view,
);
} else if ($variables['paragraph']->getType() === 'iframe') {
$variables['#attached']['library'][] = 'amp/amp.iframe';
}
if (
$variables['paragraph']->hasField('field_progress_navigation') &&
!$variables['paragraph']->field_progress_navigation->isEmpty()
) {
$variables['paragraph']->set('field_progress_navigation', NULL);
}
// Add title, type and id of parent entity
if (($parent = $variables['paragraph']->getParentEntity())) {
$variables['parent_id'] = $parent->id();
if ($parent->getEntityTypeId() === 'taxonomy_term') {
$variables['parent_type'] = 'taxonomy_term';
}
else if ($parent->getEntityTypeId() === 'node') {
$variables['parent_type'] = 'node';
}
else if ($parent->getEntityTypeId() === 'user') {
$variables['parent_type'] = 'user';
}
$title = $parent->label();
// Clean it up for use in tracking urls.
$variables['parent_title'] = str_replace('"', '', $title);
}
// Initialise amazon id.
_infinite_amp_add_amazon_id($variables);
// Overwrite amazon id it 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['amazon_tag'] = $node_entity->get('field_amazon_tag')->value;
}
}
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function infinite_amp_theme_suggestions_user_alter(array &$suggestions, array $variables, $hook) {
if (!empty($variables['elements']['#view_mode'])) {
$suggestions[] = $hook . '__' . $variables['elements']['#view_mode'];
}
}
/**
* Implements hook_preprocess_outbrain().
*/
function infinite_amp_preprocess_outbrain(&$variables) {
if (Drupal::request()->attributes->has('node')) {
$node = Drupal::request()->attributes->get('node');
$variables['canonical_url'] = $canonical_href = $node->toUrl('canonical', ['absolute' => TRUE])->toString();
$variables['amp_url'] = $amp_href = \Drupal::service('amp.query_parameters')->add($canonical_href);
$variables['#attached']['library'][] = 'amp/amp.iframe';
}
}
/**
* Implements hook_preprocess_region().
*/
function infinite_amp_preprocess_region(&$variables) {
$site_config = \Drupal::config('system.site');
$variables['site_name'] = $site_config->get('name');
}
/**
* Implements hook_preprocess_media().
*/
function infinite_amp_preprocess_media(&$variables) {
$variables['media'] = $variables['elements']['#media'];
if ($variables['media_type'] == 'nexx_video') {
$variables['omnia_id'] = \Drupal::config('nexx_integration.settings')->get('omnia_id');
}
}
/**
* Implements hook_form_system_theme_settings_alter().
*
* Custom theme settings
*/
function infinite_amp_form_system_theme_settings_alter(&$form, &$form_state) {
/*--------------- Share button Settings --------------*/
$form['share'] = array(
'#type' => 'fieldset',
'#title' => t('Share button settings'),
'#collapsed' => TRUE,
'#collapsible' => TRUE,
);
$form['share']['gtm_id'] = array(
'#type' => 'textfield',
'#title' => t('Google tag manager ID'),
'#default_value' => theme_get_setting('gtm_id'),
'#size' => 80,
'#description' => t('Enter your Google tag manager ID.'),
'#prefix' => '<div id="gtm-id-wrapper">',
'#suffix' => '</div>',
);
$form['share']['share_image_style'] = array(
'#type' => 'select',
'#title' => t('Image style for sharing'),
'#options' => image_style_options(),
'#default_value' => theme_get_setting('share_image_style'),
'#description' => t('Select the image style, that will be used for sharing on social media.'),
'#prefix' => '<div id="share-image-style-wrapper">',
'#suffix' => '</div>',
);
$form['share']['facebook_share_button'] = array(
'#type' => 'textfield',
'#title' => t('Facebook share button text'),
'#default_value' => theme_get_setting('facebook_share_button'),
'#size' => 80,
'#description' => t('Enter the the text, that will be shown on the facebook share button.'),
'#prefix' => '<div id="facebook-share-button-wrapper">',
'#suffix' => '</div>',
);
$form['share']['whatsapp_share_button'] = array(
'#type' => 'textfield',
'#title' => t('Whatsapp share button text'),
'#default_value' => theme_get_setting('whatsapp_share_button'),
'#size' => 80,
'#description' => t('Enter the the text, that will be shown on the whatsapp share button.'),
'#prefix' => '<div id="whatsapp-share-button-wrapper">',
'#suffix' => '</div>',
);
$form['share']['whatsapp_share_text'] = array(
'#type' => 'textfield',
'#title' => t('Whatsapp share text'),
'#default_value' => theme_get_setting('whatsapp_share_text'),
'#size' => 80,
'#description' => t('Enter the the text, that will be shown as share whatsapp text.'),
'#prefix' => '<div id="whatsapp-share-text-wrapper">',
'#suffix' => '</div>',
);
$form['share']['pinterest_share_button'] = array(
'#type' => 'textfield',
'#title' => t('Pinterest share button text'),
'#default_value' => theme_get_setting('pinterest_share_button'),
'#size' => 80,
'#description' => t('Enter the the text, that will be shown on the pinterest share button.'),
'#prefix' => '<div id="pinterest-share-button-wrapper">',
'#suffix' => '</div>',
);
$form['share']['twitter_share_button'] = array(
'#type' => 'textfield',
'#title' => t('Twitter share button text'),
'#default_value' => theme_get_setting('twitter_share_button'),
'#size' => 80,
'#description' => t('Enter the the text, that will be shown on the twitter share button.'),
'#prefix' => '<div id="twitter-share-button-wrapper">',
'#suffix' => '</div>',
);
$form['share']['twitter_share_via'] = array(
'#type' => 'textfield',
'#title' => t('Via which twitter handle to share.'),
'#default_value' => theme_get_setting('twitter_share_via'),
'#size' => 80,
'#description' => t('Enter the the twitter handle, that will be shown when sharing content.'),
'#prefix' => '<div id="twitter-share-via-wrapper">',
'#suffix' => '</div>',
);
$form['share']['email_share_button'] = array(
'#type' => 'textfield',
'#title' => t('Email share button text'),
'#default_value' => theme_get_setting('email_share_button'),
'#size' => 80,
'#description' => t('Enter the the text, that will be shown on the email share button.'),
'#prefix' => '<div id="email-share-button-wrapper">',
'#suffix' => '</div>',
);
$form['share']['email_share_text'] = array(
'#type' => 'textfield',
'#title' => t('Email share text'),
'#default_value' => theme_get_setting('email_share_text'),
'#size' => 80,
'#description' => t('Enter the the text, that will be shown in the email.'),
'#prefix' => '<div id="email-share-button-wrapper">',
'#suffix' => '</div>',
);
$form['share']['email_subject'] = array(
'#type' => 'textfield',
'#title' => t('Email subject text'),
'#default_value' => theme_get_setting('email_subject'),
'#size' => 80,
'#description' => t('Enter the the text, that will be shown as the email subject.'),
'#prefix' => '<div id="email-share-subject-wrapper">',
'#suffix' => '</div>',
);
}
/**
* Implements hook_preprocess_html();
*/
function infinite_amp_preprocess_html(&$variables) {
$variables['#attached']['library'][] = 'infinite_amp/amp.ad';
$variables['#attached']['library'][] = 'infinite_amp/amp.amp-carousel';
$variables['#attached']['library'][] = 'infinite_amp/amp.list';
$variables['#attached']['library'][] = 'infinite_amp/amp.mustache';
$variables['#attached']['library'][] = 'infinite_amp/amp.social-share';
$variables['#attached']['library'][] = 'amp/amp.iframe';
if (in_array(parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH), [
'/shop-it',
'/shop-it-elle',
])) {
$variables['#attached']['library'][] = 'infinite_amp/amp.bind';
}
$css = file_get_contents(drupal_get_path('theme', 'infinite_amp') . '/css/infinite-amp-base.css');
if ($css) {
$variables['amp_css'] = $css;
}
}
/**
* Implements hook_preprocess_block().
*/
function infinite_amp_preprocess_block(&$variables) {
if ($variables['plugin_id'] === 'infinite_blocks_channel_presenter') {
$variables['#attached']['library'][] = 'amp/amp.carousel';
}
}
/**
* Implements hook_preprocess_file_link() to remove type from a-tag for AMP.
*
* @see INREL-5303
*/
function infinite_amp_preprocess_file_link(&$variables) {
if ($variables['link'] instanceof GeneratedLink) {
$doc = new DOMDocument();
$doc->loadHTML($variables['link']->getGeneratedLink(), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$node = $doc->getElementsByTagName('a')[0];
$node->removeAttribute('type');
$variables['link']->setGeneratedLink(trim($doc->saveHTML()));
}
}
/**
* Implements hook_views_pre_render().
*/
function infinite_amp_views_pre_render(ViewExecutable $view) {
if ($view->id() === 'infinite_taxonomy_term' && !$view->is_attachment) {
// Remove the footer until the newsletter form is rebuilt in an AMP compatible format.
$view->footer = [];
// Remove the pager.
$view->pager = [];
}
}
function infinite_amp_preprocess_page(&$variables) {
$variables['site_name'] = \Drupal::config('system.site')->get('name');
}
function _infinite_amp_append_css($css, $purifiedPath, $standardPath) {
if (file_exists($purifiedPath)) {
$css .= file_get_contents($purifiedPath);
} elseif (file_exists($standardPath)) {
$css .= file_get_contents($standardPath);
}
return $css;
}
/**
* Helper function to add amazon id to variables and JS settings.
*/
function _infinite_amp_add_amazon_id(&$variables) {
if (!isset($variables['amazon_tag'])) {
$variables['amazon_tag'] = \Drupal::config('amazon.settings')->get('associates_id');
}
}