Fixes some PAReview issues.
[project/rijkshuisstijl.git] / functions / menu.php
1 <?php
2 /**
3 * @file
4 * Contains all functionality for theming menus.
5 */
6
7 /*
8 * Implements theme_menu_link().
9 *
10 * Hook to recognize external URLs and add class 'external'.
11 */
12 function rijkshuisstijl_menu_link($variables) {
13 $link = &$variables['element'];
14
15 // Unset all the classes.
16 unset($variables['element']['#attributes']['class']);
17
18 $element = $variables['element'];
19 $sub_menu = '';
20
21 if ($element['#below']) {
22 $sub_menu = drupal_render($element['#below']);
23 }
24
25 $output = l($element['#title'], $element['#href'], $element['#localized_options']);
26 return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";
27 }
28
29 /**
30 * Implements theme_menu_link__menu_block().
31 */
32 function rijkshuisstijl_menu_link__menu_block($variables) {
33 $attributes = $variables['element']['#attributes'];
34
35 $element = $variables['element'];
36 $element['#attributes']['class'] = array();
37
38 if (in_array('first', $attributes['class'])) {
39 $element['#attributes']['class'][] = 'first-item';
40 }
41
42 if (in_array('active', $attributes['class'])) {
43 $element['#attributes']['class'][] = 'active';
44 }
45
46 $sub_menu = '';
47 if ($element['#below']) {
48 $sub_menu = drupal_render($element['#below']);
49 $element['#attributes']['class'][] = 'open';
50 }
51 elseif ($element['#below'] == FALSE && in_array('active-trail', $attributes['class'])) {
52 $element['#attributes']['class'][] = 'active';
53 }
54
55 $output = l($element['#title'], $element['#href'], array_merge($element['#localized_options'], array('html' => TRUE)));
56 return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";
57 }
58
59 /**
60 * Implements theme_links().
61 */
62 function rijkshuisstijl_links__system_main_menu($variables) {
63 $links = $variables['links'];
64 $attributes = $variables['attributes'];
65 $heading = $variables['heading'];
66 global $language_url;
67 $output = '';
68
69 if (count($links) > 0) {
70 $output = '';
71
72 // Treat the heading first if it is present to prepend it to the
73 // list of links.
74 if (!empty($heading)) {
75 if (is_string($heading)) {
76 // Prepare the array that will be used when the passed heading
77 // is a string.
78 $heading = array(
79 'text' => $heading,
80 // Set the default level of the heading.
81 'level' => 'h2',
82 );
83 }
84
85 $output .= '<' . $heading['level'];
86 if (!empty($heading['class'])) {
87 $output .= drupal_attributes(array('class' => $heading['class']));
88 }
89
90 $output .= '>' . check_plain($heading['text']) . '</' . $heading['level'] . '>';
91 }
92
93 $output .= '<ul' . drupal_attributes($attributes) . '>';
94
95 $num_links = count($links);
96 $i = 1;
97
98 foreach ($links as $key => $link) {
99 $class = array($key);
100
101 // Add first, last and active classes to the list of links
102 // to help out themers.
103 if ($i == 1) {
104 $class[] = 'first-item';
105 }
106
107 if ($i == $num_links) {
108 $class[] = 'last-item';
109 }
110
111 if (isset($link['href']) && ($link['href'] == $_GET['q'] || ($link['href'] == '<front>' && drupal_is_front_page())) && (empty($link['language']) || $link['language']->language == $language_url->language)) {
112 $class[] = 'sel';
113 }
114
115 if (strstr($key, 'active-trail') !== FALSE && !in_array('sel', $class)) {
116 $class[] = 'sel';
117 }
118
119 $output .= '<li' . drupal_attributes(array('class' => $class)) . '>';
120
121 if (isset($link['href']) && !empty($link['title'])) {
122 // Pass in $link as $options, they share the same keys.
123 $output .= l($link['title'], $link['href'], array_merge($link, array('html' => TRUE)));
124 }
125 elseif (!empty($link['title'])) {
126 // Some links are actually not links, but we wrap these in <span>
127 // for adding title and class attributes.
128 if (empty($link['html'])) {
129 $link['title'] = check_plain($link['title']);
130 }
131
132 $span_attributes = '';
133 if (isset($link['attributes'])) {
134 $span_attributes = drupal_attributes($link['attributes']);
135 }
136
137 $output .= '<span' . $span_attributes . '>' . $link['title'] . '</span>';
138 }
139
140 $i++;
141 $output .= "</li>\n";
142 }
143
144 $output .= '</ul>';
145 }
146
147 return $output;
148 }
149
150 /**
151 * Implements theme_menu_local_tasks().
152 */
153 function rijkshuisstijl_menu_local_tasks(&$variables) {
154 $output = '';
155
156 if (!empty($variables['primary'])) {
157 // $variables['primary']['#prefix'] = '<h2 class="element-invisible">' . t('Primary tabs') . '</h2>';
158 $variables['primary']['#prefix'] = '<ul class="tabs primary">';
159 $variables['primary']['#suffix'] = '</ul>';
160 $output .= drupal_render($variables['primary']);
161 }
162
163 if (!empty($variables['secondary'])) {
164 // $variables['secondary']['#prefix'] = '<h2 class="element-invisible">' . t('Secondary tabs') . '</h2>';
165 $variables['secondary']['#prefix'] = '<ul class="tabs secondary">';
166 $variables['secondary']['#suffix'] = '</ul>';
167 $output .= drupal_render($variables['secondary']);
168 }
169
170 if (drupal_strlen($output) > 0) {
171 $output = '<div class="tabs">' . $output . '</div>';
172 }
173
174 return $output;
175 }