-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
patternkit.install
187 lines (172 loc) · 6.34 KB
/
patternkit.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
/**
* @file
* Install file for patternkit module.
*/
use Drupal\Core\Cache\Cache;
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Entity\ContentEntityType;
use Drupal\Core\Entity\EntityAccessControlHandler;
use Drupal\Core\Entity\EntityListBuilder;
use Drupal\Core\Entity\EntityViewBuilder;
use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
use Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\patternkit\Commands\PatternkitCommands;
use Drupal\patternkit\Entity\Pattern;
use Drupal\patternkit\Entity\PatternRouteProvider;
use Drupal\views\EntityViewsData;
/**
* Implements hook_requirements().
*/
function patternkit_requirements($phase): array {
$requirements = [];
if (version_compare(\Drupal::VERSION, 8.9, '<')) {
$requirements['patternkit_drupal_version'] = [
'title' => t('Patternkit Drupal Version'),
'value' => 8.9,
'description' => t('Running a Drupal version prior to 8.9 can cause issues with schema, caching, and display. Upgrade as soon as possible, or see the README.md for patches you should apply.'),
'severity' => REQUIREMENT_WARNING,
];
}
return $requirements;
}
/**
* Allows upgrading from dev version automatically.
*
* @throws \Drupal\Core\Entity\Exception\EntityTypeIdLengthException
*/
function patternkit_update_8001(&$sandbox) {
// Installs new entities if necessary.
$entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager();
if (!$entity_definition_update_manager->getEntityType('patternkit_pattern')) {
$pattern_entity_type = new ContentEntityType([
'id' => 'patternkit_pattern',
'class' => Pattern::class,
'label' => t('Pattern'),
'label_collection' => t('Pattern Libraries'),
'label_plural' => t('Patterns'),
'label_singular' => t('Pattern'),
'label_count' => [
'singular' => t('@count patterns'),
'plural' => t('@count custom blocks'),
],
'admin_permission' => 'administer blocks',
'base_table' => 'pattern',
'revision_table' => 'pattern_revision',
'entity_keys' => [
'id' => 'id',
'hash' => 'hash',
'assets' => 'assets',
'category' => 'category',
'label' => 'name',
'library' => 'library',
'libraryPluginId' => 'libraryPluginId',
'path' => 'path',
'revision' => 'revision',
'schema' => 'schema',
'template' => 'template',
'uuid' => 'uuid',
'version' => 'version',
],
'revision_metadata_keys' => [
'revision_default' => 'revision_default',
'revision_user' => 'revision_user',
'revision_created' => 'revision_created',
'revision_log_message' => 'revision_log_message',
],
'handlers' => [
'access' => EntityAccessControlHandler::class,
'storage' => SqlContentEntityStorage::class,
'storage_schema' => SqlContentEntityStorageSchema::class,
'view_builder' => EntityViewBuilder::class,
'views_data' => EntityViewsData::class,
'route_provider' => [
'html' => PatternRouteProvider::class,
],
'list_builder' => EntityListBuilder::class,
],
'links' => [
'canonical' => '/patternkit/{patternkit_pattern}',
'collection' => '/patternkit',
],
'translatable' => FALSE,
]);
$entity_definition_update_manager->installFieldableEntityType($pattern_entity_type, Pattern::baseFieldDefinitions($pattern_entity_type));
}
// Upgrades existing patterns.
$command = new PatternkitCommands();
$command->setLogger(\Drupal::logger('patternkit'));
$command->devUpdate();
// Clears caches.
$user_caches = [
'apcu_clear_cache',
'wincache_ucache_clear',
'xcache_clear_cache',
'opcache_reset',
];
array_map('call_user_func', array_filter($user_caches, 'is_callable'));
foreach (Cache::getBins() as $bin) {
$bin->deleteAll();
}
\Drupal::service('page_cache_kill_switch')->trigger();
drupal_flush_all_caches();
}
/**
* Adds proper revision metadata keys to Patternkit.
*/
function patternkit_update_8002(&$sandbox) {
$entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager();
$entity_type = $entity_definition_update_manager->getEntityType('patternkit_pattern');
$metadata_keys = $entity_type->get('revision_metadata_keys');
$metadata_keys = is_array($metadata_keys) ? $metadata_keys : [];
$metadata_keys = [
'revision_default' => 'revision_default',
'revision_user' => 'revision_user',
'revision_created' => 'revision_created',
'revision_log_message' => 'revision_log_message',
] + $metadata_keys;
$entity_type->set('revision_metadata_keys', $metadata_keys);
$entity_definition_update_manager->updateEntityType($entity_type);
}
/**
* Adds reverse lookup pattern id field for Patternkit reusable blocks.
*/
function patternkit_update_8003(&$sandbox) {
$entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager();
$entity_type = $entity_definition_update_manager->getEntityType('patternkit_block');
$entity_keys = $entity_type->get('entity_keys');
$entity_keys['pattern'] = 'pattern_id';
$entity_type->set('entity_keys', $entity_keys);
$pattern_id_field_definition = BaseFieldDefinition::create('string')
->setLabel(t('Patternkit Pattern Derivative ID'))
->setDescription(t('The machine name of the Patternkit pattern.'));
$entity_definition_update_manager
->installFieldStorageDefinition('pattern_id',
'patternkit_block', 'patternkit', $pattern_id_field_definition);
}
/**
* Sets default values for new json-editor settings.
*/
function patternkit_update_8004(&$sandbox) {
$config_key = 'patternkit.settings';
$patternkit_module_path = \Drupal::service('extension.list.module')->getPath('patternkit');
$source = new FileStorage($patternkit_module_path . '/config/install');
$config_install = $source->read($config_key);
$config_current = \Drupal::configFactory()->getEditable($config_key);
$new_keys = [
'patternkit_json_editor_use_shadow_dom',
'patternkit_json_editor_wysiwyg',
'patternkit_json_editor_ckeditor_toolbar',
];
$changed = FALSE;
foreach ($new_keys as $new_key) {
if (is_null($config_current->get($new_key))) {
$config_current->set($new_key, $config_install[$new_key]);
$changed = TRUE;
}
}
if ($changed) {
$config_current->save();
}
}