-
Notifications
You must be signed in to change notification settings - Fork 0
/
emr.install
139 lines (124 loc) · 3.93 KB
/
emr.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
/**
* @file
* Entity Meta Relation install file.
*/
use Drupal\Core\Field\BaseFieldDefinition;
/**
* Implements hook_schema().
*/
function emr_schema() {
$schema = [];
$schema['entity_meta_default_revision'] = [
'description' => 'Keeps track of the default entity meta revision.',
'fields' => [
'entity_meta_id' => [
'description' => 'The entity meta identifier.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
],
'default_revision_id' => [
'description' => 'The revision ID that is the default.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
],
],
'indexes' => [
'default_revision' => [
'default_revision_id',
'entity_meta_id',
],
],
'primary key' => ['entity_meta_id'],
];
return $schema;
}
/**
* Create the `emr_default_revision` field.
*/
function emr_update_8001(&$sandbox) {
$field = BaseFieldDefinition::create('boolean')
->setRevisionable(TRUE)
->setLabel(t('Default revision'))
->setDescription(t('A boolean indicating whether the entity meta revision maps to the default revision of the host entity.'))
->setDefaultValue(FALSE);
\Drupal::entityDefinitionUpdateManager()
->installFieldStorageDefinition('emr_default_revision', 'entity_meta', 'emr', $field);
}
/**
* Create a 'entity_meta_default_revision' table for default revision tracking.
*/
function emr_update_8002(&$sandbox) {
$schema = [
'description' => 'Keeps track of the default entity meta revision.',
'fields' => [
'entity_meta_id' => [
'description' => 'The entity meta identifier.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
],
'default_revision_id' => [
'description' => 'The revision ID that is the default one.',
'type' => 'int',
'not null' => TRUE,
],
],
'indexes' => [
'default_revision' => [
'default_revision_id',
'entity_meta_id',
],
],
'primary key' => ['entity_meta_id'],
];
\Drupal::database()->schema()->createTable('entity_meta_default_revision', $schema);
}
/**
* Update the EMR default revisions to use the tracking table.
*/
function emr_update_8003(&$sandbox) {
// Update the revisions.
/** @var \Drupal\emr\EntityMetaStorageInterface $storage */
$storage = \Drupal::entityTypeManager()->getStorage('entity_meta');
if (!isset($sandbox['total'])) {
$ids = $storage->getQuery()
->accessCheck(FALSE)
->allRevisions()
->condition('emr_default_revision', 1)
->execute();
if (!$ids) {
return t('No entity meta entities need to be updated.');
}
$sandbox['ids'] = array_keys($ids);
$sandbox['total'] = count($ids);
$sandbox['current'] = 0;
$sandbox['items_per_batch'] = 10;
}
// Get a slice from the ids.
$ids = array_slice($sandbox['ids'], $sandbox['current'], $sandbox['items_per_batch']);
$revisions = $storage->loadMultipleRevisions($ids);
foreach ($revisions as $revision) {
\Drupal::database()->insert('entity_meta_default_revision')
->fields([
'entity_meta_id' => $revision->id(),
'default_revision_id' => $revision->getRevisionId(),
])
->execute();
$sandbox['current']++;
}
$sandbox['#finished'] = empty($sandbox['total']) ? 1 : ($sandbox['current'] / $sandbox['total']);
if ($sandbox['#finished'] === 1) {
return t('A total of @current entity meta revision defaults have been tracked.', ['@current' => $sandbox['current']]);
}
}
/**
* Uninstall the old `emr_default_revision` field.
*/
function emr_update_8004(&$sandbox) {
$entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager();
$storage_definition = $entity_definition_update_manager->getFieldStorageDefinition('emr_default_revision', 'entity_meta');
$entity_definition_update_manager->uninstallFieldStorageDefinition($storage_definition);
}