Commit d8bcf1d2 authored by Eric Bremner's avatar Eric Bremner
Browse files

ISTWCMS-4704: adding update hook to remove fields from layout

parent e22977d8
......@@ -72,3 +72,91 @@ function _uw_ct_event_term_create($term, $vocabulary, array $parent = []) {
// Return the taxonomy term id.
return $new_term->id();
}
/**
* @file
* Install, update and uninstall for UW blogs.
*/
use Drupal\node\Entity\Node;
/**
* Removes the first and last section from events.
*/
function uw_ct_event_update_8101(&$sandbox) {
// Get all the nids for blogs.
$nids = \Drupal::entityQuery('node')->condition('type', 'uw_ct_event')->execute();
// Load all the blog nodes.
$nodes = Node::loadMultiple($nids);
// Step through each of the nodes and remove the sections
// that were locked in fields before. This will only remove
// the layouts for the current revision.
foreach ($nodes as $node) {
// Load the sections.
$sections = $node->get('layout_builder__layout')->getSections();
// If the node has at least 3 sections, remove the
// first and last.
if (count($sections) > 2) {
// Remove first and last section.
unset($sections[0]);
unset($sections[count($sections)]);
// Set the updated sections.
$node->layout_builder__layout->setValue($sections);
// Save the node.
$node->save();
}
// Get the node id from the node object.
$nid = $node->id();
// Get all the revisions for the nid.
$query = \Drupal::database()->select('node_revision', 'nr');
$query->addField('nr', 'vid');
$query->condition('nr.nid', $nid);
$revisions = $query->execute()->fetchAll();
// Step through each of the revisions and remove the sections.
foreach ($revisions as $revision) {
// Get the vid.
$vid = $revision->vid;
// Get all the sections for the revision.
$query = \Drupal::database()->select('node_revision__layout_builder__layout', 'nrlbl');
$query->addField('nrlbl', 'layout_builder__layout_section');
$query->condition('nrlbl.entity_id', $nid);
$query->condition('nrlbl.revision_id', $vid);
$sections = $query->execute()->fetchAll();
// If the sections are not empty, remove first and last.
if (!empty($sections)) {
// Get the last section.
$last = count($sections) - 1;
// Delete the first section.
$query = \Drupal::database()->delete('node_revision__layout_builder__layout');
$query->condition('entity_id', $nid);
$query->condition('revision_id', $vid);
$query->condition('delta', 0);
$query->execute();
// Delete the last section.
$query = \Drupal::database()->delete('node_revision__layout_builder__layout');
$query->condition('entity_id', $nid);
$query->condition('revision_id', $vid);
$query->condition('delta', $last);
$query->execute();
}
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment