/
template.php
188 lines (159 loc) · 5 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
<?php
/**
* Implements template_preprocess_page.
*
* Add convenience variables and template suggestions.
*/
function mdl_preprocess_page(&$variables) {
//dpm($variables);
$links = menu_tree_output(menu_tree_all_data(variable_get('menu_secondary_links_source', 'user-menu')));
//dpm($links);
// Top bar menus.
$variables['top_bar_main_menu'] = '';
if (!empty($variables['main_menu'])) {
$variables['top_bar_main_menu'] = theme('links__topbar_main_menu', array(
'links' => $variables['main_menu'],
'attributes' => array(
'id' => 'main-menu',
'class' => array('mdl-navigation'),
),
));
}
$variables['top_bar_secondary_menu'] = '';
if (!empty($variables['secondary_menu'])) {
$variables['top_bar_secondary_menu'] = theme('links__topbar_secondary_menu', array(
'links' => $variables['secondary_menu'],
'attributes' => array(
'id' => 'secondary-menu',
'class' => array('mdl-navigation'),
),
));
}
$variables['linked_logo'] = '';
if (!empty($variables['logo_img'])) {
$variables['linked_logo'] = l($variables['logo_img'], '<front>', array(
'attributes' => array(
'rel' => 'home',
'title' => strip_tags($variables['site_name']) . ' ' . t('Home'),
),
'html' => TRUE,
));
}
$variables['linked_site_name'] = '';
if (!empty($variables['site_name'])) {
$variables['linked_site_name'] = l($variables['site_name'], '<front>', array(
'attributes' => array(
'rel' => 'home',
'title' => strip_tags($variables['site_name']) . ' ' . t('Home'),
),
'html' => TRUE,
));
}
}
/**
* Implements theme_links() targeting the main menu topbar.
*/
function mdl_links__topbar_main_menu($variables) {
// We need to fetch the links ourselves because we need the entire tree.
$links = menu_tree_output(menu_tree_all_data(variable_get('menu_main_links_source', 'main-menu')));
$output = _mdl_links($links);
return '<nav' . drupal_attributes($variables['attributes']) . '>' . $output . '</nav>';
}
/**
* Implements theme_links() targeting the secondary menu topbar.
*/
function mdl_links__topbar_secondary_menu($variables) {
// We need to fetch the links ourselves because we need the entire tree.
$links = menu_tree_output(menu_tree_all_data(variable_get('menu_secondary_links_source', 'user-menu')));
$output = _mdl_links($links);
return '<nav' . drupal_attributes($variables['attributes']) . '>' . $output . '</nav>';
}
/**
* Helper function to output a Drupal menu as a Foundation Top Bar.
*
* @links array
* An array of menu links.
*
* @return string
* A rendered list of links, with no <ul> or <ol> wrapper.
*
* @see mdl_links__system_main_menu()
* @see mdl_links__system_secondary_menu()
*/
function _mdl_links($links) {
$output = '';
foreach (element_children($links) as $key) {
$output .= _mdl_render_link($links[$key]);
}
return $output;
}
/**
* Helper function to recursively render sub-menus.
*
* @link array
* An array of menu links.
*
* @return string
* A rendered list of links, with no <ul> or <ol> wrapper.
*
* @see _mdl_links()
*/
function _mdl_render_link($link) {
$output = '';
// This is a duplicate link that won't get the dropdown class and will only
// show up in small-screen.
$small_link = $link;
if (!empty($link['#below'])) {
$link['#attributes']['class'][] = 'has-dropdown';
}
// Render top level and make sure we have an actual link.
if (!empty($link['#href'])) {
$rendered_link = NULL;
// Foundation offers some of the same functionality as Special Menu Items;
// ie: Dividers and Labels in the top bar. So let's make sure that we
// render them the Foundation way.
if (module_exists('special_menu_items')) {
if ($link['#href'] === '<nolink>') {
$rendered_link = '<label>' . $link['#title'] . '</label>';
}
else {
if ($link['#href'] === '<separator>') {
$link['#attributes']['class'][] = 'divider';
$rendered_link = '';
}
}
}
if (!isset($rendered_link)) {
$rendered_link = theme('mdl_menu_link', array('link' => $link));
}
// Test for localization options and apply them if they exist.
if (isset($link['#localized_options']['attributes']) && is_array($link['#localized_options']['attributes'])) {
$link['#attributes'] = array_merge_recursive($link['#attributes'], $link['#localized_options']['attributes']);
}
$output .= $rendered_link;
}
return $output;
}
/**
* Theme function to render a single top bar menu link.
*/
function theme_mdl_menu_link($variables) {
$link = $variables['link'];
$options = array(
'attributes' => array(
'class' => array('mdl-navigation__link'),
),
);
return l($link['#title'], $link['#href'], $options);
}
/**
* Implements hook_theme().
*/
function mdl_theme() {
$return = array();
$return['mdl_menu_link'] = array(
'variables' => array('link' => NULL),
'function' => 'theme_mdl_menu_link',
);
return $return;
}