-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_agreement.install
77 lines (69 loc) · 1.93 KB
/
user_agreement.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
<?php
/**
* @file
* Contains user_agreement.install.
*/
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Component\Utility\Crypt;
/**
* Implements hook_install().
*/
function user_agreement_install() {
$roles = Role::loadMultiple([
RoleInterface::ANONYMOUS_ID,
RoleInterface::AUTHENTICATED_ID,
]);
$grant_permission = 'view published user agreement entities';
foreach ($roles as $role) {
$role->grantPermission($grant_permission);
$role->save();
}
}
/**
* Update user agreement submission entities.
*
* Add field for email hash.
*/
function user_agreement_update_8001() {
$field_storage_definition = BaseFieldDefinition::create("string")
->setLabel(t("User Email Hash"))
->setDescription(t("An hash of the user email"))
->setSettings([
'max_length' => 255,
'text_processing' => 0,
])
->setDefaultValue("")
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'author',
'weight' => 0,
])
->setDisplayOptions("form", [
"type" => "string_textfield",
"weight" => -3,
]);
\Drupal::entityDefinitionUpdateManager()
->installFieldStorageDefinition('email_hash', 'user_agreement_submission', 'user_agreement', $field_storage_definition);
// Update value in existing agreements.
$existing_submissions = \Drupal::entityTypeManager()
->getStorage('user_agreement_submission')
->loadMultiple();
foreach ($existing_submissions as $existing_submission) {
if ($user = $existing_submission->user->entity) {
$email = $user->mail->value;
$hash = Crypt::hashBase64($email);
$existing_submission->set('email_hash', $hash);
$existing_submission->save();
}
}
}
/**
* Delete hanging elements from queue.
*/
function user_agreement_update_8002() {
\Drupal::database()->delete('queue')
->condition('name', 'user_agreement_queue')
->execute();
}