11<?php
22
33use Drupal\taxonomy\Entity\Term;
4+ use Drupal\taxonomy\Entity\Vocabulary;
45
56/**
67 * Implements hook_install().
78 */
89function ubc_cwl_auth_install() {
9- $vocabulary = 'visibility';
10+ ubc_cwl_auth_createCwlRole();
11+ ubc_cwl_auth_createVisibilityTaxonomy();
12+ }
13+
14+ function ubc_cwl_auth_createCwlRole() {
15+
16+ // Check if the role already exists.
17+ $role_name = 'cwl';
18+ $role_exists = \Drupal\user\Entity\Role::load($role_name);
19+
20+ if (!$role_exists) {
21+ // Create the new role.
22+ $role = \Drupal\user\Entity\Role::create([
23+ 'id' => $role_name,
24+ 'label' => t('CWL'),
25+ 'is_admin' => FALSE,
26+ 'weight' => -8,
27+ ]);
28+ $role->save();
29+
30+ //$role->grantPermission('access content');
31+ //$role->save();
32+ }
33+ }
34+
35+ function ubc_cwl_auth_createVisibilityTaxonomy() {
36+
37+ // Check if the vocabulary already exists.
38+ $vocabulary_id = 'visibility';
39+ $vocabulary = Vocabulary::load($vocabulary_id);
40+
41+ if (!$vocabulary) {
42+ $vocabulary = Vocabulary::create([
43+ 'vid' => $vocabulary_id,
44+ 'name' => t('Visibility'),
45+ 'description' => t('Tag content as either CWL only or general access'),
46+ ]);
47+ $vocabulary->save();
48+ }
1049
1150 // Add Generl and CWL to the Visibility vocab
1251 $terms = [
@@ -18,14 +57,14 @@ function ubc_cwl_auth_install() {
1857 // Check if a term with this name already exists in the vocabulary.
1958 $existing_tids = \Drupal::entityQuery('taxonomy_term')
2059 ->accessCheck(FALSE)
21- ->condition('vid', $vocabulary )
60+ ->condition('vid', $vocabulary_id )
2261 ->condition('name', $name)
2362 ->execute();
2463
2564 if (empty($existing_tids)) {
2665 $term = Term::create([
2766 'name' => $name,
28- 'vid' => $vocabulary ,
67+ 'vid' => $vocabulary_id ,
2968 ]);
3069 $term->save();
3170 $tid = $term->id();
@@ -52,3 +91,4 @@ function ubc_cwl_auth_install() {
5291 $access_storage->addTermPermissionsByRoleIds(['administrator'], $term_ids['CWL']);
5392
5493}
94+
0 commit comments