3 use Drupal\Core\Database\Connection;
4 use Drupal\taxonomy\Entity\Term;
8 * Install, update and uninstall functions for the permissions_by_term module.
12 * Implements hook_schema().
14 function permissions_by_term_schema() {
18 // Specifications for tabe 'permissions_by_term_user'.
19 $schema['permissions_by_term_user'] = [
20 'description' => "Stores the tid's to which a user has permission by his uid.",
33 'type' => 'varchar_ascii',
43 // Specifications for tabe 'permissions_by_term_role'.
44 $schema['permissions_by_term_role'] = [
45 'description' => "Stores the tid's to which user's are allowed to by rid.",
58 'type' => 'varchar_ascii',
73 * Implements hook_install().
75 function permissions_by_term_install() {
76 node_access_rebuild(TRUE);
80 * Implements hook_uninstall().
82 function permissions_by_term_uninstall() {
83 node_access_rebuild(TRUE);
87 * Mandatory initial run of node_access_rebuild() Drupal core function.
89 function permissions_by_term_update_8113() {
90 node_access_rebuild(TRUE);
94 * Force a node access rebuild to fix node access grants.
96 function permissions_by_term_update_8114() {
97 node_access_rebuild(TRUE);
101 * Force a node access rebuild to fix multilingual node access grants.
103 function permissions_by_term_update_8142() {
104 node_access_rebuild(TRUE);
108 * Add field for langcode in user and role permission tables.
110 function permissions_by_term_update_8145() {
111 $database = \Drupal::database();
112 $schema = $database->schema();
115 'type' => 'varchar_ascii',
120 $schema->addField('permissions_by_term_role', 'langcode', $spec);
121 $schema->addField('permissions_by_term_user', 'langcode', $spec);
125 * Add langcode to each permission restriction dataset. Cleanup dangling datasets, with no relation to any taxonomy term.
127 function permissions_by_term_update_8152() {
129 * @var Connection $database
131 $database = \Drupal::service('database');
132 $userTerms = $database->query("SELECT tid FROM {permissions_by_term_user} WHERE langcode = '' OR langcode IS NULL")->fetchAll();
133 $roleTerms = $database->query("SELECT tid FROM {permissions_by_term_role} WHERE langcode = '' OR langcode IS NULL")->fetchAll();
135 foreach ($roleTerms as $roleTerm) {
136 if (!empty($term = Term::load($roleTerm->tid))) {
137 $termLangcode = $term->get('langcode')->getLangcode();
138 if (!empty($termLangcode)) {
139 $database->query("UPDATE {permissions_by_term_role} SET langcode = :langcode WHERE tid = :tid", [
140 ':langcode' => $termLangcode,
141 ':tid' => $roleTerm->tid,
145 $database->query("DELETE FROM {permissions_by_term_role} WHERE tid = :tid", [
146 ':tid' => $roleTerm->tid,
151 foreach ($userTerms as $userTerm) {
152 if (!empty($term = Term::load($userTerm->tid))) {
153 $termLangcode = $term->get('langcode')->getLangcode();
154 if (!empty($termLangcode)) {
155 $database->query("UPDATE {permissions_by_term_user} SET langcode = :langcode WHERE tid = :tid", [
156 ':langcode' => $termLangcode,
157 ':tid' => $userTerm->tid
161 $database->query("DELETE FROM {permissions_by_term_user} WHERE tid = :tid", [
162 ':tid' => $userTerm->tid,
169 * Remove primary keys due language codes.
171 function permissions_by_term_update_8153() {
172 $database = \Drupal::database();
173 $schema = $database->schema();
175 $schema->dropPrimaryKey('permissions_by_term_role');
176 $schema->dropPrimaryKey('permissions_by_term_user');