menu_attributes.install
2.98 KB
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
<?php
/**
* @file
* Install, update and uninstall functions for the menu_attributes module.
*/
/**
* Implements hook_install().
*/
function menu_attributes_install() {
db_update('system')
->fields(array(
'weight' => 10,
))
->condition('type', 'module')
->condition('name', 'menu_attributes')
->execute();
}
/**
* Implements hook_uninstall().
*/
function menu_attributes_uninstall() {
drupal_load('module', 'menu_attributes');
$attributes = menu_attributes_menu_attribute_info();
foreach (array_keys($attributes) as $attribute) {
variable_del("menu_attributes_{$attribute}_enable");
variable_del("menu_attributes_{$attribute}_default");
}
}
/**
* Update the module weight.
*/
function menu_attributes_update_1() {
db_update('system')
->fields(array(
'weight' => 10,
))
->condition('type', 'module')
->condition('name', 'menu_attributes')
->execute();
}
/**
* Fix any menu links that had the class attribute saved as an string.
*/
function menu_attributes_update_7000(&$sandbox) {
if (!isset($sandbox['progress'])) {
$sandbox['progress'] = 0;
$sandbox['current_mlid'] = 0;
// Only count links that possibly have a key class with a string value in
// its serialized options array.
$sandbox['max'] = db_query("SELECT COUNT(DISTINCT mlid) FROM {menu_links} WHERE options LIKE '%s:5:\"class\";s:%'")->fetchField();
}
// Process twenty links at a time.
$limit = 20;
$links = db_query_range("SELECT mlid, options FROM {menu_links} WHERE mlid > :mlid AND options LIKE '%s:5:\"class\";s:%' ORDER BY mlid", 0, $limit, array(':mlid' => $sandbox['current_mlid']))->fetchAllKeyed();
foreach ($links as $mlid => $options) {
$options = unserialize($options);
if (isset($options['attributes']['class']) && is_string($options['attributes']['class'])) {
// Convert classes to an array.
$options['attributes']['class'] = explode(' ', $options['attributes']['class']);
db_update('menu_links')
->fields(array(
'options' => serialize($options),
))
->condition('mlid', $mlid)
->execute();
}
$sandbox['progress']++;
$sandbox['current_mlid'] = $mlid;
}
$sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
// To display a message to the user when the update is completed, return it.
// If you do not want to display a completion message, simply return nothing.
return t('All menu links with non-array value for class attribute have been fixed.');
}
/**
* Grant the 'administer menu attributes' permission to roles that currently
* have the 'administer menu' permission.
*/
function menu_attributes_update_7001() {
$roles = user_roles(FALSE, 'administer menu');
foreach ($roles as $rid => $role) {
user_role_grant_permissions($rid, array('administer menu attributes'));
}
return t("Every role with the 'administer menu' permission has also received the 'administer menu attributes' permission.");
}