-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtemplate_whisperer.install
122 lines (110 loc) · 3.89 KB
/
template_whisperer.install
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
<?php
/**
* @file
* Contains template_whisperer.install.
*/
use Drupal\Core\Url;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
/**
* Implements hook_install().
*/
function template_whisperer_install() {
// Rebuild the route cache before accessing new route.
\Drupal::service("router.builder")->rebuild();
$url = Url::fromRoute('entity.template_whisperer_suggestion.collection');
$messenger = \Drupal::messenger();
$messenger->addMessage(t('Template Whisperer is available under <a href="@administer-page">Administer > Structure > Template Whisperer</a>', ['@administer-page' => $url->toString()]));
}
/**
* Implements hook_schema().
*/
function template_whisperer_schema() {
$schema = [];
$schema['template_whisperer_suggestion_usage'] = [
'description' => 'Track where a suggestion is used.',
'fields' => [
'sid' => [
'description' => 'Suggestion ID.',
'type' => 'varchar_ascii',
'length' => 128,
'not null' => TRUE,
'default' => 0,
],
'module' => [
'description' => 'The name of the module that is using the suggestion.',
'type' => 'varchar_ascii',
'length' => DRUPAL_EXTENSION_NAME_MAX_LENGTH,
'not null' => TRUE,
'default' => '',
],
'type' => [
'description' => 'The name of the object type in which the suggestion is used.',
'type' => 'varchar_ascii',
'length' => 64,
'not null' => TRUE,
'default' => '',
],
'id' => [
'description' => 'The primary key of the object using the suggestion.',
'type' => 'varchar_ascii',
'length' => 64,
'not null' => TRUE,
'default' => 0,
],
'count' => [
'description' => 'The number of times this suggestion is used by this object.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
],
'primary key' => ['sid', 'type', 'id', 'module'],
'indexes' => [
'type_id' => ['type', 'id'],
'sid_count' => ['sid', 'count'],
'sid_module' => ['sid', 'module'],
],
];
return $schema;
}
/**
* Migrate the permission `administer the template whisperer field` to roles.
*/
function template_whisperer_update_8001(&$sandbox) {
/** @var \Drupal\user\RoleInterface $roleStorage */
$roleStorage = \Drupal::service('entity_type.manager')
->getStorage('user_role');
$roles = Role::loadMultiple();
unset($roles[RoleInterface::ANONYMOUS_ID]);
// Get all roles with `administer template whisperer` permissions.
$roles_with_tw_administer = array_filter($roles, function ($role) {
return $role->hasPermission('administer template whisperer');
});
$roles_with_tw_administer = array_map(fn(RoleInterface $role) => $role->label(), $roles_with_tw_administer);
// Get all roles with `administer template whisperer suggestion entities.
$roles_with_tw_suggestion = array_filter($roles, function ($role) {
return $role->hasPermission('administer template whisperer suggestion entities');
});
$roles_with_tw_suggestion = array_map(fn(RoleInterface $role) => $role->label(), $roles_with_tw_suggestion);
$rids = array_merge($roles_with_tw_administer, $roles_with_tw_suggestion);
$rids = array_keys($rids);
if ($rids) {
$roles = $roleStorage->loadMultiple($rids);
// Add the new permission `administer the template whisperer field` to all
// roles that already have the 2 previous permissions
// `administer template whisperer` or
// `administer template whisperer suggestion entities`.
foreach ($roles as $role) {
$role->grantPermission('administer the template whisperer field');
$role->save();
}
}
}
/**
* Remove the Template Whisperer listing views to use the Entity one.
*/
function template_whisperer_update_8002(&$sandbox) {
\Drupal::configFactory()->getEditable('views.view.template_whisperer')->delete();
}