-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalm.theme
1232 lines (1151 loc) · 52.6 KB
/
palm.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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
use Drupal\block\Entity\Block;
use Drupal\node\Entity\Node;
use Drupal\Core\Field\EntityReferenceFieldItemList;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Markup;
use Drupal\Core\StringTranslation\TranslatableMarkup as Translation;
use Drupal\Core\Url;
use Drupal\nodeshop\NodeShop;
use Drupal\taxonomy\Entity\Term;
function palm_theme() {
return [
'palm_buy_button' => [
'template' => 'navigation/palm_buy_button',
'variables' => [
'product_node_id' => '',
'destination' => '',
'product_price' => '',
'label' => '',
'title' => '',
],
],
'palm_download_button' => [
'template' => 'navigation/palm_download_button',
'variables' => [
'download_url' => '',
'label' => '',
'title' => '',
],
],
'palm_social_links' => [
'template' => 'navigation/palm_social_links',
'variables' => [
'link_facebook' => '',
'link_twitter' => '',
'link_mail' => '',
'link_download' => '',
'node_url' => '',
'node_title' => '',
]
],
'palm_edition' => [
'template' => 'navigation/palm_edition',
'variables' => [
'edition' => [],
]
],
'palm_heading_pagecategory' => [
'template' => 'content/palm_heading_pagecategory',
'variables' => [
'category_prefix' => '',
'headline' => '',
'url' => '',
'link_title' => '',
]
],
];
}
/**
* Implements hook template_prepocess_page_title
*
* We use this to add a class to the page title, depending on the type of page
* that is displayed. Currently we set:
*
* - heading--article on all nodes
* - heading--pagecategory on all cart routes and for users
*
* @param array $variables The Drupal render array that goes into the template
*
* @return void
*/
function palm_preprocess_page_title(&$variables) {
$custom_headline_class = 'heading--pagecategory';
// Title overrides for nodes goes here:
if ($node = \Drupal::request()->attributes->get('node')) {
if ('ausgabe' == $node->bundle()) {
$variables['title_prefix'] = new Translation('Edition');
$variables['title_attributes']['class'][] = $custom_headline_class;
} elseif (false === strpos(\Drupal::routeMatch()->getRouteName(), 'Bonus')) {
$variables['title_attributes']['class'][] = 'heading--article';
}
}
// Title overrides for taxonomies or vocabularies goes here:
if (\Drupal::routeMatch()->getRouteName() == 'entity.taxonomy_term.canonical') {
$term = Term::load(\Drupal::routeMatch()->getRawParameter('taxonomy_term'));
if ('themen' == $term->bundle()) {
$variables['title_prefix'] = new Translation('Topic');
$variables['title_attributes']['class'][] = $custom_headline_class;
}
if ('autoren' == $term->bundle()) {
$variables['title_prefix'] = new Translation('Authors');
$variables['title_prefix_link'] = Url::fromRoute(
'view.blaetter_autoren.page_1',
[
'sort_character' => 'a'
]
);
$variables['title_prefix_link_title'] = new Translation('back to all editors');
$variables['title_attributes']['class'][] = $custom_headline_class;
}
}
if (false !== strpos(\Drupal::routeMatch()->getRouteName(), 'view.blaetter_themen.page_1')) {
$variables['title_prefix'] = 'Blätter';
}
if (false !== strpos(\Drupal::routeMatch()->getRouteName(), 'view.blaetter_blog.page_1')) {
$variables['title_prefix'] = 'Blätter';
}
if (false !== strpos(\Drupal::routeMatch()->getRouteName(), 'view.blaetter_ausgabe.page_1')) {
$variables['title_prefix'] = new Translation('editions');
}
if (false !== strpos(\Drupal::routeMatch()->getRouteName(), 'view.blaetter_dossiers.page_1')) {
$variables['title_prefix'] = 'Dossier';
$variables['title'] = new Translation('overview');
}
if (false !== strpos(\Drupal::routeMatch()->getRouteName(), 'view.blaetter_autoren.page_1')) {
$variables['title_prefix'] = 'Blätter';
}
if (false !== strpos(\Drupal::routeMatch()->getRouteName(), 'view.blaetter_autoren.page_2')) {
$variables['title_prefix'] = 'Blätter';
}
if (false !== strpos(\Drupal::routeMatch()->getRouteName(), 'view.blaetter_autoren.page_3')) {
$variables['title_prefix'] = 'Blätter';
}
if (false !== strpos(\Drupal::routeMatch()->getRouteName(), 'contact.site_page')) {
$variables['title'] = new Translation('Contact');
}
// add special prefixes for special routes
$prefix_routes_user = [
'entity.user.canonical',
'entity.user.edit_form',
];
if (in_array(\Drupal::request()->get('_route'), $prefix_routes_user)) {
$variables['title_prefix'] = new Translation('User account');
}
// We define routes, where we need a special class for the title
$routes = [
//'entity.taxonomy_term.canonical',
'contact.site_page',
'entity.user.cancel_form',
'entity.user.canonical',
'entity.user.contact_form',
'entity.user.edit_form',
'entity.contact_form.canonical',
'nodeshop.addBonus',
'nodeshop.changeBonus',
'nodeshop.finishedOrder',
'nodeshop.processOrder',
'nodeshop.removeBonus',
'nodeshop.showCart',
'search.help_node_search',
'search.help_blaetter_suche',
'search.view',
'search.view_node_search',
'search.view_suche',
'search.view_blaetter_suche',
'user.login',
'user.page',
'user.pass',
'user.register',
'user.reset.form',
'user.reset.login',
'user.reset',
'view.blaetter_ausgabe.page_1',
'view.blaetter_autoren.page_1',
'view.blaetter_autoren.page_2',
'view.blaetter_autoren.page_3',
'view.blaetter_blog.page_1',
'view.blaetter_dossiers.page_1',
'view.blaetter_themen.page_1',
'view.blaetter_zurueckgeblaettert.page_1',
'view.nodeshop_user_orders.page_1',
'view.nodeshop_user_orders.page_2',
];
if (in_array(\Drupal::request()->get('_route'), $routes)) {
$variables['title_attributes']['class'][] = $custom_headline_class;
}
}
/**
* Implementation of template_preprocess_block()
*
* @param array $variables
* @return void
*/
function palm_preprocess_block(&$variables) {
if ('grid_block' == $variables['plugin_id'] && 'visible' == $variables['configuration']['label_display']) {
// add class for red headlines to all grid blocks
$variables['title_attributes']['class'][] = 'red';
}
}
/**
* Implements hook_preprocess_HOOK().
*/
function palm_preprocess_breadcrumb(&$variables) {
$request = \Drupal::request();
// add link to author page on taxonomy term pages from the author vocabulary
if ('entity.taxonomy_term.canonical' == $request->get('_route')) {
$term = Term::load(\Drupal::routeMatch()->getRawParameter('taxonomy_term'));
if ('autoren' == $term->bundle()) {
$variables['#cache']['contexts'][] = 'url';
$author_index = [
['text' => new Translation('Authors'), 'url' => '/autoren']
];
array_splice($variables['breadcrumb'], 1, 0, $author_index);
}
}
// Change text for last breadcrumb part for user edit form
if ('entity.user.edit_form' == \Drupal::request()->get('_route')) {
$variables['breadcrumb'][2]['text'] = new Translation('Edit user account');
}
}
/**
* Implements hook template_preprocess_field__node__body
*
* We use this to add an extra class to the first paragraph of the body field as we want to have an initial there for
* every configured node type.
* In order to do so, we need to recreate the complete markup for the body field.
* We only want to do that on the full node view_mode
*
* @param array $variables The Drupal render array that goes into the template
*
* @return void
*/
function palm_preprocess_field__node__body(&$variables)
{
// // get current user
$current_user = \Drupal\user\Entity\User::load($variables['user']->id());
// get view mode (e.g. full or teaser)
$view_mode = $variables['element']['#view_mode'];
// get the node object
$node = $variables['element']['#object'];
if ('full' == $view_mode) {
// precreated render service
$renderer = \Drupal::service('renderer');
// prepare replacement body
$render_result = $renderer->render($variables['items'][0]['content']);
if (is_string($render_result)) {
$replaced_body = $render_result;
} else if (is_object($render_result) && method_exists($render_result, '__toString')) {
$replaced_body = $render_result->__toString();
} else {
$replaced_body = '';
}
// get node types, that will get an initiale
$palm_initial_nodetypes = theme_get_setting('palm_initial_nodetypes');
if (!empty($palm_initial_nodetypes) && in_array($variables['element']['#bundle'], $palm_initial_nodetypes)) {
$replaced_body = preg_replace(
'/<p>/i',
'<p class="initiale">',
$replaced_body,
1
);
}
// load ausgabe preview to node views of type story that:
// - are free to view
// - are viewed by an anonymous user
// - if its not a "Kurzgefasst" (Term 1856) nor a "Chronik" (Term 1837)
if ('story' === $variables['element']['#bundle']
&& isset($node)
&& NodeShop::grantUserAccess($node, $current_user)
&& $current_user->isAnonymous()
&& (
$node->hasField('field_artikeltyp')
&& isset($node->get('field_artikeltyp')->target_id)
&& 1856 != $node->get('field_artikeltyp')->target_id
&& 1837 != $node->get('field_artikeltyp')->target_id
)
) {
$book = \Drupal::entityTypeManager()->getStorage('node')->load($node->book['bid']);
if ($book instanceof Node && 'ausgabe' == $book->bundle()) {
$text_count_summary = new Translation(
'This article is part of the issue @issue. Click here to read the full issue.',
[
'@issue' => $book->label()
]
);
$edition = [
'#weight' => 3, // set it directly prior to links, which have #weight => 5
'#theme' => 'palm_edition',
'#edition' => [
'link' => $book->toUrl(),
'link_title' => new Translation('Click here to read the full issue.'),
'image' => $book->field_primary_picture->view('full'),
'text_count_summary' => $text_count_summary,
]
];
$markup = $renderer->render($edition);
$replaced_body = preg_replace(
'/(?s)((?:<p.*?>.*?<\/p>\s*){3})/',
'$1' . $markup,
$replaced_body,
1
);
}
}
// check for blocks that needs to be inserted into the node body
if (false !== strpos($replaced_body, '[block:')) {
// first, check for all blocks, that needs to be inserted:
\preg_match_all('/\[block:([^\]]+)\]/i', $replaced_body, $matches, \PREG_SET_ORDER);
foreach ($matches as $match) {
//$block = \Drupal\block_content\Entity\BlockContent::load($match[2]);
$block = \Drupal\block\Entity\Block::load($match[1]);
if ($block instanceof \Drupal\Core\Entity\EntityInterface) {
$block_entity = \Drupal::entityTypeManager()->getViewBuilder('block')->view($block);
$markup = $renderer->render($block_entity);
$replaced_body = str_replace(
$match[0],
$markup,
$replaced_body
);
}
}
}
// check for blocks that needs to be inserted into the node body
if (false !== strpos($replaced_body, '[content:')) {
// first, check for all blocks, that needs to be inserted:
\preg_match_all('/\[content:([^\]]+)\]/i', $replaced_body, $matches, \PREG_SET_ORDER);
foreach ($matches as $match) {
$block = \Drupal\block_content\Entity\BlockContent::load($match[1]);
if ($block instanceof \Drupal\Core\Entity\EntityInterface) {
$block_entity = \Drupal::entityTypeManager()->getViewBuilder('block_content')->view($block);
$markup = $renderer->render($block_entity);
$replaced_body = str_replace(
$match[0],
$markup,
$replaced_body
);
}
}
}
// check for media elements that needs to be inserted into the node body
if (false !== strpos($replaced_body, '[media:')) {
// first, check for all blocks, that needs to be inserted:
\preg_match_all('/\[media:([^\]]+)\]/i', $replaced_body, $matches, \PREG_SET_ORDER);
foreach ($matches as $match) {
//$block = \Drupal\block_content\Entity\BlockContent::load($match[2]);
$media = \Drupal\media\Entity\Media::load($match[1]);
if ($media instanceof \Drupal\Core\Entity\EntityInterface) {
$media_entity = \Drupal::entityTypeManager()->getViewBuilder('media')->view($media);
$markup = $renderer->render($media_entity);
$replaced_body = str_replace(
$match[0],
$markup,
$replaced_body
);
}
}
}
// replace the body with all replacements that have been made
$variables['items'][0]['content'] = Markup::create($replaced_body);
}
}
/**
* Implmentation of hook_preprocess_menu
* We use it to make the current path_alias available in the menu theme so we can
* identify menu structures that needs to be opened
*
* @param array $variables
* @return void
*/
function palm_preprocess_menu(&$variables)
{
if ('navigation' == $variables['menu_name']) {
$variables['current_path'] = \Drupal::request()->getRequestUri();
}
}
/**
* Implementation of hook_theme_suggestions_block_alter
*
* We use this to implement template suggestions based on the region of the page.
*
* @see https://www.drupal.org/node/2011434
*
* @param array $suggestions The referenced suggestions array
* @param array $variables The variables that are coming along with the suggestsions
*/
function palm_theme_suggestions_block_alter(array &$suggestions, array $variables)
{
if (!empty($variables['elements']['#id'])) {
$block = Block::load($variables['elements']['#id']);
$suggestions[] = 'block__' . $block->getRegion();
$suggestions[] = 'block__' . $block->getRegion() . '__' . $variables['elements']['#id'];
}
return $suggestions;
}
/**
* Implementation of hook_theme_suggestions_field_alter
*
* We use this to implement template suggestions based on the display mode of the field.
*
* @see https://www.drupal.org/project/zurb_foundation/issues/2962578
*
* @param array $suggestions The referenced suggestions array
* @param array $variables The variables that are coming along with the suggestsions
*/
function palm_theme_suggestions_field_alter(array &$suggestions, array $variables) {
$suggestion = 'field__' .
$variables['element']['#entity_type'] . '__' .
$variables['element']['#field_name'] . '__' .
$variables['element']['#view_mode'];
$suggestions[] = $suggestion;
return $suggestions;
}
function palm_theme_suggestions_taxonomy_term_alter(&$suggestions, $vars, $hook) {
$suggestions[] = 'taxonomy_term__' . $vars['elements']['#view_mode'];
}
/**
* Implememntation of hook_preprocess_page
*
* @param array $variables
* @return void
*/
function palm_preprocess_page(&$variables) {
// get route name of current request
$route_name = \Drupal::routeMatch()->getRouteName();
// prepare promo-block-setting
$variables['page']['promo_block'] = theme_get_setting('palm_promo_banner_state');
$variables['page']['promo_banner_title'] = theme_get_setting('palm_promo_banner_title');
$variables['page']['promo_banner_claim'] = theme_get_setting('palm_promo_banner_claim');
$variables['page']['promo_banner_description'] = theme_get_setting('palm_promo_banner_description');
$variables['page']['promo_banner_link'] = theme_get_setting('palm_promo_banner_link');
$variables['page']['promo_banner_image_url'] = theme_get_setting('palm_promo_banner_image_url');
$variables['page']['promo_banner_theme'] = theme_get_setting('palm_promo_banner_theme');
// disable promo-block in certain routes or for logged in users
if (str_starts_with($route_name, 'user')
|| str_starts_with($route_name, 'view.user')
|| str_starts_with($route_name, 'entity.user')
|| str_starts_with($route_name, 'nodeshop')
|| str_starts_with($route_name, 'view.nodeshop')
|| str_starts_with($route_name, 'search')
|| str_starts_with($route_name, 'view.search')
|| str_starts_with($route_name, 'contact')
|| \Drupal::currentUser()->isAuthenticated()
) {
$variables['page']['promo_block'] = false;
}
// check for error pages
if ('system.401' == $route_name || 'system.403' == $route_name || 'system.404' == $route_name) {
$error_code = str_replace('system.', '', $route_name);
$variables['page']['hero']['category_title'] = [
'#theme' => 'palm_heading_pagecategory',
'#headline' => new Translation('Error @code', ['@code' => $error_code]),
'#weight' => -11,
];
// force Drupal to resort the blocks in the region
$variables['page']['hero']['#sorted'] = false;
$variables['page']['promo_block'] = false;
}
// Add title prefix for all shop pages
if (false !== strpos(\Drupal::routeMatch()->getRouteName(), 'view.blaetter_shop')) {
$variables['page']['hero']['category_title'] = [
'#theme' => 'palm_heading_pagecategory',
'#category_prefix' => 'Blätter',
'#headline' => 'Kiosk',
'#weight' => -11,
];
// force Drupal to resort the blocks in the region
$variables['page']['hero']['#sorted'] = false;
$variables['page']['promo_block'] = false;
}
// Add title prefix for all eurozine pages
if (false !== strpos(\Drupal::routeMatch()->getRouteName(), 'view.blaetter_eurozine')) {
$variables['page']['hero']['category_title'] = [
'#theme' => 'palm_heading_pagecategory',
'#category_prefix' => 'Blätter',
'#headline' => 'International',
'#weight' => -11,
];
// force Drupal to resort the blocks in the region
$variables['page']['hero']['#sorted'] = false;
}
// Add title prefix for all eurozine pages
if (false !== strpos(\Drupal::routeMatch()->getRouteName(), 'view.blaetter_dokumente')) {
$variables['page']['hero']['category_title'] = [
'#theme' => 'palm_heading_pagecategory',
'#category_prefix' => 'Dokumente',
'#headline' => 'zum Zeitgeschehen',
'#weight' => -11,
];
// force Drupal to resort the blocks in the region
$variables['page']['hero']['#sorted'] = false;
}
if (($node = \Drupal::request()->attributes->get('node')) && $node instanceof Node) {
if ('story' == $node->getType()) {
$issue = _palm_get_issue($node);
$edition = '';
if (!empty($issue['year']) && !empty($issue['edition'])) {
$edition = $issue['edition'] . ' ' . $issue['year'];
}
$variables['page']['hero']['category_title'] = [
'#theme' => 'palm_heading_pagecategory',
'#category_prefix' => new Translation('Edition'),
'#headline' => $edition,
'#weight' => -11,
];
$variables['page']['hero']['#sorted'] = false;
}
// provide headings if in full node view
if ('dossier' == $node->getType()) {
$dossier_name = '';
if ($node->hasField('field_dossier') && $node->get('field_dossier') instanceof EntityReferenceFieldItemList) {
$dossier_name = $node->get('field_dossier')->entity->label();
$variables['page']['hero']['category_title'] = [
'#theme' => 'palm_heading_pagecategory',
'#category_prefix' => new Translation('Dossier'),
'#headline' => $dossier_name,
'#weight' => -11,
];
// force Drupal to resort the blocks in the region
$variables['page']['hero']['#sorted'] = false;
}
}
if ('product' == $node->getType()
&& $route_name != 'nodeshop.addBonus'
&& $route_name != 'nodeshop.changeBonus'
&& $route_name != 'nodeshop.removeBonus'
) {
$rubric_name = '';
if ($node->hasField('field_rubrik') && $node->get('field_rubrik') instanceof EntityReferenceFieldItemList) {
$rubric_name = $node->get('field_rubrik')->entity->label();
}
$variables['page']['hero']['category_title'] = [
'#theme' => 'palm_heading_pagecategory',
'#category_prefix' => new Translation('Blätter Kiosk'),
'#headline' => $rubric_name,
'#weight' => -11,
];
// force Drupal to resort the blocks in the region
$variables['page']['hero']['#sorted'] = false;
$variables['page']['promo_block'] = false;
}
// default page category on all page type nodes
if ('page' == $node->getType()) {
$page_category_prefix = new Translation('About');
$page_category_title = new Translation('the »Blätter«');
if ($node->hasField('field_page_category_prefix')
&& !empty($node->get('field_page_category_prefix')->value)) {
$page_category_prefix = $node->get('field_page_category_prefix')->value;
}
if ($node->hasField('field_page_category_title')
&& !empty($node->get('field_page_category_title')->value)) {
$page_category_title = $node->get('field_page_category_title')->value;
}
$variables['page']['hero']['category_title'] = [
'#theme' => 'palm_heading_pagecategory',
'#category_prefix' => $page_category_prefix,
'#headline' => $page_category_title,
'#weight' => -11,
];
// force Drupal to resort the blocks in the region
$variables['page']['hero']['#sorted'] = false;
}
// default page category on all aktuell type nodes
if ('aktuell' == $node->getType()) {
$variables['page']['hero']['category_title'] = [
'#theme' => 'palm_heading_pagecategory',
'#headline' => 'Aus der Redaktion',
'#weight' => -11,
];
// force Drupal to resort the blocks in the region
$variables['page']['hero']['#sorted'] = false;
}
// default page category on all zurückgeblaettert type nodes
if ('zurueckgeblaettert' == $node->getType()) {
$variables['page']['hero']['category_title'] = [
'#theme' => 'palm_heading_pagecategory',
'#headline' => 'Zurückgeblättert',
'#weight' => -11,
];
// force Drupal to resort the blocks in the region
$variables['page']['hero']['#sorted'] = false;
}
// default page category on all document type nodes
if ('document' == $node->getType()) {
$variables['page']['hero']['category_title'] = [
'#theme' => 'palm_heading_pagecategory',
'#headline' => 'zum Zeitgeschehen',
'#category_prefix' => 'Dokumente',
'#weight' => -11,
];
// force Drupal to resort the blocks in the region
$variables['page']['hero']['#sorted'] = false;
}
// default page category on all eurozine type nodes
if ('eurozine' == $node->getType()) {
$variables['page']['hero']['category_title'] = [
'#theme' => 'palm_heading_pagecategory',
'#category_prefix' => 'Blätter',
'#headline' => 'International',
'#weight' => -11,
];
// force Drupal to resort the blocks in the region
$variables['page']['hero']['#sorted'] = false;
}
// add page category on all blog type nodes
if ('blog' == $node->getType()) {
$variables['page']['hero']['category_title'] = [
'#theme' => 'palm_heading_pagecategory',
'#category_prefix' => 'Blätter',
'#headline' => new Translation('Blog'),
'#weight' => -11,
];
// force Drupal to resort the blocks in the region
$variables['page']['hero']['#sorted'] = false;
}
// add page category on all blog type nodes
if ('praemie' == $node->getType()) {
$variables['page']['hero']['category_title'] = [
'#theme' => 'palm_heading_pagecategory',
'#category_prefix' => 'Blätter',
'#headline' => new Translation('bonus'),
'#weight' => -11,
];
// force Drupal to resort the blocks in the region
$variables['page']['hero']['#sorted'] = false;
}
}
// add footer link node id and title if exists
$footer_link_nid = theme_get_setting('palm_footer_link');
if (!empty($footer_link_nid)) {
$variables['page']['footer_link'] = $footer_link_nid;
}
$footer_link_title = theme_get_setting('palm_footer_link_title');
if (!empty($footer_link_title)) {
$variables['page']['footer_link_title'] = $footer_link_title;
}
// add cancel button link and title if exists
$cancel_link_url = theme_get_setting('palm_footer_cancellation_button_link');
if (!empty($cancel_link_url)) {
$variables['page']['cancel_link_url'] = $cancel_link_url;
}
$cancel_link_title = theme_get_setting('palm_footer_cancellation_button_link_title');
if (!empty($cancel_link_title)) {
$variables['page']['cancel_link_title'] = $cancel_link_title;
}
}
/**
* Implementation of hook_preprocess_region
*
* @param array $variables
* @return void
*/
function palm_preprocess_region (&$variables) {
if ('navigation' == $variables['region']) {
// add promo button link and title if exists
$header_promo_button_link = theme_get_setting('palm_header_promo_button_link');
if (!empty($header_promo_button_link)) {
$variables['header_promo_button_link'] = $header_promo_button_link;
}
$header_promo_button_link_title = theme_get_setting('palm_header_promo_button_link_title');
if (!empty($header_promo_button_link_title)) {
$variables['header_promo_button_link_title'] = $header_promo_button_link_title;
}
}
}
/**
* Implementation of template_preprocess_node
*
* @param array $variables The reference to the variable array
* @return void
*/
function palm_preprocess_node(&$variables)
{
// get current user
$current_user = \Drupal\user\Entity\User::load($variables['user']->id());
// get view mode (e.g. full or teaser)
$view_mode = $variables['elements']['#view_mode'];
// get the renderer
$renderer = \Drupal::service('renderer');
/**
* @var Node|null $node
*/
$node = $variables['node'] ?: null;
// // add dossier articles to the dossier teaser
// if ('teaser_dossier' == $view_mode) {
// $variables['dossier_articles'] = [
// '#type' => 'view',
// '#name' => 'blaetter_dossiers',
// '#display_id' => 'block_1',
// '#arguments' => [
// $node->field_dossier->target_id,
// ],
// ];
// }
// add edition preview and purchase buttons to stories that are not available to the end user
if ('full' == $view_mode
&& isset($node)
&& 'story' == $node->getType()
&& !NodeShop::grantUserAccess($node, $current_user)
) {
if (isset($node->book) && !empty($node->book['bid'])) {
$book = \Drupal::entityTypeManager()->getStorage('node')->load($node->book['bid']);
if ($book instanceof Node && 'ausgabe' == $book->getType()) {
// calculate length of snippet if original body is set
$text_count_summary = '';
$original_body = $node->get('body')->getString();
$original_body_length = max(1, strlen($original_body));
$teaser_body_length = max(1, strlen(
nodeshop_trim_text(
$original_body, 1500, false
)
));
$teaser_body_percentage = round($teaser_body_length * 100 / $original_body_length);
$text_count_summary = new Translation(
'You\'ve read @countlow% of the text, to read the remaining @counthigh%, you might want to: ',
[
'@countlow' => $teaser_body_percentage,
'@counthigh'=> (100 - $teaser_body_percentage)
]
);
$edition = [
'#weight' => 3, // set it directly prior to links, which have #weight => 5
'#theme' => 'palm_edition',
'#edition' => [
'image' => $book->field_primary_picture->view('full'),
'text_count_summary' => $text_count_summary,
'buttons' => [
'buy_article' => [
'#theme' => 'palm_buy_button',
'#product_node_id' => $node->id(),
'#destination' => \Drupal::service('path.current')->getPath(),
'#product_price' => NodeShop::getPrice($node),
'#label' => new Translation('Buy article'),
'#title' => new Translation('Puts @title (print) into the cart.', ['@title' => $node->getTitle()]),
],
'buy_online' => [
'#theme' => 'palm_buy_button',
'#product_node_id' => $book->id(),
'#destination' => \Drupal::service('path.current')->getPath(),
'#product_price' => NodeShop::getPrice($book),
'#label' => new Translation('Buy online issue'),
'#title' => new Translation('Puts @title (online) into the cart.', ['@title' => $book->getTitle()]),
]
]
]
];
if ($book->hasField('field_ip') && !empty($book->get('field_ip')->value)) {
$print_issue = \Drupal::entityTypeManager()->getStorage('node')->load(
$book->get('field_ip')->value
);
if ($print_issue instanceof Node) {
$edition['#edition']['buttons']['buy_print'] = [
'#theme' => 'palm_buy_button',
'#product_node_id' => $print_issue->id(),
'#destination' => \Drupal::service('path.current')->getPath(),
'#product_price' => NodeShop::getPrice($print_issue),
'#label' => new Translation('Buy print issue'),
'#title' => new Translation('Puts @title (print) into the cart.', ['@title' => $print_issue->getTitle()]),
];
}
}
if ($current_user->isAnonymous()) {
$edition['#edition']['buttons']['login'] = [
'#title' => new Translation('login to read full article'),
'#type' => 'link',
'#url' => Url::fromRoute(
'user.login',
[],
[
'query' => [
'destination' => \Drupal::service('path.current')->getPath()
]
]
),
'#attributes' => [
'rel' => 'nofollow',
'class' => ['button', 'button--reversed']
]
];
// $edition['#edition']['buttons']['register'] = [
// '#theme' => 'link',
// '#title' => new Translation('register'),
// '#type' => 'link',
// '#url' => Url::fromRoute(
// 'user.register',
// [],
// [
// 'query' => [
// 'destination' => \Drupal::service('path.current')->getPath()
// ]
// ]
// ),
// '#attributes' => [
// 'rel' => 'nofollow',
// 'class' => ['button', 'button--reversed'],
// ]
// ];
}
$variables['content']['edition'] = $edition;
unset($variables['content']['field_themen']);
unset($variables['content']['field_placeholder_issue']);
unset($variables['content']['links']);
}
}
}
// add social block for all full node views
if ('full' == $view_mode && isset($node)) {
$node_url = $node->toUrl()->setAbsolute()->toString();
$node_title = $node->getTitle();
$node_teaser = '';
if (!empty($node->body->view('teaser'))) {
// just get the plain teaser text for the mail, no html needed.
$node_teaser = $node->body->view('teaser');
}
$variables['social'] = [
'#theme' => 'palm_social_links',
'#link_facebook' => true, // is set from node_title and node_url in the template
'#link_twitter' => new Translation('Read on @blaetter: @title', ['@title' => $node_title]),
'#link_mail' => 'mailto:?subject=' . new Translation(
'Read @blaetter: @title',
[
'@title' => $node_title
]
) . '&body=' . new Translation(
'Hello,%0D%0A%0D%0AI found this interesting article on the website www.blaetter.de:%0D%0A%0D%0A@title%0D%0A%0D%0A@teaser%0D%0A%0D%0A@link%0D%0A',
[
'@title' => $node_title,
'@teaser' => strip_tags($renderer->render($node_teaser)),
'@link' => $node_url,
]
),
'#node_url' => $node_url,
'#node_title' => $node_title,
];
// set download links only for nodes of type story. There might be additional
// node types configured via nodeshop, but there is currently no way to access
// this check from this point.
if (NodeShop::grantUserAccess($node, $current_user) && 'story' == $node->bundle()) {
try {
\Drupal::service('router.route_provider')->getRouteByName(
'nodeshop.product_features.product_feature_access_to_attachment.download'
);
$variables['social']['#link_download'] = Url::fromRoute(
'nodeshop.product_features.product_feature_access_to_attachment.download',
[
'node' => $node->id()
]
);
} catch (\Exception $e) {
// nothing to do here, just catch exception...
}
}
// Add link rel to Blätter Podcast RSS Feed on the podcast page (e.g. node id 28623)
if (28623 == $node->id()) {
$link = [
'#tag' => 'link',
'#attributes' => [
'rel' => 'alternate',
'type' => 'application/rss+xml',
'title' => 'Blätter Podcast',
'href' => 'https://blaetter.podigee.io/feed/mp3'
],
];
$variables['#attached']['html_head'][] = [$link, 'swiftype_course'];
}
// add robots header to restricted URL
if (31575 == $node->id()) {
$variables['#attached']['http_header'][] = ['X-Robots-Tag', 'noindex, noarchive, nosnippet'];
}
}
if (isset($node) && 'ausgabe' == $node->bundle()) {
// unset download link in the social menu here because we might set it ourselves...
if (isset($variables['social']) && isset($variables['social']['#link_download'])) {
unset($variables['social']['#link_download']);
}
// add the articles for an edition (aka ausgabe) as a block.
$variables['edition_articles'] = [
'#type' => 'view',
'#name' => 'blaetter_ausgabe',
'#display_id' => 'block_2',
'#arguments' => [
$node->id(),
],
];
// prepare buttons to buy print and digital issues or download them if already bought:
if ($node->hasField('field_ip') && !empty($node->get('field_ip')->value)) {
$print_issue = \Drupal::entityTypeManager()->getStorage('node')->load(
$node->get('field_ip')->value
);
if ($print_issue instanceof Node) {
$variables['button_buy_print'] = [
'#theme' => 'palm_buy_button',
'#product_node_id' => $print_issue->id(),
'#destination' => \Drupal::service('path.current')->getPath(),
'#product_price' => NodeShop::getPrice($print_issue),
'#label' => new Translation('Buy print issue'),
'#title' => new Translation('Puts @title (print) into the cart.', ['@title' => $print_issue->getTitle()]),
];
}
}
if (!NodeShop::isUserNode($node, $current_user) && !$current_user->hasRole('onlineabonnent')) {
// only display this button if the user not already bought this online issue
$variables['button_buy_online'] = [
'#theme' => 'palm_buy_button',
'#product_node_id' => $node->id(),
'#destination' => \Drupal::service('path.current')->getPath(),
'#product_price' => NodeShop::getPrice($node),
'#label' => new Translation('Buy online issue'),
'#title' => new Translation('Puts @title (online) into the cart.', ['@title' => $node->getTitle()]),
];
} else {
// only display this button if issue is newer than janurary 2012 (with explicit exception for december 2011
if (24911 <= $node->id() && 27086 != $node->id()) {
$variables['button_download_pdf'] = [
'#theme' => 'palm_download_button',
'#download_url' => Url::fromRoute(
'nodeshop.product_features.product_feature_access_to_attachment.download',
[
'node' => $node->id()
]
),
'#label' => new Translation('Download digital issue'),
'#title' => new Translation('Download @title as pdf file.', ['@title' => $node->getTitle()]),
];
}
// Downlaod of epub only for nodes newer than 25756
if (25756 <= $node->id()) {
$variables['button_download_epub'] = [
'#theme' => 'palm_download_button',
'#download_url' => Url::fromRoute(
'blaetter_export_epub.epub.download',
[
'node' => $node->id()
]
),
'#label' => new Translation('Download ePub for eReader'),
'#title' => new Translation('Download @title as ePub file.', ['@title' => $node->getTitle()]),
];
}
}
// This is made to add an extra to the node title if a node from type
// ausgabe is put into the cart.
// The following way seems a bit dirty, but it's the only working way I
// could found.
if ('cart_item_node' == $view_mode) {
if ('ausgabe' == $node->bundle()) {
$variables['label'][0]['#context']['value'] = $variables['label'][0]['#context']['value'] .
' (' . new Translation('digital issue') . ')';
}
}
}
// add term based css class to article on full story nodes
if ('full' == $view_mode
&& isset($node)
&& 'story' == $node->getType()
&& $node->hasField('field_artikeltyp')
&& isset($node->get('field_artikeltyp')->target_id)