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

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

parent 7e2db7e2
<?php
/**
* @file
* Install, update and uninstall for profiles.
*/
use Drupal\node\Entity\Node;
/**
* Removes all the node fields from the layout.
*/
function uw_ct_profile_update_8101(&$sandbox) {
// The list of components/blocks to be removed.
$components_to_remove = [
'extra_field_block:node:uw_ct_profile:content_moderation_control',
'extra_field_block:node:uw_ct_profile:links',
'field_block:node:uw_ct_profile:field_uw_ct_profile_contact',
'field_block:node:uw_ct_profile:field_uw_ct_profile_sort_name',
'field_block:node:uw_ct_profile:field_uw_ct_profile_image',
'field_block:node:uw_ct_profile:field_uw_ct_profile_affiliation',
'field_block:node:uw_ct_profile:field_uw_ct_profile_title',
'field_block:node:uw_ct_profile:field_uw_ct_profile_info_link',
'field_block:node:uw_ct_profile:field_uw_ct_profile_link_persona',
'field_block:node:uw_ct_profile:field_uw_ct_profile_type',
'field_block:node:uw_ct_profile:field_uw_meta_tags',
'field_block:node:uw_ct_profile:field_uw_meta_image',
'field_block:node:uw_ct_profile:field_uw_meta_description',
'field_block:node:uw_ct_profile:field_uw_profile_summary',
];
// Get all the nids for profiles.
$nids = \Drupal::entityQuery('node')->condition('type', 'uw_ct_profile')->execute();
// Load all the contact 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();
// Step through each of the sections and load the components
// and see if it needs to be removed.
foreach ($sections as $section_id => $section) {
// Get the components for the section.
$components = $section->getComponents();
// Step through each of the components and see if it needs
// to be removed.
foreach ($components as $id => $component) {
// Get the configuration for the component which will have
// the uuid.
$config = $component->get('configuration');
// If the uuid is in the list of components to be removed,
// then remove that component.
if (in_array($config['id'], $components_to_remove)) {
$sections[$section_id]->removeComponent($id);
}
}
}
// Set the new value of the sections, with the removed
// components and save the node.
$node->layout_builder__layout->setValue($sections);
$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->addField('nrlbl', 'delta');
$query->condition('nrlbl.entity_id', $nid);
$query->condition('nrlbl.revision_id', $vid);
$sections = $query->execute()->fetchAll();
// If the sections are not empty, remove fields from layout.
if (!empty($sections)) {
// Step through each section and remove fields.
foreach ($sections as $section) {
// Get the section object.
$section_info = unserialize($section->layout_builder__layout_section);
// Get the components for the section.
$components = $section_info->getComponents();
// Step through each of the components and see if it needs
// to be removed.
foreach ($components as $id => $component) {
// Get the configuration for the component which will have
// the uuid.
$config = $component->get('configuration');
// If the uuid is in the list of components to be removed,
// then remove that component.
if (in_array($config['id'], $components_to_remove)) {
$section_info->removeComponent($id);
}
}
// Update query to remove components.
$query = \Drupal::database()->update('node_revision__layout_builder__layout')
->fields(
[
'layout_builder__layout_section' => serialize($section_info),
]
)
->condition('entity_id', $nid)
->condition('revision_id', $vid)
->condition('delta', $section->delta)
->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