generated from SU-SWS/stanford_starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchem_h_subtheme.theme
executable file
·217 lines (188 loc) · 6.81 KB
/
chem_h_subtheme.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
<?php
/**
* @file
* Preprocess functions for chem_h.
*/
use Drupal\ui_patterns\Element\PatternContext;
use Drupal\node\NodeInterface;
/**
* Implements hook_preprocess().
*/
function chem_h_subtheme_preprocess(&$variables, $hook) {
if ($hook === 'pattern_news-vertical-teaser') {
$variables['use'] = '@chemh_subtheme/news-vertical-teaser/news-vertical-teaser.html.twig';
}
}
/**
* Prepares variables for the html.html.twig template.
*/
function chem_h_subtheme_preprocess_html(&$variables) {
$variables['stanford_basic_path'] = \Drupal::service('extension.list.theme')
->getPath('stanford_basic');
$twittercard = [
'#tag' => 'meta',
'#attributes' => [
'name' => 'twitter:card',
'content' => 'summary_large_image',
],
];
$twittersite = [
'#tag' => 'meta',
'#attributes' => [
'name' => 'twitter:site',
'content' => '@Stanford_ChEMH',
],
];
$twittercreator = [
'#tag' => 'meta',
'#attributes' => [
'name' => 'twitter:creator',
'content' => '@Stanford_ChEMH',
],
];
$twittertitle = [
'#tag' => 'meta',
'#attributes' => [
'name' => 'twitter:title',
'content' => 'Sarafan ChEM-H',
],
];
$twitterdesc = [
'#tag' => 'meta',
'#attributes' => [
'name' => 'twitter:description',
'content' => 'Bridging chemistry, engineering, biology, and medicine to improve human health',
],
];
$twitterimg = [
'#tag' => 'meta',
'#attributes' => [
'name' => 'twitter:image',
'content' => 'https://chemh.stanford.edu/sites/g/files/sbiybj25016/files/styles/breakpoint_2xl_2x/public/media/image/building_dedication_0.jpg?itok=PZmd5ipP',
],
];
// Is front page variable
// set the front variable to add meta tags to on the homepage.
if (\Drupal::service('path.matcher')->isFrontPage()) {
$variables['page']['#attached']['html_head'][] = [$twittercard, 'twitter:card'];
$variables['page']['#attached']['html_head'][] = [$twittersite, 'twitter:site'];
$variables['page']['#attached']['html_head'][] = [$twittercreator, 'twitter:creator'];
$variables['page']['#attached']['html_head'][] = [$twittertitle, 'twitter:title'];
$variables['page']['#attached']['html_head'][] = [$twitterdesc, 'twitter:description'];
$variables['page']['#attached']['html_head'][] = [$twitterimg, 'twitter:image'];
}
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function chem_h_subtheme_theme_suggestions_block_alter(array &$suggestions, array $variables) {
if (!empty($variables['elements']['#id']) && $variables['elements']['#id'] == 'chem_h_subtheme_search') {
$suggestions[] = 'block__stanford_basic_search';
}
}
function chem_h_subtheme_theme_suggestions_form_alter(array &$suggestions, array $variables) {
if (isset($variables['element']['#view_id'])) {
$suggestions[] = 'form__' . $variables['element']['#view_id'];
}
}
/**
* Implements hook_preprocess_HOOK().
*/
function chem_h_subtheme_preprocess_ds_entity_view(&$variables) {
// if not running on a paragraph, exit out.
if (!isset($variables['content']['#paragraph'])) {
return;
}
$paragraph = $variables['content']['#paragraph'];
// Process Cards
if ($paragraph->bundle() == 'stanford_card') {
chem_h_subtheme_preprocess_card($variables);
}
// Process Banners
if ($paragraph->bundle() == 'stanford_banner') {
chem_h_subtheme_preprocess_banner($variables);
}
}
/**
* Implements hook_preprocess_card().
*/
function chem_h_subtheme_preprocess_card(&$variables) {
$paragraph = $variables['content']['#paragraph'];
// Add border style class to card wrapper.
$variables['attributes']['class'][] = $paragraph->getBehaviorSetting('react_paragraphs:card_variant_info', 'card_border_style', 'su-card-wrapper--border-regular');
$card_info = [];
// Add card type class to card wrapper. 3rd variable provides the default card type.
$card_info['card_type'] = $paragraph->getBehaviorSetting('react_paragraphs:card_variant_info', 'card_type', 'su-card-wrapper--default');
$variables['attributes']['class'][] = $card_info['card_type'];
}
/**
* Implements hook_preprocess_banner().
*/
function chem_h_subtheme_preprocess_banner(&$variables) {
$paragraph = $variables['content']['#paragraph'];
// Add button color style class to card wrapper.
$variables['attributes']['class'][] = $paragraph->getBehaviorSetting('react_paragraphs:banner_variant_info', 'banner_style', 'chemh-wrapper--banner-default');
}
/**
* Implements hook_preprocess_HOOK().
*/
function chem_h_subtheme_preprocess_field__node__title__stanford_person(&$variables) {
/** @var \Drupal\node\NodeInterface $node */
$node = $variables['element']['#object'];
$type_terms = [];
$term_storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
if ($node->hasField('su_person_type_group') && !$node->get('su_person_type_group')
->isEmpty()) {
/** @var \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem $field_item */
foreach ($node->get('su_person_type_group') as $field_item) {
try {
$term_id = $field_item->get('target_id')->getString();
$type_terms[] = $term_storage->load($term_id)->label();
}
catch (\Throwable $e) {
continue;
}
}
}
$terms_to_display = 1;
if ($type_terms && $variables['items'][0]['content']['#tag'] == 'h1') {
$variables['items'][0]['content']['#attributes']['class'][] = 'order-last';
$variables['items'][0]['content'] = [
'title' => $variables['items'][0]['content'],
'type' => [
'#type' => 'html_tag',
'#tag' => 'div',
'#attributes' => [
'class' => ['chem-h-person-type', 'order-first'],
],
'#value' => implode(', ', array_slice($type_terms, 0, $terms_to_display)),
],
];
}
}
/**
* Implements hook_preprocess_paragraph__stanford_layout().
*/
function chem_h_subtheme_preprocess_paragraph__stanford_layout(array &$variables): void {
/** @var \Drupal\paragraphs\Layout\Paragraph $paragraph */
$paragraph = &$variables['paragraph'];
$existing_classes = $variables['attributes']['class'] ?? [];
$extra_classes = [
$paragraph->getBehaviorSetting('react_paragraphs:layout_variant_info', 'padding_bottom'),
$paragraph->getBehaviorSetting('react_paragraphs:layout_variant_info', 'section_color'),
];
$variables['attributes']['class'] = array_merge($existing_classes, $extra_classes);
}
/**
* Implements hook_preprocess_HOOK().
*/
function chem_h_subtheme_preprocess_pattern_events_card(&$variables){
if (isset($variables['context']) && $variables['context'] instanceof PatternContext) {
/** @var \Drupal\ui_patterns\Element\PatternContext $context */
$context = $variables['context'];
$node = $context->getProperty('entity');
if ($node instanceof NodeInterface) {
$variables['page_url'] = $node->toUrl()->toString();
}
}
}