forked from clarin-eric/clarin-drupal-bootstrap-theme
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclarin_bootstrap.theme
106 lines (92 loc) · 2.64 KB
/
clarin_bootstrap.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
<?php
/**
* @file
* Functions to support theming in the SASS Starterkit subtheme.
*/
/**
* Implements template_preprocess_views_view()
* @param array $vars
*/
function clarin_bootstrap_preprocess_views_view(&$vars){
$view = $vars['view'];
$vars['more']['#options']['attributes']['class'] = array(
'btn-link-blue'
);
}
/**
* Implements hook_preprocess_paragraph().
*/
function clarin_bootstrap_preprocess_node(&$variables)
{
$node = $variables['node'];
if ($node->hasField('field_show_toc') && $node->hasField('body')) {
if ($node->get('field_show_toc')->value == 1) {
$variables['toc'] = clarin_bootstrap_function_generate_navigation($node->get('body')->value);
$variables['#attached']['library'][] = 'clarin_bootstrap/toc';
}
}
if ($node->hasField('field_see_also_links')) {
if (count($node->get('field_see_also_links')->getValue()) > 0) {
$variables['more_links'] = 1;
}
}
}
function clarin_bootstrap_function_generate_navigation($HTML) {
$DOM = new DOMDocument();
$DOM->loadHTML($HTML);
$navigation = '<ul>';
// Iterating through all elements
$h2IteratorStatus = 0; //0-closed, 1-open
$h3IteratorStatus = 0; //0-closed, 1-open
$h4IteratorStatus = 0; //0-closed, 1-open
$h2i = 0;
$h3i = 0;
$h4i = 0;
foreach($DOM->getElementsByTagName('*') as $element) {
if($element->tagName == 'h2') {
if($h3IteratorStatus){
//it's open, need to close
$navigation .= '</ul>';
$h3IteratorStatus = 0;
}
if($h2IteratorStatus){
//it's open, need to close
$navigation .= '</li>';
$h2IteratorStatus = 0;
}
$h2IteratorStatus = 1;
$navigation .= '<li><a href="#heading-2-' . $h2i . '">' . $element->textContent .'</a></li>';
$h2i++;
} else if($element->tagName == 'h3') {
if(!$h3IteratorStatus){
$navigation .= '<ul>';
$h2IteratorStatus = 1;
}
$h3IteratorStatus = 1;
$navigation .= '<li><a href="#heading-3-' . $h3i . '">' . $element->textContent .'</a></li>';
$h3i++;
} else if ($element->tagName == 'h4') {
if(!$h4IteratorStatus){
$navigation .= '<ul>';
$h3IteratorStatus = 1;
}
$h4IteratorStatus = 1;
$navigation .= '<li><a href="#heading-4-' . $h4i . '">' . $element->textContent .'</a></li>';
$h4i++;
}
}
//check for last opened h3
if($h4IteratorStatus){
$navigation .= '</ul>';
}
//check for last opened h3
if($h3IteratorStatus){
$navigation .= '</ul>';
}
//check for last opened h2
if($h2IteratorStatus){
//it's open, need to close
$navigation .= '</li>';
}
return $navigation.'</ul>';
}