/
template.php
259 lines (218 loc) · 7.46 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
<?php
/**
* @file
* Template overrides as well as (pre-)process and alter hooks for the
* phoebe theme.
*/
// Menu styles.
define('PHOEBE_MENU_ICON', 'icon');
define('PHOEBE_MENU_ICON_TEXT', 'icon text');
/**
* Implements hook_form_comment_form_alter().
*/
function phoebe_form_comment_form_alter(&$form, &$form_state) {
$form['author']['#access'] = FALSE;
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function phoebe_form_comment_confirm_delete_alter(&$form, &$form_state) {
// Add "button" class to cancel link.
$form['actions']['cancel']['#options']['attributes']['class'][] = 'button';
}
/**
* Implements hook_block_view_alter().
*/
function phoebe_block_view_alter(&$data, $block) {
// Footer: Footer menu
if ($block->module == 'menu' && $block->delta == 'menu-footer-menu') {
phoebe_icon_prepare_menu($data['content'], PHOEBE_MENU_ICON, 'light-sky', 'white');
}
}
/**
* Implements hook_js_alter().
*/
function phoebe_js_alter(&$js) {
global $theme;
$theme_path = path_to_theme();
// Get max weight.
$max_weight = 0;
foreach ($js as $key => $value) {
if (!empty($value['weight']) && $value['weight'] > $max_weight) {
$max_weight = $value['weight'];
}
}
// Move the main script to the footer. This script includes the config, which
// should load after Drupal.settings has been set in the header.
$search = array(
$theme_path . '/js/' . $theme . '.js',
$theme_path . '/js/' . $theme . '.min.js'
);
foreach ($js as $key => $value) {
if (in_array($key, $search)) {
$js[$key]['scope'] = 'footer';
$js[$key]['weight'] = ++$max_weight;
}
}
}
/**
*
* @param array $opts
* @return string
*/
function phoebe_icon(array $opts) {
$themes = list_themes();
$colors = $themes['phoebe']->info['settings']['icon']['colors'];
$prefix = $themes['phoebe']->info['settings']['icon']['prefix'];
$size = $themes['phoebe']->info['settings']['icon']['size'];
if (empty($opts['color'])) {
$opts['color'] = NULL;
}
$default_icon = array(
'class_name' => 'default' . ($opts['color'] ? ' ' . $opts['color'] : ''),
'id' => $opts['id'],
'color' => $opts['color'],
'fill' => empty($colors[$opts['color']]) ? NULL : $colors[$opts['color']],
'width' => empty($opts['width']) ? $size : (int) $opts['width'],
'height' => empty($opts['height']) ? $size : (int) $opts['height'],
);
$hover_icon = NULL;
$has_hover = !empty($opts['hover_id']) || !empty($opts['hover_color']);
if ($has_hover) {
if (empty($opts['hover_color'])) {
$opts['hover_color'] = $opts['color'];
}
$hover_icon = array(
'class_name' => 'hover' . ($opts['hover_color'] ? ' ' . $opts['hover_color'] : ''),
'id' => empty($opts['hover_id']) ? $opts['id'] : $opts['hover_id'],
'color' => $opts['hover_color'],
'fill' => empty($colors[$opts['hover_color']]) ? NULL : $colors[$opts['hover_color']],
'width' => $default_icon['width'],
'height' => $default_icon['height'],
);
}
$attr = array(
'class' => $prefix . ' ' . $prefix . '-' . $opts['id'] . ($has_hover ? ' has-hover' : '') . (isset($opts['class']) ? ' ' . $opts['class'] : ''),
'title' => isset($opts['title']) ? $opts['title'] : '',
'aria-hidden' => 'true',
'draggable' => 'false',
'ondragstart' => 'return false;',
);
// Use padding-bottom hack on container to preserve aspect ratio.
// @see https://css-tricks.com/scale-svg
$ratio = ($default_icon['height'] / $default_icon['width'] * 10000) / 100;
if ($ratio !== 100) {
$attr['style'] = 'height: 0; padding: 0 0 ' . $ratio . '% 0;';
}
$html = '<span' . drupal_attributes($attr) . '>';
$html .= _phoebe_icon_svg_symbol($default_icon);
if (isset($hover_icon)) {
$html .= _phoebe_icon_svg_symbol($hover_icon);
}
$html .= '</span>';
if (isset($opts['title'])) {
$html .= '<span class="element-invisible">' . $opts['title'] . '</span>';
}
return $html;
}
/**
* Prepares a menu tree for rendering with icons.
*
* @param array $menu
* @param string $style PHOEBE_MENU_* constant.
* @param string $color
* @param string $hover_color
*/
function phoebe_icon_prepare_menu(&$menu, $style, $color, $hover_color = NULL) {
foreach ($menu as $delta => $link) {
if (is_numeric($delta)) {
reset($link['#original_link']['options']['attributes']['class']);
$id = current($link['#original_link']['options']['attributes']['class']);
$clone = (object) $link;
$key = '#title';
$title = check_plain($clone->{$key});
if ($style === PHOEBE_MENU_ICON) {
$menu[$delta]['#localized_options']['attributes']['title'] = $title;
$menu[$delta]['#localized_options']['html'] = TRUE;
$menu[$delta]['#title'] = phoebe_icon(array(
'id' => substr($id, 5),
'color' => $color,
'hover_color' => $hover_color,
'title' => $title,
));
}
elseif ($style === PHOEBE_MENU_ICON_TEXT) {
$menu[$delta]['#localized_options']['html'] = TRUE;
$menu[$delta]['#title'] = phoebe_icon(array(
'id' => substr($id, 5),
'color' => $color,
'hover_color' => $hover_color,
)) . '<span class="text">' . $title . '</span>';
}
}
}
}
/**
*
* @param array $opts
* @return string
*/
function _phoebe_icon_svg_symbol(array $opts) {
$themes = list_themes();
$prefix = $themes['phoebe']->info['settings']['icon']['prefix'];
return '<svg class="' . $opts['class_name'] . '"' . ($opts['fill'] ? ' fill="' . $opts['fill'] . '"' : '') . ' preserveAspectRatio="xMidYMid meet" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><use xlink:href="#' . $prefix . '-' . $opts['id'] . '"></use></svg>';
}
/**
*
* @param array $opts
* @return string
*/
function _phoebe_icon_svg(array $opts) {
static $svg;
static $symbols;
if (!isset($svg)) {
$theme_path = drupal_get_path('theme', 'phoebe');
$svg_path = $theme_path . '/svg/icon.svg';
if (file_exists($svg_path)) {
$data = file_get_contents($svg_path);
$svg = new SimpleXMLElement($data);
$svg->registerXPathNamespace('svg', 'http://www.w3.org/2000/svg');
$svg->registerXPathNamespace('xlink', 'http://www.w3.org/1999/xlink');
}
else {
$svg = FALSE;
}
}
if (!isset($symbols)) {
$symbols = array();
}
if ($svg === FALSE) {
return '';
}
$themes = list_themes();
$prefix = $themes['phoebe']->info['settings']['icon']['prefix'];
if (!isset($symbols[$opts['id']])) {
$result = $svg->xpath("//svg:symbol[@id='" . $prefix . "-" . $opts['id'] . "']");
if (empty($result)) {
$symbols[$opts['id']] = FALSE;
}
else {
$view_box = '0 0 ' . $opts['width'] . ' ' . $opts['height'];
if (isset($result[0]->attributes()->viewBox)) {
$view_box = (string) $result[0]->attributes()->viewBox;
}
$children = '';
foreach ($result[0]->children() as $child) {
$children .= $child->asXML();
}
$symbols[$opts['id']] = array(
'view_box' => $view_box,
'children' => $children,
);
}
}
if (empty($symbols[$opts['id']])) {
return '';
}
return '<svg class="' . $opts['class_name'] . '"' . ($opts['fill'] ? ' fill="' . $opts['fill'] . '"' : '') . ' viewBox="' . $symbols[$opts['id']]['view_box'] . '" preserveAspectRatio="xMidYMid meet" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">' . $symbols[$opts['id']]['children'] . '</svg>';
}