-
Notifications
You must be signed in to change notification settings - Fork 8
/
workbench_access.drush.inc
109 lines (100 loc) · 2.67 KB
/
workbench_access.drush.inc
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
<?php
/**
* @file
* Drush commands for Workbench Access.
*/
use Drupal\Core\Language\LanguageInterface;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\Entity\Vocabulary;
/**
* Implements hook_drush_command().
*/
function workbench_access_drush_command() {
$items = [];
$items['workbench-access-test'] = [
'description' => 'Install test vocabulary for Workbench Access.',
'aliases' => ['wa-test'],
];
$items['workbench-access-flush'] = [
'description' => 'Flush section assignments for users and roles.',
'aliases' => ['wa-flush'],
];
return $items;
}
/**
* Implements hook_drush_help().
*/
function workbench_access_drush_help($section) {
$items = workbench_access_drush_command();
$name = str_replace('workbench access:', '', $section);
if (isset($items[$name])) {
return dt($items[$name]['description']);
}
}
/**
* Installs the test vocabulary.
*
* @TODO: Refactor
*/
function drush_workbench_access_test() {
try {
// Create a vocabulary.
$vocabulary = Vocabulary::create([
'name' => 'Workbench Access',
'description' => 'Test taxonomy for Workbench Access',
'vid' => 'workbench_access',
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
'weight' => 100,
]);
$vocabulary->save();
// Create some terms.
$terms = [
'Alumni',
'Faculty',
'Staff',
'Students',
];
$children = [
'Directory',
'Information',
];
$filter_formats = filter_formats();
$format = array_pop($filter_formats);
foreach ($terms as $name) {
$term = Term::create([
'name' => $name,
'description' => [],
'vid' => $vocabulary->id(),
'parent' => 0,
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
]);
$term->save();
foreach ($children as $child) {
$child = Term::create([
'name' => "$name $child",
'description' => [],
'vid' => $vocabulary->id(),
'parent' => $term->id(),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
]);
$child->save();
}
}
}
catch (Exception $e) {
drush_print('The test vocabulary has already been created.');
}
}
/**
* Flushes assigned user permissions.
*/
function drush_workbench_access_flush() {
$section_storage = \Drupal::entityTypeManager()->getStorage('section_association');
foreach (\Drupal::entityTypeManager()->getStorage('access_scheme')->loadMultiple() as $scheme) {
$sections = $section_storage->loadByProperties([
'access_scheme' => $scheme->id(),
]);
$section_storage->delete($sections);
}
drush_print('User and role assignments cleared.');
}