-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfactorial_tools.module
190 lines (165 loc) · 6.04 KB
/
factorial_tools.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
<?php
/**
* @file
* Helper functions for deployment.
*/
define('FACTORIAL_TOOLS_BATCH_LIMIT', 10);
use \Drupal\Core\Entity\Query\QueryInterface;
/**
* Delete all entities that can be loaded using given query.
*
* @param array $sandbox
* The $sandbox variable passed to hook_update_N.
* @param QueryInterface $query
* The query used to find ids of the entities to be deleted.
* @param QueryInterface $count_query
* Optional count query to be used for finding maximum number of entities.
*
* @return string
* Return value to be used for hook_update_N.
*/
function factorial_tools_entities_delete_by_query(&$sandbox, QueryInterface $query, QueryInterface $count_query = NULL) {
if (!isset($sandbox['progress'])) {
$count_query = ($count_query) ? $count_query : clone $query;
$sandbox['progress'] = 0;
$sandbox['max'] = $count_query->count()->execute();
$sandbox['messages'] = array();
}
$query->range($sandbox['progress'], FACTORIAL_TOOLS_BATCH_LIMIT);
$entity_ids = $query->execute();
$entity_type = $query->getEntityTypeId();
$storage_handler = \Drupal::entityTypeManager()->getStorage($entity_type);
$entities = $storage_handler->loadMultiple($entity_ids);
$storage_handler->delete($entities);
$sandbox['progress'] += count($entity_ids);
$sandbox['#finished'] = ($sandbox['progress'] >= $sandbox['max'])
? TRUE
: ($sandbox['progress'] / $sandbox['max']);
if ($sandbox['#finished']) {
return t('Deleted @count number of entities of type @type and given conditions.', array('@count' => $sandbox['progress'], '@type' => $entity_type));
}
}
/**
* Delete all entities of a given entity type.
*
* @param array $sandbox
* The $sandbox variable passed to hook_update_N.
* @param string $entity_type
* The entity type machine name.
* @param array $conditions
* Various conditions supported by entityQuery.
*
* @return string
* Return value to be used for hook_update_N.
*/
function factorial_tools_entities_delete(&$sandbox, $entity_type, $conditions = array()) {
if (!isset($sandbox['progress'])) {
$query = _factorial_tools_create_entity_query($entity_type, $conditions);
$sandbox['progress'] = 0;
$sandbox['max'] = $query->count()->execute();
$sandbox['messages'] = array();
}
$query = _factorial_tools_create_entity_query($entity_type, $conditions);
$query->range($sandbox['progress'], FACTORIAL_TOOLS_BATCH_LIMIT);
$entity_ids = $query->execute();
$storage_handler = \Drupal::entityTypeManager()->getStorage($entity_type);
$entities = $storage_handler->loadMultiple($entity_ids);
$storage_handler->delete($entities);
$sandbox['progress'] += count($entity_ids);
$sandbox['#finished'] = ($sandbox['progress'] >= $sandbox['max'])
? TRUE
: ($sandbox['progress'] / $sandbox['max']);
if ($sandbox['#finished']) {
return t('Deleted @count number of entities of type @type and given conditions.', array('@count' => $sandbox['progress'], '@type' => $entity_type));
}
}
/**
* Helper function to create entityQuery.
*/
function _factorial_tools_create_entity_query($entity_type, $conditions = array()) {
$query = \Drupal::entityQuery($entity_type);
if ($conditions) {
foreach ($conditions as $condition) {
@list($field, $arg, $op) = $condition;
if ($op === NULL) {
$query->condition($field, $arg);
}
else {
$query->condition($field, $arg, $op);
}
}
}
return $query;
}
/**
* Work on entities that can be loaded using given query.
*
* @param callable $func
* The function to call.
* @param array $sandbox
* The $sandbox variable passed to hook_update_N.
* @param QueryInterface $query
* The query used to find ids of the entities to be deleted.
* @param QueryInterface $count_query
* Optional count query to be used for finding maximum number of entities.
*
* @return string
* Return value to be used for hook_update_N.
*/
function factorial_tools_entities_apply_func_by_query(callable $func, array &$sandbox, QueryInterface $query, QueryInterface $count_query = NULL) {
if (!isset($sandbox['progress'])) {
$count_query = ($count_query) ? $count_query : clone $query;
$sandbox['progress'] = 0;
$sandbox['max'] = $count_query->count()->execute();
$sandbox['messages'] = array();
}
$query->range($sandbox['progress'], FACTORIAL_TOOLS_BATCH_LIMIT);
$entity_ids = $query->execute();
$entity_type = $query->getEntityTypeId();
$storage_handler = \Drupal::entityTypeManager()->getStorage($entity_type);
$entities = $storage_handler->loadMultiple($entity_ids);
foreach ($entities as $ndx => $entity) {
$func($entity);
}
$sandbox['progress'] += count($entity_ids);
$sandbox['#finished'] = ($sandbox['progress'] >= $sandbox['max'])
? TRUE
: ($sandbox['progress'] / $sandbox['max']);
if ($sandbox['#finished']) {
return t('Applied func on @count number of entities of type @type and given conditions.', array('@count' => $sandbox['progress'], '@type' => $entity_type));
}
}
/**
* Get a menu as an array.
*/
/**
* Renders a menu html.
*/
function factorial_tools_get_menu_links($menu_name) {
$menu_tree = \Drupal::menuTree();
$parameters = $menu_tree->getCurrentRouteMenuTreeParameters($menu_name);
$tree = $menu_tree->load($menu_name, $parameters);
// Transform the tree using the manipulators you want.
$manipulators = [
// Only show links that are accessible for the current user.
['callable' => 'menu.default_tree_manipulators:checkAccess'],
// Use the default sorting of menu links.
['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'],
];
$tree = $menu_tree->transform($tree, $manipulators);
// Finally, build a renderable array from the transformed tree.
$menu = $menu_tree->build($tree);
$menu_arr = [];
if (!empty($menu['#items'])) {
foreach ($menu['#items'] as $link) {
$title = $link['title'];
$href = $link['url']->toString();
$menu_arr[] = [
'url' => $href,
'text' => $title,
'modifier' => empty($link['in_active_trail']) ? '' : 'is-active',
];
}
}
return $menu_arr;
}