-
Notifications
You must be signed in to change notification settings - Fork 0
/
tide_page.install
283 lines (255 loc) · 9.42 KB
/
tide_page.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?php
/**
* @file
* Tide Page install.
*/
use Drupal\workflows\Entity\Workflow;
use Drupal\search_api\Item\Field;
use Drupal\node\Entity\Node;
/**
* Implements hook_install().
*/
function tide_page_install() {
// Enable Editorial workflow if workflow module is enabled.
$moduleHandler = \Drupal::service('module_handler');
if ($moduleHandler->moduleExists('workflows')) {
$editorial_workflow = Workflow::load('editorial');
if ($editorial_workflow) {
$editorial_workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'page');
$editorial_workflow->save();
}
}
// Add relevant fields to search API index.
_tide_page_add_fields_search_api();
// Enable entity type/bundles for use with scheduled transitions.
if (\Drupal::moduleHandler()->moduleExists('scheduled_transitions')) {
$config_factory = \Drupal::configFactory();
$config = $config_factory->getEditable('scheduled_transitions.settings');
$bundles = $config->get('bundles');
if ($bundles) {
foreach ($bundles as $bundle) {
$enabled_bundles = [];
$enabled_bundles[] = $bundle['bundle'];
}
if (!in_array('page', $enabled_bundles)) {
$bundles[] = ['entity_type' => 'node', 'bundle' => 'page'];
$config->set('bundles', $bundles)->save();
}
}
else {
$bundles[] = ['entity_type' => 'node', 'bundle' => 'page'];
$config->set('bundles', $bundles)->save();
}
}
}
/**
* Implements hook_update_dependencies().
*/
function tide_page_update_dependencies() {
$dependencies = [];
$dependencies['tide_page'][8001] = ['tide_core' => 8001];
$dependencies['tide_page'][8002] = ['tide_core' => 8002];
$dependencies['tide_page'][8006] = ['tide_core' => 8037];
return $dependencies;
}
/**
* Refactor Show Related Content and Show Social Sharing fields.
*/
function tide_page_update_8001() {
module_load_include('inc', 'tide_core', 'includes/helpers');
$config_location = [\Drupal::service('extension.list.module')->getPath('tide_page') . '/config/install'];
_tide_import_single_config('field.field.node.page.field_show_related_content', $config_location);
_tide_import_single_config('field.field.node.page.field_show_social_sharing', $config_location);
$pages = \Drupal::entityTypeManager()->getStorage('node')
->loadByProperties(['type' => 'page']);
if ($pages && count($pages)) {
/** @var \Drupal\node\Entity\Node $node */
foreach ($pages as $node) {
$data_changed = FALSE;
if ($node->hasField('field_show_related_content') && $node->hasField('field_page_show_related')) {
$node->set('field_show_related_content', $node->get('field_page_show_related'));
$data_changed = TRUE;
}
if ($node->hasField('field_show_social_sharing') && $node->hasField('field_page_show_social')) {
$node->set('field_show_social_sharing', $node->get('field_page_show_social'));
$data_changed = TRUE;
}
if ($data_changed) {
$node->setNewRevision(FALSE);
$node->save();
}
}
}
}
/**
* Refactor Featured Image field.
*/
function tide_page_update_8002() {
module_load_include('inc', 'tide_core', 'includes/helpers');
$config_location = [\Drupal::service('extension.list.module')->getPath('tide_page') . '/config/install'];
_tide_import_single_config('field.field.node.page.field_featured_image', $config_location);
$pages = \Drupal::entityTypeManager()->getStorage('node')
->loadByProperties(['type' => 'page']);
if ($pages && count($pages)) {
/** @var \Drupal\node\Entity\Node $node */
foreach ($pages as $node) {
if ($node->hasField('field_featured_image') && $node->hasField('field_page_feature_image')) {
$node->set('field_featured_image', $node->get('field_page_feature_image'));
$node->setNewRevision(FALSE);
$node->save();
}
}
}
}
/**
* Add fields to search API.
*/
function _tide_page_add_fields_search_api() {
$moduleHandler = \Drupal::service('module_handler');
if ($moduleHandler->moduleExists('tide_search')) {
$index_storage = \Drupal::entityTypeManager()
->getStorage('search_api_index');
$index = $index_storage->load('node');
// Index the Intro field.
$field_page_intro = new Field($index, 'field_page_intro_text');
$field_page_intro->setType('text');
$field_page_intro->setPropertyPath('field_page_intro_text');
$field_page_intro->setDatasourceId('entity:node');
$field_page_intro->setLabel('Introduction text');
$index->addField($field_page_intro);
$index->save();
}
}
/**
* Add field_show_topic_term_and_tags.
*/
function tide_page_update_8003() {
module_load_include('inc', 'tide_core', 'includes/helpers');
$config_location = [\Drupal::service('extension.list.module')->getPath('tide_page') . '/config/install'];
_tide_import_single_config('field.field.node.page.field_show_topic_term_and_tags', $config_location);
}
/**
* Updates field_show_topic_term_and_tags default value for current page nodes.
*/
function tide_page_update_8004(&$sandbox) {
if (!isset($sandbox['total'])) {
if ($count = \Drupal::entityQuery('node')
->condition('type', 'page')
->count()
->execute()) {
$sandbox['total'] = $count;
$sandbox['current'] = 0;
$sandbox['#finished'] = $count ? 0 : 1;
}
else {
return;
}
}
$batch = 10;
$page_ids = \Drupal::entityQuery('node')
->condition('type', 'page')
->range($sandbox['current'], $batch)
->execute();
foreach ($page_ids as $page_id) {
$page_node = Node::load($page_id);
if ($page_node->hasField('field_show_topic_term_and_tags')) {
$page_node->set('field_show_topic_term_and_tags', 1);
$page_node->save();
}
$sandbox['current']++;
}
if ($sandbox['total'] == 0) {
$sandbox['#finished'] = 1;
}
else {
$sandbox['#finished'] = ($sandbox['current'] / $sandbox['total']);
}
}
/**
* Enable entity type/bundles for use with scheduled transitions.
*/
function tide_page_update_8005() {
if (\Drupal::moduleHandler()->moduleExists('scheduled_transitions')) {
$config_factory = \Drupal::configFactory();
$config = $config_factory->getEditable('scheduled_transitions.settings');
$bundles = $config->get('bundles');
if ($bundles) {
foreach ($bundles as $bundle) {
$enabled_bundles = [];
$enabled_bundles[] = $bundle['bundle'];
}
if (!in_array('page', $enabled_bundles)) {
$bundles[] = ['entity_type' => 'node', 'bundle' => 'page'];
$config->set('bundles', $bundles)->save();
}
}
else {
$bundles[] = ['entity_type' => 'node', 'bundle' => 'page'];
$config->set('bundles', $bundles)->save();
}
}
}
/**
* Add field display headings for table of contents.
*/
function tide_page_update_8006() {
module_load_include('inc', 'tide_core', 'includes/helpers');
$config_location = [\Drupal::service('extension.list.module')->getPath('tide_page') . '/config/install'];
$new_field = 'field.field.node.page.field_node_display_headings';
$config_read = _tide_read_config($new_field, $config_location, TRUE);
// Obtain the storage manager for field instances.
// Create a new field instance from the yaml configuration and save.
\Drupal::entityManager()->getStorage('field_config')
->create($config_read)
->save();
$config_factory = \Drupal::configFactory();
$config = $config_factory->getEditable('core.entity_form_display.node.page.default');
$dependencies = $config->get('dependencies.config');
if (!in_array('field.field.node.page.field_node_display_headings', $dependencies)) {
$dependencies[] = 'field.field.node.page.field_node_display_headings';
$config->set('dependencies.config', $dependencies);
}
$third_party_settings = $config->get('third_party_settings.field_group.group_other.children');
if (!isset($third_party_settings['field_node_display_headings'])) {
$third_party_settings = [
'field_show_social_sharing',
'field_show_content_rating',
'field_show_table_of_content',
'field_node_display_headings',
];
$config->set('third_party_settings.field_group.group_other.children', $third_party_settings);
}
$table_of_content_weight = $config->get('content.field_show_table_of_content.weight');
$content = $config->get('content');
if (!isset($content['field_node_display_headings'])) {
$content['field_node_display_headings'] = [
'weight' => $table_of_content_weight + 1,
'settings' => [],
'third_party_settings' => [],
'type' => 'options_buttons',
'region' => 'content',
];
$config->set('content', $content);
}
$config->save();
// Add to JSON.
$json_config = $config_factory->getEditable('jsonapi_extras.jsonapi_resource_config.node--page');
$json_content = $json_config->get('resourceFields');
if (!isset($json_content['field_node_display_headings'])) {
$json_content['field_node_display_headings'] = [
'fieldName' => 'field_node_display_headings',
'publicName' => 'field_node_display_headings',
'enhancer' => [
'id' => '',
],
'disabled' => FALSE,
];
$json_config->set('resourceFields', $json_content);
}
$json_config->save();
// Update show table of content label.
$field_config = $config_factory->getEditable('field.field.node.page.field_show_table_of_content');
$description = 'The table of contents is automatically built from the heading structure of your page.';
$field_config->set('description', $description);
$field_config->save();
}