-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdrupalcrudadminmodule.module
269 lines (236 loc) · 9.04 KB
/
drupalcrudadminmodule.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
<?php
/**
* @file
* This module file is required by Drupal to act as entry point to the current module.
*
* @author Bastian Feder <[email protected]>
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
* @copyright Copyright (c) 2013 Liip Inc.
*/
use Liip\Drupal\Modules\DrupalConnector\Common;
use Liip\Drupal\Modules\DrupalConnector\ConnectorFactory;
use Liip\Drupal\Modules\EventManager\EventManagerException;
/**
* Defines a number of routes.
*
* Implements the hook_menu.
*
* @return array
*/
function drupalcrudadminmodule_menu()
{
/** @var \Liip\Drupal\Modules\DrupalConnector\ConnectorFactory $factory */
$factory = new ConnectorFactory();
$items = array();
/**
* Route to handle the action listing all entities of a module.
*
* admin/config/content/crud/<module>/<entity>
*
* e.g. admin/config/content/crud/organisations/organisation
*/
$items['admin/config/content/crud/%/%'] = array(
'title callback' => 'drupalcrudadminmodule_title_controller',
'title arguments' => array(4, 5, '', 'overview', $factory),
'page callback' => 'drupalcrudadminmodule_controller',
'page arguments' => array(4, 5, '', 'overview', $factory),
'access callback' => 'drupalcrudadminmodule_access_controller',
'access arguments' => array(4, 5, 'administer', $factory),
);
/**
* Route to handle actions on an undefined entity.
*
* admin/config/content/crud/<module>/<entity/<action>
*
* e.g. admin/config/content/crud/organisations/organisation/add
*/
$items['admin/config/content/crud/%/%/add'] = array(
'title callback' => 'drupalcrudadminmodule_title_controller',
'title arguments' => array(4, 5, '', 6, $factory),
'page callback' => 'drupalcrudadminmodule_controller',
'page arguments' => array(4, 5, '', 6, $factory),
'access callback' => 'drupalcrudadminmodule_access_controller',
'access arguments' => array(4, 5, 'administer', $factory),
'type' => MENU_LOCAL_ACTION,
);
/**
* Route to handle dedicated actions on an identified entity.
*
* admin/config/content/crud/<module>/<entity/<entityId>/<action>
*
* e.g. admin/config/content/crud/organisations/organisation/hey378236hs03kjefr0/edit
* e.g. admin/config/content/crud/organisations/organisation/hey378236hs03kjefr0/delete
* e.g. admin/config/content/crud/organisations/organisation/hey378236hs03kjefr0/read
*/
$items['admin/config/content/crud/%/%/%/%'] = array(
'title callback' => 'drupalcrudadminmodule_title_controller',
'title arguments' => array(4, 5, 6, 7, $factory),
'page callback' => 'drupalcrudadminmodule_controller',
'page arguments' => array(4, 5, 6, 7, $factory),
'access callback' => 'drupalcrudadminmodule_access_controller',
'access arguments' => array(4, 5, 'administer', $factory),
);
return $items;
}
/**
* Defines the properties of every theme to be used by the module.
*
* @return array
*/
function drupalcrudadminmodule_theme()
{
/** @var Liip\Drupal\Modules\DrupalConnector\Common $dcc */
$dcc = ConnectorFactory::getCommonConnector();
$templatePath = $dcc->drupal_get_path('module', 'drupalcrudadminmodule') . '/templates';
return array(
'drupalcrudadminmodule_confirm_delete' => array(
'template' => 'drupalcrudadminmodule_confirm_delete',
'variables' => array('entityName' => null, 'moduleName' => null, 'entity' => null),
'path' => $templatePath,
),
);
}
/**
* Action controller to route the incoming requests.
*
* @param string $module
* @param string $entity
* @param string $identifier
* @param string $action
* @param ConnectorFactory $factory
*
* @return array
* @throws InvalidArgumentException
*
* @see drupalcrudadminmodule_page()
* @see drupalcrudadminmodule_delete_form()
*/
function drupalcrudadminmodule_controller($module, $entity = '', $identifier = '', $action = '', ConnectorFactory $factory = null)
{
// verify if mandatory functions are implemented by the target module.
_drupalcrudadminmodule_verify_contract($module, $entity, $factory);
/** @var \Liip\Drupal\Modules\DrupalConnector\Module $dcm */
$dcm = $factory->getModuleConnector();
/** @var \Liip\Drupal\Modules\DrupalConnector\Common $dcc */
$dcc = $factory->getCommonConnector();
try {
switch (strtolower($action)) {
case 'overview':
case 'list':
$page = drupalcrudadminmodule_page(
'drupalcrudadminmodule_overview_table',
$module,
$entity,
$identifier,
$factory
);
return $page;
case 'read':
return drupalcrudadminmodule_page('drupalcrudadminmodule_edit_form', $module, $entity, $identifier, $factory);
case 'add':
return drupalcrudadminmodule_page('drupalcrudadminmodule_edit_form', $module, $entity, '', $factory);
case 'edit':
return drupalcrudadminmodule_page('drupalcrudadminmodule_edit_form', $module, $entity, $identifier, $factory);
case 'delete':
// find event trigger in function drupalcrudadminmodule_delete_form
return drupalcrudadminmodule_delete_form($module, $entity, $identifier, $factory);
default:
throw new InvalidArgumentException("Invalid action ($action) invoked.");
}
} catch (EventManagerException $e) {
// todo: use watchdog_exception
$dcc->watchdog(
'Liip Drupal CRUD Admin Module',
$e->getMessage(),
array('action' => $action, 'module' => $module, 'entity' => $entity, 'identifier' => $identifier),
WATCHDOG_ERROR
);
}
}
/**
* Generates the title displayed on the currently shown page.
*
* @param string $module
* @param string $entity
* @param string $identifier
* @param string $action
* @param ConnectorFactory $factory
*
* @throws InvalidArgumentException
* @return string
*/
function drupalcrudadminmodule_title_controller($module, $entity, $identifier = '', $action = '', ConnectorFactory $factory = null)
{
/** @var \Liip\Drupal\Modules\DrupalConnector\Common $dcc */
$dcc = $factory->getCommonConnector();
switch (strtolower($action)) {
case 'overview':
case 'list':
$title = $dcc->t(
"LIIP_DRUPAL_CRUD_ADMIN_MODULE_OVERVIEW",
array('@module' => $module)
);
break;
case 'add':
$title = $dcc->t(
"LIIP_DRUPAL_CRUD_ADMIN_MODULE_ACTION_ADD",
array('@module' => $module, '@entity' => $entity)
);
break;
case 'read':
$title = $dcc->t(
"LIIP_DRUPAL_CRUD_ADMIN_MODULE_ACTION_ENTITY_READ",
array('@module' => $module, '@entity' => $entity, '@id' => $identifier)
);
break;
case 'delete':
$title = $dcc->t(
"LIIP_DRUPAL_CRUD_ADMIN_MODULE_ACTION_DELETE",
array('@module' => $module, '@entity' => $entity, '@id' => $identifier)
);
break;
case 'edit':
$title = $dcc->t(
"LIIP_DRUPAL_CRUD_ADMIN_MODULE_ACTION_EDIT",
array('@module' => $module, '@entity' => $entity)
);
break;
default:
throw new InvalidArgumentException("Invalid action ($action) invoked.");
}
return $title;
}
/**
* Verifies that the current user has the required permission to access the content.
*
* Tries to invoke the hook_menu_access_controller() on the consuming module and returns its return value if it exists.
*
* @param string $moduleName
* @param string $entityName
* @param string $permission
* @param ConnectorFactory $factory
*
* @return bool
*
* @see ${moduleName}_menu_access_controller()
*/
function drupalcrudadminmodule_access_controller($moduleName, $entityName, $permission, ConnectorFactory $factory)
{
/** @var \Liip\Drupal\Modules\DrupalConnector\Module $dcm */
$dcm = $factory->getModuleConnector();
// check for a custom hook_menuAccessController() ans if there is one, use this instead.
$permissions = $dcm->module_invoke($moduleName, 'menuAccessController', $entityName, $permission, $factory);
if (!empty($permissions)) {
return $permissions;
}
/** @var \Liip\Drupal\Modules\DrupalConnector\User $dcu */
$dcu = $factory->getUserConnector();
/** @var \Liip\Drupal\Modules\DrupalConnector\Bootstrap $dcb */
$dcb = $factory->getBootstrapConnector();
// do not show this menu entry on the administration overview page
if ("de/admin/config/content" == $dcb->request_path()) {
return false;
}
// still respect access restrictions on user level
return $dcu->user_access($permission);
}