This repository has been archived by the owner on Sep 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
termstatus.install
122 lines (95 loc) · 3.08 KB
/
termstatus.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 install hooks for termstatus module.
*/
use Drupal\user\Entity\Role;
/**
* Handle the out-of-the box necessities.
*
* Implements hook_install().
*/
function termstatus_install() {
// Create field storage for the 'Status' base field.
$field_manager = \Drupal::service('entity_field.manager');
$field_storage_manager = \Drupal::service('field_storage_definition.listener');
$definition = $field_manager->getFieldStorageDefinitions('taxonomy_term')['status'];
$field_storage_manager->onFieldStorageDefinitionCreate($definition);
$vocabularies = termstatus_get_vocabularies();
if ($vocabularies) {
foreach ($vocabularies as $vocabulary) {
$vid = $vocabulary->id();
$terms = \Drupal::entityTypeManager()
->getStorage('taxonomy_term')
->loadTree($vid, 0, NULL, TRUE);
// Migrate all existing terms to published state.
if (!empty($terms)) {
foreach ($terms as $term) {
$term->setStatus(TRUE);
$term->save();
}
}
// Give existing roles the view permission.
$permission = 'view published terms in ' . $vid;
termstatus_grant_permission_to_existing_roles($permission);
}
}
}
/**
* Handles the necessary changes for module uninstall.
*
* Implements hook_uninstall().
*/
function termstatus_uninstall() {
$vocabularies = termstatus_get_vocabularies();
if ($vocabularies) {
/** @var \Drupal\taxonomy\Entity\Vocabulary $vocabulary */
foreach ($vocabularies as $vocabulary) {
$vid = $vocabulary->id();
// To be able to delete the status field storage,
// we set the status field for all terms to null.
$terms = \Drupal::entityTypeManager()
->getStorage('taxonomy_term')
->loadTree($vid, 0, NULL, TRUE);
if (!empty($terms)) {
foreach ($terms as $term) {
$term->setStatus(NULL);
$term->save();
}
}
$permission = 'view published terms in ' . $vid;
termstatus_grant_permission_to_existing_roles($permission);
}
}
// Delete field storage for the 'Status' base field.
$field_manager = \Drupal::service('entity_field.manager');
$field_storage_manager = \Drupal::service('field_storage_definition.listener');
$definition = $field_manager->getFieldStorageDefinitions('taxonomy_term')['status'];
$field_storage_manager->onFieldStorageDefinitionDelete($definition);
}
/**
* Helper function to grant existing roles view permissions.
*
* @param $permission
*/
function termstatus_grant_permission_to_existing_roles($permission) {
$roles = Role::loadMultiple();
/** @var Role $role */
foreach ($roles as $role) {
$role->grantPermission($permission);
$role->save();
}
}
/**
* Helper function to revoke existing roles of view permissions.
*
* @param $permission
*/
function termstatus_revoke_permission_from_existing_roles($permission) {
$roles = Role::loadMultiple();
/** @var Role $role */
foreach ($roles as $role) {
$role->revokePermission($permission);
$role->save();
}
}