-
Notifications
You must be signed in to change notification settings - Fork 4
/
ami.install
163 lines (146 loc) · 5.3 KB
/
ami.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
/**
* @file
* Contains install and update functions for AMI.
*/
use Drupal\Core\Language\LanguageInterface;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\taxonomy\Entity\Term;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\Entity\FieldConfig;
use Drupal\Component\Utility\Environment;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Database\Database;
use Drupal\Core\Config\Entity\ConfigEntityType;
/**
* Implements hook_install().
*/
function ami_install() {
}
/**
* Update 8901 - Create AMI Set entity.
*/
function ami_update_8901() {
// Code provided by @giancarloby to allow D9 compat.
$schema = Database::getConnection()->schema();
if (!$schema->tableExists('ami_setentity')) {
\Drupal::entityTypeManager()->clearCachedDefinitions();
\Drupal::entityDefinitionUpdateManager()
->installEntityType(\Drupal::entityTypeManager()->getDefinition('ami_set_entity'));
}
else {
return 'AMI Set entity already exists';
}
}
/**
* Update 8902 - Add ZIP file Field for AMI Set entity.
*/
function ami_update_8902() {
$validators = [
'file_validate_extensions' => ['zip'],
'file_validate_size' => [Environment::getUploadMaxSize()],
];
$storage_definition = BaseFieldDefinition::create('file')
->setLabel(new TranslatableMarkup('Attached ZIP file'))
->setDescription(new TranslatableMarkup('A Zip file containing accompanying Files for the Source Data'))
->setSetting('file_extensions', 'zip')
->setSetting('upload_validators', $validators)
->setSetting('uri_scheme', 'private')
->setSetting('file_directory', '/ami/zip')
->setRequired(FALSE)
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'file',
'weight' => -3,
])
->setDisplayOptions('form', [
'type' => 'file',
'description' => [
'theme' => 'file_upload_help',
'description' => new TranslatableMarkup('Source Files for this Set')
],
'settings' => [
'upload_validators' => $validators,
],
'weight' => -3,
])
->setDisplayConfigurable('view', TRUE)
->setDisplayConfigurable('form', TRUE);
\Drupal::entityDefinitionUpdateManager()->installFieldStorageDefinition('zip_file', 'ami_set_entity', 'ami', $storage_definition);
}
/**
* Update 8903 - Install Importer Adapter Config Entity.
*/
function ami_update_8903() {
\Drupal::entityDefinitionUpdateManager()->installEntityType(new ConfigEntityType([
'id' => 'importeradapter',
'label' => new TranslatableMarkup('AMI Importer Adapter'),
'config_prefix' => 'importeradapter',
'admin_permission' => 'administer site configuration',
'entity_keys' => [
'id' => 'id',
'label' => 'label',
'uuid' => 'uuid',
],
'config_export' => [
"id",
"label",
"plugin",
"update_existing",
"target_entity_types",
"active",
"plugin_configuration"
],
]));
}
/**
* Update 8903 - Make Private default upload location for ami Set ZIP files.
*/
function ami_update_8904() {
$field_storage_definition = \Drupal::entityDefinitionUpdateManager()->getFieldStorageDefinition('zip_file', 'ami_set_entity');
$field_storage_definition->setSetting('uri_scheme', 'private');
$field_storage_definition->setSetting('file_directory', '/ami/zip');
\Drupal::entityDefinitionUpdateManager()->updateFieldStorageDefinition($field_storage_definition);
}
/**
* Update 8905 - Add Report CSV file Field for AMI Set entity.
*/
function ami_update_8905() {
$validators = [
'file_validate_extensions' => ['csv'],
'file_validate_size' => [Environment::getUploadMaxSize()],
];
$storage_definition = BaseFieldDefinition::create('file')
->setLabel(t('AMI Process Reports'))
->setDescription(t('Processed set report in CSV format'))
->setSetting('file_extensions', 'csv')
->setSetting('upload_validators', $validators)
->setSetting('uri_scheme', 'private')
->setSetting('file_directory', '/ami/reports')
->setRequired(FALSE)
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'file',
'weight' => -2,
])
->setDisplayConfigurable('view', TRUE)
->setDisplayConfigurable('form', TRUE);
\Drupal::entityDefinitionUpdateManager()->installFieldStorageDefinition('report_file', 'ami_set_entity', 'ami', $storage_definition);
$field_storage_definition_status = \Drupal::entityDefinitionUpdateManager()->getFieldStorageDefinition('status', 'ami_set_entity');
$field_storage_definition_status->setLabel(t("This Set's last known status"));
$field_storage_definition_status->setSetting('allowed_values', \Drupal\ami\Entity\amiSetEntity::STATUS);
$field_storage_definition_status->setSetting('default_value', 'READY');
\Drupal::entityDefinitionUpdateManager()->updateFieldStorageDefinition($field_storage_definition_status);
// Update all AMI sets already with 'ready' use the new uppercase default
$entities_nids = \Drupal::entityQuery('ami_set_entity')
->condition('status', 'ready')
->accessCheck(FALSE)
->execute();
// Load all the articles.
$entities = \Drupal::entityTypeManager()->getStorage('ami_set_entity')->loadMultiple($entities_nids);
foreach ($entities as $entity) {
$entity->setStatus(\Drupal\ami\Entity\amiSetEntity::STATUS_READY);
$entity->save();
}
}