-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction_button.module
222 lines (203 loc) · 6.57 KB
/
action_button.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
<?php
/**
* @file
* A button that will load a small menu ajax time, when told.
*/
/**
* Implements hook_init().
*
* This includes the javascript and library needed for the action button to
* function.
* We load these here instead of dynamically on theme, because the library
* wasn't getting included at the right time.
*/
function action_button_init() {
drupal_add_js(drupal_get_path('module', 'action_button') . '/action_button.js');
drupal_add_library('system', 'drupal.ajax');
}
/**
* Implements hook_menu().
*/
function action_button_menu() {
// When using the drupal.ajax, it needs a nojs menu.
// This also acts as a base for the fetch/list/ajax
$items['action_button/fetch_list/nojs'] = array(
'page callback' => 'action_button_fetch_list',
'page arguments' => array(2),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
$items['action_button/fetch_list/ajax'] = array(
'delivery callback' => 'ajax_deliver'
) + $items['action_button/fetch_list/nojs'];
return $items;
}
/**
* Get the list of actions, which is used in the action button list.
*
* It takes the context from the $_GET parameter.
*
* @param $ajax
* Return as ajax or as normal.
*/
function action_button_fetch_list($ajax) {
$is_ajax = $ajax === 'ajax';
$context = drupal_get_query_parameters();
$html = '';
try {
$html = action_button_render_list($context);
}
catch (Exception $e) {
if ($e instanceof DingProviderAuthException) {
drupal_set_message($e->getMessage());
ctools_include('ajax');
ctools_add_js('ajax-responder');
$commands[] = ctools_ajax_command_redirect('<front>');
return array(
'#type' => 'ajax',
'#commands' => $commands,
);
}
}
// remove <div class="item-list"> and it's </div>
$html = substr($html, 23, -6);
if ($is_ajax) {
$commands = array();
$selector = '#' . $context['html_id'] . ' .action-list';
$commands[] = ajax_command_replace($selector, $html);
return array(
'#type' => 'ajax',
'#commands' => $commands,
);
}
else {
return $html;
}
}
/**
* Implements hook_theme().
*/
function action_button_theme($existing, $type, $theme, $path) {
$baseTheme = array(
'file' => 'action_button.theme.inc'
);
return array(
'action_button' => $baseTheme + array(
'template' => 'action-button',
'variables' => array(
'context' => NULL,
'title' => t('More'),
'group' => NULL,
'render_list' => FALSE,
'reverse' => FALSE
),
),
'action_button_checkbox' => $baseTheme + array(
'variables' => array('id' => NULL, 'group' => NULL)
)
);
}
/**
* Get the HTML for the list.
*
* @param $context
* The context of the action button.
*/
function action_button_render_list($context) {
// Get the items.
$items = module_invoke_all('action_button_list', $context);
if (empty($items)) {
$items[] = array(
'#type' => 'markup',
'#markup' => t('There are no actions avaiable for this item'),
);
}
// Set a default weight value.
$i = 0;
foreach ($items as &$item) {
if (!isset($item['#weight'])) {
$item['#weight'] = ($i += 0.1);
}
}
// Make sure other modules can alter the list.
drupal_alter('action_button_list_alter', $items);
// Sort the list after the alter.
uasort($items, 'element_sort');
// Handle the items.
$list_items = array();
foreach ($items as &$item) {
$classes = array();
$group_val = '';
if (!isset($context['group'])) {
$classes[] = 'action-list-item';
}
// If it's a group button, give the list items another class.
elseif (!empty($context['group'])) {
// $classes[] = 'action-list-group-item';
$classes[] = 'action-list-item';
$group_val = $context['group'];
}
// To allow easier creation of links as action commands, we set up some
// default values for every render array with the #theme link.
if ($item['#theme'] == 'link') {
// The link theme needs the following options, so add them if they're
// not present already.
if (!isset($item['#options'])) {
$item['#options'] = array();
}
$item['#options'] += array(
'attributes' => array(),
'html' => TRUE,
);
// Add the classes
if (!empty($item['#options']['attributes']['class']) && is_array($item['#options']['attributes']['class']))
$item['#options']['attributes']['class'] = array_merge($item['#options']['attributes']['class'], $classes);
else
$item['#options']['attributes']['class'] = $classes;
// Set the group reference if it's a group.
if (!empty($group_val)) {
$item['#options']['attributes']['ref'] = $group_val;
}
// If the theme has a callback, set the path to this callback, and make
// sure the link uses the right classes and has the right get variables.
if (isset($item['#callback'])) {
// $item['#path'] = 'action_button/callback/' . $item['#callback'];
$item['#path'] = $item['#callback']; //'action_button/callback/' . $item['callback'];
$item['#options']['query'] = $context; // Har vi brug for hele context, eller kan vi nøjes med id?
$item['#options']['attributes']['class'][] = 'use-ajax';
if (empty($item['#options']['attributes']['id'])) {
$item['#options']['attributes']['id'] = drupal_html_id('action-link');
}
}
if (isset($context['destination'])) {
$item['#options']['query']['destination'] = $context['destination'];
}
}
// If it's not a link theme, just add
else {
// Add some classes to the render array (BUT how do we know the render
// array support classes)
$item['attributes']['class'] = $classes;
// What do we do with render arrays that are not a link?
if (isset($group_val)) {
$item['attributes']['ref'] = $group_val;
}
}
// Add the rendered item to the item array.
$list_items[]['data'] = render($item);
}
// Return the html for the list.
return theme(
'item_list',
array(
'items' => $list_items,
'attributes' => array('class' => array('action-list'))
)
);
}
/**
* Implements hook_ctools_plugin_directory()
*/
function action_button_ctools_plugin_directory($owner, $plugin_type) {
return 'plugins/' . $plugin_type;
}