forked from SU-SWS/stanford_news
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstanford_news.install
198 lines (170 loc) · 6.53 KB
/
stanford_news.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
<?php
/**
* @file
* stanford_news.install
*/
use Drupal\field\Entity\FieldConfig;
use Drupal\Core\Config\FileStorage;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Utility\UpdateException;
use Drupal\Core\Logger\RfcLogLevel;
use Drupal\features\FeaturesManagerInterface;
/**
* Deletes old hero banner field if it's still there.
*/
function stanford_news_update_8101() {
if ($field = FieldConfig::load('node.stanford_news.field_s_news_hero_banner')) {
$field->delete();
}
}
/**
* Install new dependencies.
*/
function stanford_news_update_8102() {
\Drupal::service('module_installer')->install([
'stanford_paragraph_section_highlight_cards',
'stanford_paragraph_responsive_image',
'stanford_paragraph_video',
]);
}
/**
* Updates schema for news by reverting the feature.
*/
function stanford_news_update_8103() {
/** @var \Drupal\features\FeaturesManagerInterface $manager */
$manager = \Drupal::service('features.manager');
/** @var \Drupal\features\Package $feature */
$feature = $manager->loadPackage("stanford_news", TRUE);
$components = $feature->getConfigOrig();
// Determine which config the user wants to import/revert.
$config_to_create = [];
foreach ($components as $component) {
$config_to_create[$component] = '';
}
$manager->createConfiguration($config_to_create);
drupal_flush_all_caches();
}
/**
* Change the subhead and intro fields in to one field with summary.
*/
function stanford_news_update_8104() {
drupal_flush_all_caches();
$database = \Drupal::database();
// Migrate the text from Intro Text to Teaser Text.
$intro_field_name = "body";
$subtitle_field_name = "field_s_news_sub_title";
$teaser_field_name = "field_s_news_teaser";
$summary_field_name = "field_s_news_summary";
$entity_type = "node";
$bundle = "stanford_news";
global $config_directories;
$config_path = $config_directories['sync'] ?? drupal_get_path('module', 'stanford_news') . "/config/install";
$source = new FileStorage($config_path);
$config_storage = \Drupal::service('config.storage');
// Save the summary field to the db.
$storage = new FieldStorageConfig($source->read("field.storage.node.field_s_news_summary"));
$storage->enforceIsNew()->save();
$config_storage->write("field.field.node.stanford_news.field_s_news_summary", $source->read("field.field.node.stanford_news.field_s_news_summary"));
// Save the teaser field to the DB.
$storage = new FieldStorageConfig($source->read("field.storage.node.field_s_news_teaser"));
$storage->enforceIsNew()->save();
$config_storage->write("field.field.node.stanford_news.field_s_news_teaser", $source->read("field.field.node.stanford_news.field_s_news_teaser"));
// Intro text values for news items.
$rows = $database->select("node__body", 'n')
->fields('n')
->condition("bundle", "stanford_news")
->execute()
->fetchAll();
// The table data to restore after the update is completed.
$revision_rows = $database->select("node_revision__body", 'n')
->fields('n')
->condition("bundle", "stanford_news")
->execute()
->fetchAll();
if (!is_null($rows)) {
foreach ($rows as $row) {
$row = (array) $row;
$row[$teaser_field_name . "_value"] = $row['body_value'];
$row[$teaser_field_name . "_format"] = $row['body_format'];
unset($row['body_value']);
unset($row['body_format']);
unset($row['body_summary']);
$database->insert("node__" . $teaser_field_name)->fields($row)->execute();
}
}
if (!is_null($revision_rows)) {
foreach ($revision_rows as $row) {
$row = (array) $row;
$row[$teaser_field_name . "_value"] = $row['body_value'];
$row[$teaser_field_name . "_format"] = $row['body_format'];
unset($row['body_value']);
unset($row['body_format']);
unset($row['body_summary']);
$database->insert("node_revision__" . $teaser_field_name)->fields($row)->execute();
}
}
// Remove the body field entries from the DB.
$database->delete("node__body")
->condition("bundle", "stanford_news")
->execute();
$database->delete("node_revision__body")
->condition("bundle", "stanford_news")
->execute();
// Subheading text values for news items.
$rows = $database->select("node__" . $subtitle_field_name, 'n')
->fields('n')
->execute()
->fetchAll();
// The table data to restore after the update is completed.
$revision_rows = $database->select("node_revision__" . $subtitle_field_name, 'n')
->fields('n')
->execute()
->fetchAll();
if (!is_null($rows)) {
foreach ($rows as $row) {
$row = (array) $row;
$row[$summary_field_name . "_format"] = "content_editor";
$row[$summary_field_name . "_value"] = "<p>" . $row[$subtitle_field_name . "_value"] . "</p>";
unset($row[$subtitle_field_name . "_value"]);
$database->insert("node__" . $summary_field_name)->fields($row)->execute();
}
}
if (!is_null($revision_rows)) {
foreach ($revision_rows as $row) {
$row = (array) $row;
$row[$summary_field_name . "_format"] = "content_editor";
$row[$summary_field_name . "_value"] = "<p>" . $row[$subtitle_field_name . "_value"] . "</p>";
unset($row[$subtitle_field_name . "_value"]);
$database->insert("node_revision__" . $summary_field_name)->fields($row)->execute();
}
}
// Delete the old fields.
$field = FieldConfig::loadByName($entity_type, $bundle, $subtitle_field_name);
$field->delete();
$field = FieldConfig::loadByName($entity_type, $bundle, $intro_field_name);
$field->delete();
field_purge_batch(250);
}
/**
* Patch up some missing information that happened update 8104
*/
function stanford_news_update_8105() {
$entity_type = "node";
$bundle = "stanford_news";
$fields = ["field_s_news_summary", "field_s_news_teaser"];
foreach ($fields as $field_name) {
$field = FieldConfig::loadByName($entity_type, $bundle, $field_name);
$field_keyval = \Drupal::keyValue("config.entity.key_store.field_config");
$field_keyval->set("uuid:" . $field->uuid(), ["field.field" . $field->id()]);
$map_keyval = \Drupal::keyValue("entity.definitions.bundle_field_map");
$bundle_maps = $map_keyval->get($entity_type);
$bundle_maps[$field_name] = [
'type' => 'text_long',
'bundles' => [
$bundle => $bundle,
],
];
$map_keyval->set($entity_type, $bundle_maps);
}
}