Skip to content
Snippets Groups Projects
Commit eaf156d5 authored by Eric Bremner's avatar Eric Bremner Committed by Kevin Paxman
Browse files

ISTWCMS-5880: fixing update hook for existing sites and media

parent 38a5b6b9
No related branches found
No related tags found
3 merge requests!284Feature/istwcms 5880 ebremner banners above,!274Draft: ISTWCMS-5551: fixing office hours display,!260Feature/istwcms 5668 a5kulkar rename references to publications
......@@ -465,6 +465,46 @@ function uw_cfg_common_update_9105(&$sandbox) {
*/
function uw_cfg_common_update_9106(&$sandbox) {
// The database service.
$database = \Drupal::database();
// The tables to be used for fields to get and remove.
$tables = [
'node__field_uw_type_of_media',
'node_revision__field_uw_type_of_media',
'node__field_uw_hero_image',
'node_revision__field_uw_hero_image',
];
// Step through each of the tables and get the data,
// then truncate them. We need to truncate the table
// so that we are able to modify the fields, if there
// is data in these tables, Drupal throws an error about
// data already existing in the fields. We will insert
// the data back into the tables after everything is
// completed.
foreach ($tables as $table) {
// Get the rows in the table.
$values = $database->select($table, 'n')
->fields('n')
->execute()
->fetchAll();
// Step through each of the rows and add to variable.
foreach ($values as $value) {
// Add to the variable and cast to an array so that
// we are not using an object, just makes it easier
// to work with when inserting these rows back
// into the database.
$node_data[$table][] = (array)$value;
}
// Truncate the table.
$database->truncate($table)->execute();
}
// The content types that are not getting any changes.
$cts_not_to_install = [
'uw_ct_sidebar',
......@@ -630,4 +670,29 @@ function uw_cfg_common_update_9106(&$sandbox) {
$node->save();
}
}
// Step through all the tables and insert back into
// the database.
foreach ($node_data as $table => $data) {
// Get all the rows from the table.
foreach ($data as $field_data) {
// Reset the fields array, so we are working with
// a blank array to insert.
$fields = [];
// Step through all the entries in the row and add
// to the fields array, this makes it much easier to
// do one insert command.
foreach ($field_data as $key => $value) {
$fields[$key] = $value;
}
// Insert the row into the database.
$result = $database->insert($table)
->fields($fields)
->execute();
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment