-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmicons.module
executable file
·384 lines (336 loc) · 12.4 KB
/
micons.module
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
<?php
/**
* @file
* Module to associate icons with menu items
*
* @author [email protected]
*/
/**
* Implements hook_menu().
*/
function micons_menu() {
$items['admin/config/user-interface/micons'] = array(
'title' => 'Menu Icon settings',
'description' => 'Associates icons with menu items',
'page callback' => 'drupal_get_form',
'page arguments' => array('micons_admin_settings'),
'access arguments' => array('administer site configuration'),
);
return $items;
}
/**
* Implements hook_theme_registry_alter()
* Override the nodehierarchy child links function.
*
* @param $theme_registry
*/
function micons_theme_registry_alter(&$theme_registry) {
// Override the link theming functions to hide titles if so configured.
if (variable_get('micons_hide_titles', FALSE)) {
if (!empty($theme_registry['menu_link'])) {
$theme_registry['menu_link']['function'] = 'menu_icon_menu_link';
}
if (!empty($theme_registry['link'])) {
$theme_registry['link']['function'] = 'menu_icon_link';
}
}
}
/**
* Implements hook_form_alter().
*/
function micons_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'menu_edit_item') {
if (isset($form['mlid']['#value'])) {
$options = unserialize(db_query('SELECT options FROM {menu_links} WHERE mlid = :mlid', array(':mlid' => $form['mlid']['#value']))->fetchField());
}
if (!isset($options) || !isset($options['menu_icon'])) {
$options = array('menu_icon' => array('enable' => NULL, 'image_style' => NULL));
}
$form['icon'] = array(
'#type' => 'fieldset',
'#weight' => 5,
'#title' => t('Menu icon settings'),
'#description' => t('If checked, the following icon will be used as background image for this menu item.'),
'#attributes' => array('classes' => array('theme-settings-bottom')),
);
$form['icon']['use_icon_logo'] = array(
'#type' => 'checkbox',
'#title' => t('Use an icon'),
'#default_value' => $options['menu_icon']['enable'],
'#tree' => FALSE,
'#description' => t('Check this if you want this icon to be used.'),
);
$form['icon']['image_style'] = array(
'#title' => t('Image style'),
'#type' => 'select',
'#options' => image_style_options(FALSE),
'#empty_option' => '<' . t('Menu Icons default') . '>',
'#default_value' => $options['menu_icon']['image_style'],
'#description' => t('The preview image will be shown while editing the content.'),
'#required' => FALSE,
);
$form['icon']['icon_path'] = array(
'#type' => 'textfield',
'#title' => t('Path to the icon'),
'#default_value' => (isset($options['menu_icon']['path']) ? $options['menu_icon']['path'] : variable_get('micons_default_icon', drupal_get_path('module', 'micons') . '/images/default_icon.png')),
'#description' => t('The path to the image you would like to use as a background image for this menu item.'),
);
$form['icon']['icon_path']['#states'] = array(
'visible' => array (
':input[name="use_icon_logo"]' => array('checked' => TRUE),
),
);
$form['icon']['icon_upload'] = array(
'#type' => 'file',
'#title' => t('Upload a new icon image'),
'#maxlength' => 40,
'#description' => t("If you don't have direct file access to the server, use this field to upload your icon."),
);
$form['icon']['icon_upload']['#states'] = array(
'visible' => array (
':input[name="use_icon_logo"]' => array('checked' => TRUE),
),
);
$form['submit']['#weight'] = 9;
$form['delete']['#weight'] = 10;
$form['#attributes']['enctype'] = 'multipart/form-data';
$form['#submit'][] = 'micons_form_submit';
}
// Add a custom submit callback for image style forms.
if (in_array($form_id, array('image_style_form', 'image_effect_form', 'image_style_revert_form', 'image_style_delete_form', 'micons_admin_settings'))) {
$form['#submit'][] = 'micons_css_generate';
}
}
/**
* Process the submitted form
*
*/
function micons_form_submit($form, &$form_state) {
// Check the destination folder, attempt to create it if it does't exist
$directory_path = micons_directory_path();
file_prepare_directory($directory_path, FILE_CREATE_DIRECTORY);
// Store the current icon path
$path = $form_state['values']['icon_path'];
// Define the validation settings
$validate = array(
'file_validate_is_image' => array(),
);
// Check for a new uploaded icon, and use that instead.
if ($file = file_save_upload('icon_upload', $validate)) {
$parts = pathinfo($file->filename);
$filename = $directory_path . '/menu_icon_' . $form_state['values']['mlid'] . '.' . $parts['extension'];
file_unmanaged_copy($file->uri, $filename, FILE_EXISTS_REPLACE);
// Flush image style generated images
image_path_flush($filename);
$path = $filename;
}
$options = unserialize(db_query('SELECT options FROM {menu_links} WHERE mlid = :mlid', array(':mlid' => $form_state['values']['mlid']))->fetchField());
$options['menu_icon'] = array(
'enable' => $form_state['values']['use_icon_logo'],
'path' => $path,
'image_style' => $form_state['values']['image_style'],
);
// Use default image style if not explicitly set.
if (empty($options['menu_icon']['image_style'])) {
$options['menu_icon']['image_style'] = variable_get('micons_image_style_default', 'menu_icon');
}
if (!isset($options['attributes'])) {
$options['attributes'] = array();
}
if (!isset($options['attributes']['class'])) {
$options['attributes']['class'] = array();
}
$classes = array();
$classes[] = "menu_icon";
$classes[] = "menu-" . $form_state['values']['mlid'];
if ($options['menu_icon']['enable'] && !empty($options['menu_icon']['path']) && file_exists($options['menu_icon']['path'])) {
foreach ($classes as $class) {
if (!in_array($class, $options['attributes']['class'])) {
$options['attributes']['class'][] = $class;
}
}
}
if (empty($options['attributes']['class'])) {
unset($options['attributes']['class']);
}
db_update('menu_links')
->fields(array(
'options' => serialize($options),
))
->condition('mlid', $form_state['values']['mlid'])
->execute();
// Regenerate the css file
micons_css_generate();
}
/**
* Implements hook_init().
*/
function micons_init() {
drupal_add_css('public://css/micons.css');
}
/**
* Build the menu_icon's settings form
*
* @return a form array
*/
function micons_admin_settings($form, &$form_state) {
$form['micons_default_icon'] = array(
'#type' => 'textfield',
'#title' => t('Icon path'),
'#default_value' => variable_get('micons_default_icon', drupal_get_path('module', 'micons') . '/images/default_icon.png'),
'#description' => t('A Drupal path to the icon or image to use as a default.'),
'#required' => FALSE,
);
$options = array();
foreach (image_styles() as $pid => $preset) {
$options[$preset['name']] = $preset['name'];
}
if (!empty($options)) {
$form['micons_image_style_default'] = array(
'#type' => 'select',
'#title' => t('Image default style'),
'#default_value' => variable_get('micons_image_style_default', 'menu_icon'),
'#description' => t('Choose a default !link to be used for menu icons. This setting can be overwritten per menu item.', array('!link' => l(t('Image style'), 'admin/config/media/image-styles'))),
'#required' => FALSE,
'#options' => $options,
);
}
$form['micons_image_folder'] = array(
'#type' => 'textfield',
'#title' => t('Icon folder'),
'#default_value' => variable_get('micons_image_folder', 'micons'),
'#description' => t('The name of the files directory in which the new uploaded icons will be stored. This folder will be created in the files directory'),
'#required' => FALSE,
);
$form['micons_position'] = array(
'#type' => 'select',
'#title' => t('Position'),
'#default_value' => variable_get('micons_position', 'left'),
'#options' => array(
'right' => t('right'),
'left' => t('left'),
),
'#required' => FALSE,
);
$form['micons_hide_titles'] = array(
'#type' => 'checkbox',
'#title' => t('Hide menu titles if icon is present'),
'#default_value' => variable_get('micons_hide_titles', FALSE),
'#description' => t('Check this to hide menu titles and display only the icon, if an icon is configured. You will need to clear the theme registry cache after changing this option for it to take effect.'),
);
return system_settings_form($form);
}
/**
* Implements hook_image_default_styles().
* Define the default micons image style.
*/
function micons_image_default_styles() {
$styles = array();
$styles['menu_icon'] = array(
'effects' => array(
array(
'name' => 'image_scale',
'data' => array('width' => 45, 'height' => 45, 'upscale' => 1),
'weight' => 0,
),
)
);
return $styles;
}
/**
* Build CSS based on menu IDs
*
* @return A string with the CSS
*/
function micons_css_generate() {
$css = "";
$result = db_query("SELECT mlid, options FROM {menu_links}");
$pos = variable_get('micons_position', 'left');
foreach ($result as $item) {
$options = unserialize($item->options);
if (isset($options['menu_icon']) && $options['menu_icon']['enable'] && !empty($options['menu_icon']['path']) && file_exists($options['menu_icon']['path'])) {
$image_path = $options['menu_icon']['path'];
$image_style = (isset($options['menu_icon']['image_style']) && !empty($options['menu_icon']['image_style'])) ? $options['menu_icon']['image_style'] : NULL;
if ($image_style) {
$source_uri = $image_path;
$image_path = image_style_path($image_style, $source_uri);
if (!file_exists($image_path)) {
image_style_create_derivative(image_style_load($image_style), $source_uri, $image_path);
}
}
// Retrieve the image dimensions
$info = image_get_info($image_path);
$image_url = file_create_url($image_path);
// Support private filesystem
$css .= theme('micons_css_item', array('mlid' => $item->mlid, 'path' => $image_url, 'size' => $info['width'], 'pos' => $pos));
}
}
if (!empty($css)) {
$csspath = 'public://css';
file_prepare_directory($csspath, FILE_CREATE_DIRECTORY);
file_unmanaged_save_data($css, $csspath . '/micons.css', FILE_EXISTS_REPLACE);
}
}
/**
* Implements hook_theme().
*/
function micons_theme() {
return array(
'micons_css_item' => array(
'variables' => array('mlid' => NULL, 'path' => NULL, 'size' => NULL, 'pos' => NULL),
'template' => 'micons_css_item',
),
);
}
/**
* Implements hook_flush_caches().
*/
function micons_flush_caches() {
micons_css_generate();
}
/**
* Returns the file directory path in which both the CSS file and the icons are stored.
*/
function micons_directory_path($full = TRUE) {
$path = variable_get('micons_image_folder', 'micons');
$path_full = 'public://' . $path;
return ($full ? $path_full : $path);
}
/**
* Override theme_menu_link - hide link titles if enabled.
*
* @param $variables
*
* @return string
*/
function menu_icon_menu_link($variables) {
$element = $variables['element'];
$sub_menu = '';
if ($element['#below']) {
$sub_menu = drupal_render($element['#below']);
}
$output = l($element['#title'], $element['#href'], $element['#localized_options']);
if (isset($element['#localized_options']['menu_icon'])) {
if ($element['#localized_options']['menu_icon']['enable'] == 1) {
$element['#attributes']['title'] = $element['#title'];
$output = l('', $element['#href'], $element['#localized_options']);
}
}
return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";
}
/**
* Override theme_link - hide link titles if enabled.
*
* @param $variables
*
* @return string
*/
function menu_icon_link($variables) {
if (isset($variables['options']['menu_icon'])) {
if ($variables['options']['menu_icon']['enable'] == 1) {
$variables['options']['attributes']['title'] = $variables['text'];
return '<a href="' . check_plain(url($variables['path'], $variables['options'])) . '"' . drupal_attributes($variables['options']['attributes']) . '></a>';
}
}
return '<a href="' . check_plain(url($variables['path'], $variables['options'])) . '"' . drupal_attributes($variables['options']['attributes']) . '>' . ($variables['options']['html'] ? $variables['text'] : check_plain($variables['text'])) . '</a>';
}