forked from woeldiche/domination_tools
/
template.php
536 lines (464 loc) · 14.3 KB
/
template.php
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
<?php
/**
* A toolbox of functions to help you theme Drupal sites.
*
* Most of these functions are intended to inject classes for styling into
* your Drupal site to allow for a more object oriented approach to CSS.
*
* Secondly the functions expand on Drupal core's template suggestions and
* give examples of how to insert custom template suggestions to streamline
* the number of tpl.php-files in your theme.
*
* - Replace any instance of THEMENAME with the system name of the theme.
* - Replace any instance of MENU_NAME or FORMID with the system name of the
* menu or formID of the form.
* - Adapt, remove or combine conditions as your design dictates.
* - Remove all unneeded code.
*
* Fork this on Github.com/woeldiche/domination-tools.
*/
/**
* Expand Drupal's standard template suggestions.
*
* Some options are:
* - For page.tpl.php:
* - Node type: $vars['node']->type
* - User role: $vars['user']['roles']
* - For node.tpl.php:
* - View mode: $vars['view_mode']
* - Node type: $vars['type']
*/
function THEMENAME_preprocess(&$vars, $hook) {
// Add template suggestions for page.tpl.php.
if ($hook == 'page') {
// Check if the page has a node type and add template suggestion.
if (isset($vars['node']->type)) {
// Pattern: page--node--[node_type].tpl.php for node pages.
$vars['theme_hook_suggestions'][] = 'page__node__' . $vars['node']->type;
}
}
// Add template suggestions for node.tpl.php.
if ($hook == 'node') {
// Pattern: node--[view mode].tpl.php including custom view modes.
$vars['theme_hook_suggestions'][] = 'node__' . $vars['view_mode'];
// Pattern: node--[node type]--[view mode].tpl.php.
$vars['theme_hook_suggestions'][] = 'node__' . $vars['type'] . '__' . $vars['view_mode'];
}
// Add template suggestions for block.tpl.php.
if ($hook == 'block') {
// Add theme suggestion based module.
switch($vars['elements']['#block']->module) {
case 'menu':
case 'menu_block':
$vars['theme_hook_suggestions'][] = 'block__navigation';
break;
// Render some blocks without wrapper and leave it to the module.
case 'views':
case 'mini_panels':
$vars['theme_hook_suggestions'][] = 'block__nowrapper';
}
}
}
/**
* Implements template_preprocess_html().
*
* Adds classes to <body> based on path.
*/
function THEMENAME_preprocess_html(&$vars) {
// Get the current path and break it into sections.
$parts = explode('/', drupal_get_path_alias());
// Add classes to body based on first section of path.
switch ($parts[0]) {
case 'path_foo':
$vars['classes_array'][] = 'section-foo';
break;
case 'path_bar':
case 'path_baz':
$vars['classes_array'][] = 'section-bar';
break;
default:
$vars['classes_array'][] = 'section-baz';
break;
}
// Add classes based on combined first and second section.
if (count($parts) >= 2) {
switch ($parts[0] . '-' . $parts[1]) {
case 'path_foo-list':
case 'path_baz-list':
$vars['classes_array'][] = 'list-page';
break;
}
}
}
/**
* Sets variable that tells the page if it is being rendered in the overlay.
*
* Allows you to hide eg. sidebars in the overlay.
*
* Usage in page.tpl.php:
* <php? if ($region_name && !$in_overlay): ?>
* <?php print render($region_name); ?>
* <?php endif; ?>
*/
function THEMENAME_preprocess_page(&$vars) {
if (module_exists('overlay')) {
if (overlay_get_mode() == 'child') {
$vars['in_overlay'] = TRUE;
} else {
$vars['in_overlay'] = FALSE;
}
}
}
/**
* Implements template_preprocess_block().
*
* Adds classes for styling.
*
* Good options are:
* - Block name: $vars['elements']['#block']->bid.
* - Module: $vars['elements']['#block']->module.
* - Region: $vars['elements']['#block']->region.
*/
function THEMENAME_preprocess_block(&$vars, $hook) {
/**
* Add classes to blocks created by Views based on views name.
*/
// Check if block was created by views.
if ($vars['elements']['#block']->module == 'views') {
// Get views name from $vars['elements']['#block']->delta.
$block_delta = explode('-', $vars['elements']['#block']->delta);
$views_name = $block_delta[0];
// Add classes based on views name.
switch ($views_name) {
case 'view_foo':
case 'view_bar':
$vars['title_attributes_array']['class'][] = 'title-list';
break;
case 'view_baz':
$vars['title_attributes_array']['class'] = 'title-block';
$vars['classes_array'][] = 'block-secondary';
break;
default:
$vars['title_attributes_array']['class'][] = 'title-block';
}
}
/**
* Add classes based on region.
*/
switch ($vars['elements']['#block']->region) {
case 'region_foo':
case 'region_bar':
case 'region_baz':
$vars['title_attributes_array']['class'][] = 'title-list';
break;
case 'region_foobar':
$vars['classes_array'][] = 'block-list';
break;
default;
}
/*
* Add classes based on module excluding certain regions.
*/
switch ($vars['elements']['#block']->region) {
// Exclude certain blocks in certain regions.
case 'footer_sitemap':
case 'user_first':
case 'user_second':
case 'menu':
case 'footer_first':
case 'footer_second':
// Do nothing.
break;
default:
switch($vars['elements']['#block']->module) {
// For the rest of the regions add classes to navigation blocks.
case 'menu':
case 'menu_block':
$vars['attributes_array']['class'][] = 'block-style-menu';
break;
// And style standard blocks.
case 'block':
$vars['attributes_array']['class'][] = 'block-secondary';
break;
}
}
}
/**
* Implements hook_preprocess_node.
*
* Add styling classes based on content type.
*
* Good options are:
* - View Mode: $vars['view_mode']
* - Content type: $vars['type']
*/
function THEMENAME_preprocess_node(&$vars) {
// Add classes based on node type.
switch ($vars['type']) {
case 'news':
case 'pages':
$vars['attributes_array']['class'][] = 'content-wrapper';
$vars['attributes_array']['class'][] = 'text-content';
break;
}
// Add classes & theme hook suggestions based on view mode.
switch ($vars['view_mode']) {
case 'block_display':
$vars['theme_hook_suggestions'][] = 'node__aside';
$vars['title_attributes_array']['class'] = array('title-block');
$vars['attributes_array']['class'][] = 'block-content';
$vars['attributes_array']['class'][] = 'st-spot';
$vars['attributes_array']['class'][] = 'vgrid';
$vars['attributes_array']['class'][] = 'clearfix';
break;
}
}
/**
* Implements template_preprocess_field().
*
* Adds classes to field based on field name.
*
* Good options are:
* - Field name: $vars['element']['#field_name'].
* - Content type: $vars['element']['#bundle'].
* - View mode: $vars['element']['#view_mode'].
*/
function THEMENAME_preprocess_field(&$vars,$hook) {
// add class to a specific fields across content types.
switch ($vars['element']['#field_name']) {
case 'body':
$vars['classes_array'][] = 'text-content';
break;
case 'field_summary':
$vars['classes_array'][] = 'text-teaser';
break;
case 'field_location':
case 'field_date':
case 'field_price':
case 'field_deadline':
case 'field_website':
case 'field_organizer':
case 'field_contact_information':
// Replace classes entirely, instead of adding extra.
$vars['classes_array'] = array('list-definition', 'text-content');
break;
case 'field_image':
// Replace classes entirely, instead of adding extra.
$vars['classes_array'] = array('title-image');
break;
default:
break;
}
// Add classes to body based on content type and view mode.
if ($vars['element']['#field_name'] = 'body') {
// Add classes to Foobar content type.
if ($vars['element']['#bundle'] == 'foobar') {
$vars['classes_array'][] = 'text-secondary';
}
// Add classes to other content types with view mode 'teaser';
elseif ($vars['element']['#view_mode'] == 'teaser') {
$vars['classes_array'][] = 'text-secondary';
}
// The rest is text-content.
else {
$vars['classes_array'][] = 'text-content';
}
}
}
/**
* Implements template_preprocess_views_view().
*
* Adds styling classes to views.
* Adds custom template suggestions.
*/
function THEMENAME_preprocess_views_view(&$vars) {
/**
* Add custom template suggestions to specific views.
*/
switch ($vars['view']->name) {
case 'view_foo':
case 'view_bar':
case 'view_baz':
$vars['theme_hook_suggestions'][] = 'views_view__no_wrapper';
break;
}
/**
* Add alternating classes to View Foo based on offset.
*/
if ($vars['view']->name == 'view_foo') {
switch ($vars['view']->offset) {
case 0:
break;
case 1:
$vars['classes_array'][] = 'st-magenta';
break;
case 2:
$vars['classes_array'][] = 'st-yellow';
break;
case 3:
$vars['classes_array'][] = 'st-petroleum';
break;
// Set same style on the rest.
default:
$vars['classes_array'][] = 'st-default';
break;
}
}
}
/**
* Implements template_preprocess_views_views_fields().
*
* Shows/hides summary on tiles based on presence of images.
*/
function THEMENAME_preprocess_views_view_fields(&$vars) {
if ($vars['view']->name == 'nodequeue_1') {
// Check if we have both an image and a summary
if (isset($vars['fields']['field_image'])) {
// If a combined field has been created, unset it and just show image
if (isset($vars['fields']['nothing'])) {
unset($vars['fields']['nothing']);
}
} elseif (isset($vars['fields']['title'])) {
unset ($vars['fields']['title']);
}
// Always unset the separate summary if set
if (isset($vars['fields']['field_summary'])) {
unset($vars['fields']['field_summary']);
}
}
}
/**
* Implements template_preprocess_panels_pane().
*
* Adds classes for styling.
*/
function THEMENAME_preprocess_panels_pane(&$vars) {
/**
* Add styling classes to labels/pane-titles for fields as panes.
*/
if ($vars['pane']->type == 'entity_field') {
switch ($vars['content']['#field_name']) {
case 'field_location':
case 'field_date':
case 'field_price':
case 'field_deadline':
case 'field_website':
case 'field_organizer':
case 'field_contact_information':
$vars['title_attributes_array']['class'] = array('list-key');
}
}
/**
* add classes to classes to labels/pane-titles for views panes.
*/
if ($vars['pane']->type == 'views_panes') {
// First add classes based on display
switch ($vars['pane']->subtype) {
case 'display_name_foo':
$vars['title_attributes_array']['class'][] = 'content-footer-title';
$vars['title_attributes_array']['class'][] = 'text-secondary';
break;
case 'display_name_bar':
case 'display_name_baz':
$vars['title_attributes_array']['class'][] = 'title-field';
}
}
/**
* Add classes to labels/pane-titles based on location.
*/
switch ($vars['pane']->panel) {
case 'outer_right':
$vars['title_attributes_array']['class'][] = 'title-block';
break;
}
// Suggestions base on sub-type.
$vars['theme_hook_suggestions'][] = 'panels_pane__' . str_replace('-', '__', $vars['pane']->subtype);
// Suggestions on panel pane.
$vars['theme_hook_suggestions'][] = 'panels_pane__' . $vars['pane']->panel;
}
/**
* Implements theme_menu_tree().
*
* Adds classes to all menu wrappers.
*/
function THEMENAME_menu_tree($vars) {
return '<ul class="menu">' . $vars['tree'] . '</ul>';
}
/**
* Implements theme_menu_tree().
*
* Adds additional wrapper classes for specific menu.
*/
function THEMENAME_menu_tree__MENU_NAME($vars) {
return '<ul class="menu vertical-menu">' . $vars['tree'] . '</ul>';
}
/**
* Implements hook_form_FORMID_alter().
*
* Adds classes to items on specific form.
*/
function THEMENAME_form_FORMID_alter(&$form) {
// Add classes to submit button.
$form['actions']['submit']['#attributes']['class'][] = 'button';
$form['actions']['submit']['#attributes']['class'][] = 'submit';
// Add classes to form items base on their type by walking through the form
// array mapping item types to classes.
array_walk($form, 'THEMENAME_form_walker', array(
'submit' => array(
'button',
'submit',
),
'textfield' => array('text-secondary'),
'textarea' => array('text-secondary'),
'password' => array('password'),
'foo' => array('foo'),
));
}
/**
* Implements hook_form_alter().
*
* Adds classes to items all forms based on item type.
*/
function THEMENAME_form_alter(&$form, &$form_state, $form_id) {
// Add classes to submit button.
$form['actions']['submit']['#attributes']['class'][] = 'button';
$form['actions']['submit']['#attributes']['class'][] = 'submit';
// Add classes to form items base on their type by walking through the form
// array mapping item types to classes.
array_walk($form, 'THEMENAME_form_walker', array(
'submit' => array(
'button',
'submit',
),
'textfield' => array('text-secondary'),
'textarea' => array('text-secondary'),
'password' => array('password'),
'foo' => array('foo'),
));
}
/**
* Form walker which addes classes to the array elements based on item types.
*
* @param type $item
* @param type $key
* @param type $map
*/
function THEMENAME_form_walker(&$item, &$key, $map) {
// If the item is an array and have the "#type" key it has to be a form item.
if (is_array($item) && isset($item['#type'])) {
// Check if "map" have the type defined, if not set the default class(es).
$classes = isset($map[$item['#type']]) ? $map[$item['#type']] : $map['default'];
// Check that the class attribute have been sat. If not create the class
// array.
if (isset($item['#attributes']['class'])) {
$item['#attributes']['class'] += $classes;
}
else {
$item['#attributes'] = array('class' => $classes);
}
// If the type is a fieldset walk that to add classes to its form items.
if ($item['#type'] == 'fieldset') {
array_walk($item, 'THEMENAME_form_walker');
}
}
}