-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjbict.theme
274 lines (243 loc) · 9.55 KB
/
jbict.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
<?php
/**
* @file
* Functions to support theming in the Bartik theme.
*/
use Drupal\Component\Utility\Html;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Template\Attribute;
use Drupal\views\Form\ViewsForm;
use Drupal\Core\Render\Markup;
/**
* Implements hook_preprocess_HOOK() for HTML document templates.
*
* Adds body classes if certain regions have content.
*/
function jbict_preprocess_html(&$variables) {
// Add information about the number of sidebars.
if (!empty($variables['page']['sidebar_first']) && !empty($variables['page']['sidebar_second'])) {
$variables['attributes']['class'][] = 'layout-two-sidebars';
}
elseif (!empty($variables['page']['sidebar_first'])) {
$variables['attributes']['class'][] = 'layout-one-sidebar';
$variables['attributes']['class'][] = 'layout-sidebar-first';
}
elseif (!empty($variables['page']['sidebar_second'])) {
$variables['attributes']['class'][] = 'layout-one-sidebar';
$variables['attributes']['class'][] = 'layout-sidebar-second';
}
else {
$variables['attributes']['class'][] = 'layout-no-sidebars';
}
if (!empty($variables['page']['featured_top'])) {
$variables['attributes']['class'][] = 'has-featured-top';
}
$site_config = \Drupal::config('system.site');
// Construct page title.
if (isset($variables['page']['#title']) && is_array($variables['page']['#title'])) {
// Do an early render if the title is a render array.
$variables['page']['#title'] = (string) \Drupal::service('renderer')->render($variables['page']['#title']);
}
if (!empty($variables['page']['#title'])) {
$head_title = array(
// Marking the title as safe since it has had the tags stripped.
'title' => Markup::create(trim(strip_tags($variables['page']['#title']))),
'name' => $site_config->get('slogan'),
);
}
else {
$head_title = ['name' => $site_config->get('name')];
if ($site_config->get('slogan')) {
$head_title['slogan'] = strip_tags($site_config->get('slogan'));
}
}
$variables['head_title'] = $head_title;
// @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
$variables['head_title_array'] = $head_title;
if ( $variables['root_path'] == false ) {
$variables['head_title'] = array($site_config->get('slogan'));
}
$site_config = \Drupal::config('system.site');
// Construct page title.
if (isset($variables['page']['#title']) && is_array($variables['page']['#title'])) {
// Do an early render if the title is a render array.
$variables['page']['#title'] = (string) \Drupal::service('renderer')->render($variables['page']['#title']);
}
if (!empty($variables['page']['#title'])) {
$head_title = array(
// Marking the title as safe since it has had the tags stripped.
'title' => Markup::create(trim(strip_tags($variables['page']['#title']))),
'name' => $site_config->get('slogan'),
);
}
else {
$head_title = ['name' => $site_config->get('name')];
if ($site_config->get('slogan')) {
$head_title['slogan'] = strip_tags($site_config->get('slogan'));
}
}
$variables['head_title'] = $head_title;
// @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
$variables['head_title_array'] = $head_title;
if ( $variables['root_path'] == false ) {
$variables['head_title'] = array($site_config->get('slogan'));
}
}
/**
* Implements hook_preprocess_HOOK() for page title templates.
*/
function jbict_preprocess_page_title(&$variables) {
// Since the title and the shortcut link are both block level elements,
// positioning them next to each other is much simpler with a wrapper div.
if (!empty($variables['title_suffix']['add_or_remove_shortcut']) && $variables['title']) {
// Add a wrapper div using the title_prefix and title_suffix render
// elements.
$variables['title_prefix']['shortcut_wrapper'] = [
'#markup' => '<div class="shortcut-wrapper clearfix">',
'#weight' => 100,
];
$variables['title_suffix']['shortcut_wrapper'] = [
'#markup' => '</div>',
'#weight' => -99,
];
// Make sure the shortcut link is the first item in title_suffix.
$variables['title_suffix']['add_or_remove_shortcut']['#weight'] = -100;
}
}
/**
* Implements hook_preprocess_HOOK() for maintenance-page.html.twig.
*/
function jbict_preprocess_maintenance_page(&$variables) {
// By default, site_name is set to Drupal if no db connection is available
// or during site installation. Setting site_name to an empty string makes
// the site and update pages look cleaner.
// @see template_preprocess_maintenance_page
if (!$variables['db_is_active']) {
$variables['site_name'] = '';
}
// Bartik has custom styling for the maintenance page.
$variables['#attached']['library'][] = 'jbict/maintenance_page';
}
/**
* Implements hook_preprocess_HOOK() for node.html.twig.
*/
function jbict_preprocess_node(&$variables) {
// Remove the "Add new comment" link on teasers or when the comment form is
// displayed on the page.
if ($variables['teaser'] || !empty($variables['content']['comments']['comment_form'])) {
unset($variables['content']['links']['comment']['#links']['comment-add']);
}
$node = $variables['node'];
$language = \Drupal::languageManager()->getCurrentLanguage()->getId();
$owner = $node->getOwner()->getTranslation($language);
$variables['author_fullname'] = $owner->field_full_name->value;
}
/**
* Implements hook_preprocess_HOOK() for block.html.twig.
*/
function jbict_preprocess_block(&$variables) {
// Add a clearfix class to system branding blocks.
if ($variables['plugin_id'] == 'system_branding_block') {
$variables['attributes']['class'][] = 'clearfix';
}
}
/**
* Implements hook_preprocess_HOOK() for menu.html.twig.
*/
function jbict_preprocess_menu(&$variables) {
$variables['attributes']['class'][] = 'clearfix';
}
/**
* Implements hook_theme_suggestions_HOOK_alter() for form templates.
*/
function jbict_theme_suggestions_form_alter(array &$suggestions, array $variables) {
if ($variables['element']['#form_id'] == 'search_block_form') {
$suggestions[] = 'form__search_block_form';
}
}
/**
* Implements hook_form_alter().
*/
function jbict_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Add classes to the search form.
if (in_array($form_id, ['search_block_form', 'search_form'])) {
$key = ($form_id == 'search_block_form') ? 'actions' : 'basic';
if (!isset($form[$key]['submit']['#attributes'])) {
$form[$key]['submit']['#attributes'] = new Attribute();
}
$form[$key]['submit']['#attributes']->addClass('search-form__submit');
}
$form_object = $form_state->getFormObject();
// Add class to the Media Library views form.
if ($form_object instanceof ViewsForm && strpos($form_object->getBaseFormId(), 'views_form_media_library') === 0) {
// The conditional below exists because the media-library-views-form class
// is currently added by Classy, but Umami will eventually not use Classy as
// a base theme.
// @todo remove conditional, keep class addition in
// https://drupal.org/node/3110137
// @see https://www.drupal.org/node/3109287
// @see classy_form_alter()
if (!isset($form['#attributes']['class']) || !in_array('media-library-views-form', $form['#attributes']['class'])) {
$form['#attributes']['class'][] = 'media-library-views-form';
}
}
}
/**
* Implements hook_preprocess_links__media_library_menu().
*
* This targets the menu of available media types in the media library's modal
* dialog.
*
* @todo Do this in the relevant template once
* https://www.drupal.org/project/drupal/issues/3088856 is resolved.
*/
function jbict_preprocess_links__media_library_menu(array &$variables) {
foreach ($variables['links'] as &$link) {
// This conditional exists because the media-library-menu__link class is
// currently added by Classy, but Bartik will eventually not use Classy as a
// base theme.
// @todo remove conditional, keep class addition in
// https://drupal.org/node/3110137
// @see https://www.drupal.org/node/3109287
// @see classy_preprocess_links__media_library_menu()
if (!isset($link['link']['#options']['attributes']['class']) || !in_array('media-library-menu__link', $link['link']['#options']['attributes']['class'])) {
$link['link']['#options']['attributes']['class'][] = 'media-library-menu__link';
}
}
}
/**
* Implements hook_preprocess_image_widget().
*
* @todo Revisit in https://drupal.org/node/3117430
*/
function jbict_preprocess_image_widget(&$variables) {
if (!empty($variables['element']['fids']['#value'])) {
$file = reset($variables['element']['#files']);
$variables['data']["file_{$file->id()}"]['filename']['#suffix'] = ' <span class="file-size">(' . format_size($file->getSize()) . ')</span> ';
}
}
/**
* Implements template_preprocess_links().
*
* This makes it so array keys of #links items are added as a class. This
* functionality was removed in Drupal 8.1, but still necessary in some
* instances.
*
* @todo remove in https://drupal.org/node/3120962
*/
function jbict_preprocess_links(&$variables) {
if (!empty($variables['links'])) {
foreach ($variables['links'] as $key => $value) {
if (!is_numeric($key)) {
$class = Html::getClass($key);
$variables['links'][$key]['attributes']->addClass($class);
}
}
}
}
function jbict_preprocess_username(&$variables) {
$account = $variables['account'];
$language = \Drupal::languageManager()->getCurrentLanguage()->getId();
$user = $account->getTranslation($language);
$variables['fullname'] = $user->field_full_name->value;
}