workflow.module
15.9 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
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
<?php
/**
* @file
* Support workflows made up of arbitrary states.
*/
define('WORKFLOW_CREATION_STATE', 1);
define('WORKFLOW_CREATION_DEFAULT_WEIGHT', -50);
define('WORKFLOW_DELETION', 0);
// Couldn't find a more elegant way to preserve translation.
define('WORKFLOW_CREATION_STATE_NAME', '(' . t('creation') . ')');
/**
* Role ID for anonymous users.
*/
// Add brackets to indicate a special role, and distinguish from frequently used 'author' role.
define('WORKFLOW_ROLE_AUTHOR_NAME', '(' . t('author') . ')');
define('WORKFLOW_ROLE_AUTHOR_RID', 'workflow_author');
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\workflow\Entity\Workflow;
use Drupal\workflow\Entity\WorkflowState;
use Drupal\workflow\Entity\WorkflowTransitionInterface;
module_load_include('inc', 'workflow', 'workflow.form');
module_load_include('inc', 'workflow', 'workflow.field');
/**********************************************************************
*
* Info hooks.
*
*/
/**
* Implements hook_help().
*/
function workflow_help($route_name, RouteMatchInterface $route_match) {
$output = '';
switch ($route_name) {
case 'help.page.workflow':
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('The Workflow module adds a field to Entities to
store field values as Workflow states. You can control "state transitions"
and add action to specific transitions.') . '</p>';
}
return $output;
}
/**
* Implements hook_hook_info().
*
* Allow adopters to place their hook implementations in either
* their main module or in a module.workflow.inc file.
*/
function workflow_hook_info() {
$hooks['workflow'] = array('group' => 'workflow');
return $hooks;
}
/**
* Implements hook_entity_type_build().
*
* @see https://www.drupal.org/node/2196275 "hook_entity_info() renamed to hook_entity_type_build()"
*/
function TODOworkflow_entity_type_build(array &$entity_types) {
workflow_debug(__FILE__, __FUNCTION__, __LINE__); // @todo D8-port: still test this snippet.
/* @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
// Add a form controller for a custom entity form without overriding the
// default entity form. To override the default entity form,
// use hook_entity_type_alter().
//$entity_types['node']->setFormClass('mymodule_foo', 'Drupal\mymodule\NodeFooFormController');
}
/**********************************************************************
*
* CRUD hooks.
*
*/
/**
* Implements hook_user_cancel().
*
* Update tables for deleted account, move account to user 0 (anon.)
* ALERT: This may cause previously non-Anonymous posts to suddenly
* be accessible to Anonymous.
*/
function workflow_user_cancel($edit, $account, $method) {
Workflow::workflowManager()->cancelUser($edit, $account, $method);
}
/**
* Implements hook_user_delete().
*/
function workflow_user_delete($account) {
Workflow::workflowManager()->deleteUser($account);
}
/**
* Implements hook_ENTITY_TYPE_insert().
*
* Is called when adding a new Workflow type.
* The technical name for the Workflow entity is 'workflow_type'.
*/
function workflow_workflow_type_insert(EntityInterface $entity) {
Workflow::workflowManager()->participateUserRoles($entity);
}
/**
* Implements hook_entity_insert().
*/
function workflow_entity_insert(EntityInterface $entity) {
workflow_entity_update($entity);
}
/**
* Implements hook_entity_update().
*/
function workflow_entity_update(EntityInterface $entity) {
// Avoid this hook on workflow objects.
if (!in_array($entity->getEntityTypeId(), [
'workflow_type',
'workflow_state',
'workflow_config_transition',
'workflow_transition',
'workflow_scheduled_transition',
])) {
// Execute/save the transitions fom the widgets in the entity form.
Workflow::workflowManager()->executeTransitionsOfEntity($entity);
}
}
/**
* Implements hook_cron().
*
* Given a timeframe, execute all scheduled transitions.
*/
function workflow_cron() {
Workflow::workflowManager()->executeScheduledTransitionsBetween(0, REQUEST_TIME);
}
/**
* Business related functions, the API.
*/
/**
* @deprecated D8: @see WorkflowManager::executeTransition().
*/
function workflow_execute_transition(WorkflowTransitionInterface $transition, $force = FALSE) {
// Execute transition and update the attached entity.
return Workflow::workflowManager()->executeTransition($transition);
}
/**
* Functions to get an options list (to show in a Widget).
* To be used in non-OO modules, like workflow_rules, workflow_views.
*
* The naming convention is workflow_get_<entity_type>_names.
* (A bit different from 'user_role_names'.)
* Can be used for hook_allowed_values from list.module.
* Todo: move to \Drupal::entityManager::getEntityTypeLabels($group = FALSE).
* - user_role
* - workflow
* - workflow_state
* - sid
*/
/**
* @deprecated D8: workflow_get_roles --> workflow_get_user_role_names
*/
function workflow_get_roles($permission) {
return [];
}
/**
* Retrieves the names of roles matching specified conditions.
*
* deprecated D8: workflow_get_roles --> workflow_get_user_role_names
*
* Usage:
* D7: $roles = workflow_get_user_role_names('participate in workflow');
* D8: $type_id = $workflow->id();
* D8: $roles = workflow_get_user_role_names("create $type_id workflow_transition");
*
* @param string $permission
* (optional) A string containing a permission. If set, only roles
* containing that permission are returned. Defaults to NULL, which
* returns all roles.
* Normal usage for filtering roles that are enabled in a workflow_type
* would be: $permission = 'create $type_id transition'.
*
* @return array
* Array of role names keyed by role ID, including the 'author' role.
*/
function workflow_get_user_role_names($permission) {
static $roles = NULL;
if (!$roles[$permission]) {
$roles[$permission] =
array(WORKFLOW_ROLE_AUTHOR_RID => WORKFLOW_ROLE_AUTHOR_NAME) +
// Copied from AccountForm::form().
array_map(array('\Drupal\Component\Utility\SafeMarkup', 'checkPlain'), user_role_names(TRUE, $permission));
}
return $roles[$permission];
}
/**
* Get an options list for workflow states.
*
* @param mixed $wid
* The Workflow ID.
* @param bool $grouped
* Indicates if the value must be grouped per workflow.
* This influence the rendering of the select_list options.
* @param bool $all
* Indicates to return all (TRUE) or active (FALSE) states of a workflow.
*
* @return array $options
* An array of $sid => state->label(), grouped per Workflow.
*/
function workflow_get_workflow_state_names($wid = '', $grouped = FALSE, $all = FALSE) {
$options = array();
// Get the (user-dependent) options.
// Since this function is only used in UI, it is save to use the global $user.
$user = workflow_current_user();
foreach ($workflows = Workflow::loadMultiple($wid ? array($wid) : NULL) as $workflow) {
$workflow_options = array();
$state = WorkflowState::create(array('wid' => $workflow->id()));
$workflow_options = $state->getOptions(NULL, '', $user, FALSE);
if (!$grouped) {
$options += $workflow_options;
}
else {
// Make a group for each Workflow.
$options[$workflow->label()] = $workflow_options;
}
}
return $options;
}
/**
* Get an options list for workflows. Include an initial empty value
* if requested. Validate each workflow, and generate a message if not complete.
*
* @param bool $required
* Indicates if the resulting list contains a options value.
* @return array $options
* An array of $wid => workflow->label().
*/
function workflow_get_workflow_names($required = TRUE) {
$options = array();
if (!$required) {
$options[''] = t('- Select a value -');
}
foreach (Workflow::LoadMultiple() as $wid => $workflow) {
if ($workflow->isValid()) {
$options[$wid] = $workflow->label();
}
}
return $options;
}
/**
* Helper function, to get the label of a given State Id.
*
* @param $sid
*
* @return string
*
* deprecated: workflow_get_sid_label() --> workflow_get_sid_name()
*/
function workflow_get_sid_name($sid) {
if (empty($sid)) {
$label = 'No state';
}
elseif ($state = WorkflowState::load($sid)) {
$label = $state->label();
}
else {
$label = 'Unknown state';
}
return t($label);
}
/**
* Determines the Workflow field_name of an entity.
* If an entity has multiple workflows, only returns the first one.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity at hand.
* @param string $field_name
* The field name. If given, will be passed as return value.
*
* @return string
*/
function workflow_get_field_name(EntityInterface $entity, $field_name = '') {
if (!$entity) {
// $entity may be empty on Entity Add page.
$field_name = '';
}
elseif (!$field_name) {
$fields = _workflow_info_fields($entity);
$field = reset($fields);
/* @var $field \Drupal\field\Entity\FieldStorageConfig */
$field_name = $field->getName();
}
return $field_name;
}
/**
* Helper function to get the entity from a route.
*
* This is a hack. It should be solved by using $routematch.
*
* @param \Drupal\Core\Entity\EntityInterface|NULL $entity
*
* @return \Drupal\Core\Entity\EntityInterface
*/
function workflow_url_get_entity(EntityInterface $entity = NULL) {
if (!$entity) {
$route_match = \Drupal::routeMatch();
// On node pages, we'd get an object.
$entity = $route_match->getParameter('node');
if ($entity && is_object($entity)) {
return $entity;
}
if ($entity && !is_object($entity)) {
// On workflow tab, we'd get an id.
$entity = \Drupal\node\Entity\Node::load($entity);
return $entity;
}
// It was not a Node, try a Term.
if(!$entity) {
// On term pages, we get objects, or id's.
$entity = $route_match->getParameter('taxonomy_term');
if ($entity && is_object($entity)) {
return $entity;
}
elseif ($entity && !is_object($entity)) {
$entity = \Drupal\taxonomy\Entity\Term::load($entity);
}
}
if (!$entity) {
// We may be on a entity add page/
// Or we may be on a page of some unknown entity.
}
}
return $entity;
}
/**
* Helper function to get the entity from a route.
*
*/
function workflow_url_get_operation(EntityInterface $entity = NULL) {
$url = \Drupal\Core\Url::fromRoute('<current>');
// The last part of the path is the operation: edit, workflow, devel.
$url_parts = explode('/', $url->toString());
$operation = array_pop($url_parts);
// Except for view pages.
if (is_numeric($operation) || $operation == 'view') {
$operation = '';
}
return $operation;
}
/**
* Functions to get the state of an entity.
*/
/**
* Wrapper function to get a UserInterface object.
* We use UserInterface to check permissions.
*
* @param \Drupal\Core\Session\AccountInterface|NULL $account
*
* @return \Drupal\user\UserInterface
*/
function workflow_current_user(\Drupal\Core\Session\AccountInterface $account = NULL) {
$account = ($account) ? $account : \Drupal::currentUser();
return \Drupal\user\Entity\User::load($account->id());
}
/**
* Gets the current state ID of a given entity.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* @param string $field_name
*
* @return string $current_sid
*
* @deprecated : use Workflow::workflowManager()->getCurrentStateId()
*/
function workflow_node_current_state(EntityInterface $entity, $field_name = '') {
return Workflow::workflowManager()->getCurrentStateId($entity, $field_name);
}
/**
* Gets the previous state ID of a given entity.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* @param string $field_name
*
* @return string $previous_sid
*
* @deprecated : use Workflow::workflowManager()->getPreviousStateId()
*/
function workflow_node_previous_state($entity, $field_name = '') {
return Workflow::workflowManager()->getPreviousStateId($entity, $field_name);
}
/**
* Get a specific workflow, given an entity type. Only one workflow is possible per node type.
*
* @param string $entity_bundle
* An entity bundle.
* @param string $entity_type
* An entity type. This is passed when also the Field API must be checked.
*
* @return Workflow
* A Workflow object, or FALSE if no workflow is retrieved.
*
* Caveat: gives undefined results with multiple workflows per entity.
*
* @todo: support multiple workflows per entity.
*/
function workflow_get_workflows_by_type($entity_bundle, $entity_type) {
static $map = array();
if (!isset($map[$entity_type][$entity_bundle])) {
$wid = FALSE;
$map[$entity_type][$entity_bundle] = FALSE;
if (!$wid && isset($entity_type)) {
foreach (_workflow_info_fields(NULL, $entity_type, $entity_bundle) as $field_info) {
$wid = $field_info->getSetting('workflow_type');
}
}
// Set the cache with a workflow object.
if ($wid) {
// $wid can be numeric or named.
$workflow = Workflow::load($wid);
$map[$entity_type][$entity_bundle] = $workflow;
}
}
return $map[$entity_type][$entity_bundle];
}
/**
* Gets the workflow field names, if not known already.
*
* For workflow_field, multiple workflows per bundle are supported.
* For workflow_node, only one 'field' structure is returned.
*
* @param EntityInterface $entity
* Object to work with. May be empty, e.g., on menu build.
* @param string $entity_type
* Entity type of object. Optional, but required if $entity provided.
* @param string $entity_bundle
* Bundle of entity. Optional.
* @param string $field_name
* Field name. Optional.
*
* @return Drupal\field\Entity\FieldStorageConfig[] $field_info
* An array of FieldStorageConfig objects.
*/
function _workflow_info_fields($entity = NULL, $entity_type = '', $entity_bundle = '', $field_name = '') {
$field_info = array();
// Figure out the $entity's bundle and id.
if ($entity) {
$entity_type = $entity->getEntityTypeId();
$entity_bundle = $entity->bundle();
}
else {
// Entity type and bundle should be specified.
}
$field_list = Drupal::entityManager()->getFieldMapByFieldType('workflow');
foreach ($field_list as $e_type => $data) {
if (!$entity_type || ($entity_type == $e_type)) {
foreach ($data as $f_name => $data) {
if (!$entity_bundle || isset($data['bundles'][$entity_bundle])) {
if (!$field_name || ($field_name == $f_name)) {
// Do not use the field_name as ID, but the unique <entity_type>.<field_name>
// since you cannot share the same field on multiple entity_types (unlike D7).
$field_config = \Drupal\field\Entity\FieldStorageConfig::loadByName($e_type, $f_name);
$field_info[$field_config->id()] = $field_config;
}
}
}
}
}
return $field_info;
}
/**
* Gets a list of field names, to be used in Options lists.
*
*/
function _workflow_info_field_names($entity = NULL, $entity_type = '', $entity_bundle = '', $field_name = '') {
$result = [];
foreach ($fields = _workflow_info_fields($entity, $entity_type, $entity_bundle, $field_name) as $definition) {
$field_name = $definition->getName();
$result[$field_name] = $definition->getName();
}
return $result;
}
/**
* Helper function for D8-port: Get some info on screen.
* @see workflow_devel module
*
* Usage:
* workflow_debug( __FILE__, __FUNCTION__, __LINE__, '', ''); // @todo D8-port: still test this snippet.
*
* @param string $class_name
* @param string $function_name
* @param string $line
* @param string $value1
* @param string $value2
*
*/
function workflow_debug($class_name, $function_name, $line = '', $value1 = '', $value2 = '') {
$debug_switch = FALSE;
$debug_switch = TRUE;
if (!$debug_switch) {
return;
}
$class_name_elements = explode( "\\" , $class_name);
$output = 'Testing... function ' . end($class_name_elements) . '::' . $function_name . '/' . $line;
if ($value1) {
$output .= ' = ' . $value1;
}
if ($value2) {
$output .= ' > ' . $value2;
}
drupal_set_message($output, 'warning', TRUE);
}