From 1daa9870e5a8afde031ae7b238b42f3d3a967a69 Mon Sep 17 00:00:00 2001 From: Marc Berger Date: Thu, 14 Sep 2023 13:12:55 -0700 Subject: [PATCH 1/2] feat: Remove the ability for existing events to add new sections --- .../ys_layouts/src/UpdateExistingNodes.php | 55 +++++++++++++++++++ .../custom/ys_layouts/ys_layouts.deploy.php | 8 +++ 2 files changed, 63 insertions(+) diff --git a/web/profiles/custom/yalesites_profile/modules/custom/ys_layouts/src/UpdateExistingNodes.php b/web/profiles/custom/yalesites_profile/modules/custom/ys_layouts/src/UpdateExistingNodes.php index 7ac26fa551..77a60a922a 100644 --- a/web/profiles/custom/yalesites_profile/modules/custom/ys_layouts/src/UpdateExistingNodes.php +++ b/web/profiles/custom/yalesites_profile/modules/custom/ys_layouts/src/UpdateExistingNodes.php @@ -147,6 +147,61 @@ public function updateExistingPageLock() { } } + /** + * Update existing events to disable adding more sections. + */ + public function updateExistingEventsLock() { + // Find all event nodes to update existing. + $nids = \Drupal::entityQuery('node') + ->accessCheck(FALSE) + ->condition('type', 'event') + ->execute(); + + foreach ($nids as $nid) { + $node = Node::load($nid); + $layout = $node->get('layout_builder__layout'); + + /** @var \Drupal\layout_builder\Field\LayoutSectionItemList $layout */ + $sections = $layout->getSections(); + foreach ($sections as $section) { + if ($section->getLayoutSettings()['label'] == '') { + $section->setThirdPartySetting( + 'layout_builder_lock', + 'lock', + [ + 6 => 6, + 7 => 7, + ] + ); + + $this->updateTempStore(function (&$stored_data) { + if ($stored_data->data['section_storage']->getContext('entity')->getContextData()->getEntity()->bundle() == 'event') { + $section_storage = $stored_data->data['section_storage']; + $sections = $section_storage->getSections(); + foreach ($sections as $key => $section) { + if ($section->getLayoutSettings()['label'] == '') { + $sectionToUpdate = $key; + } + } + $section_storage + ->getSection($sectionToUpdate) + ->setThirdPartySetting( + 'layout_builder_lock', + 'lock', + [ + 6 => 6, + 7 => 7, + ] + ); + } + }); + + $node->save(); + } + } + } + } + /** * Updates Post Meta for existing nodes. */ diff --git a/web/profiles/custom/yalesites_profile/modules/custom/ys_layouts/ys_layouts.deploy.php b/web/profiles/custom/yalesites_profile/modules/custom/ys_layouts/ys_layouts.deploy.php index ccd61394da..e9938e5068 100644 --- a/web/profiles/custom/yalesites_profile/modules/custom/ys_layouts/ys_layouts.deploy.php +++ b/web/profiles/custom/yalesites_profile/modules/custom/ys_layouts/ys_layouts.deploy.php @@ -25,3 +25,11 @@ function ys_layouts_deploy_9001() { // Replaces old title section with new post meta block. $updateExistingNodes->updateExistingPostMeta(); } + +/** + * Updates events to disable adding new sections. + */ +function ys_layouts_deploy_9002() { + $updateExistingNodes = new UpdateExistingNodes(); + $updateExistingNodes->updateExistingEventsLock(); +} From c39bf802a95202c2ee78e165a761266d0414bc03 Mon Sep 17 00:00:00 2001 From: Marc Berger Date: Thu, 14 Sep 2023 15:59:31 -0700 Subject: [PATCH 2/2] fix: Fix error with specifying which sections get updated --- .../ys_layouts/src/UpdateExistingNodes.php | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/web/profiles/custom/yalesites_profile/modules/custom/ys_layouts/src/UpdateExistingNodes.php b/web/profiles/custom/yalesites_profile/modules/custom/ys_layouts/src/UpdateExistingNodes.php index 77a60a922a..59ca2ceb83 100644 --- a/web/profiles/custom/yalesites_profile/modules/custom/ys_layouts/src/UpdateExistingNodes.php +++ b/web/profiles/custom/yalesites_profile/modules/custom/ys_layouts/src/UpdateExistingNodes.php @@ -164,7 +164,7 @@ public function updateExistingEventsLock() { /** @var \Drupal\layout_builder\Field\LayoutSectionItemList $layout */ $sections = $layout->getSections(); foreach ($sections as $section) { - if ($section->getLayoutSettings()['label'] == '') { + if ($section->getLayoutSettings()['label'] != 'Title and Metadata') { $section->setThirdPartySetting( 'layout_builder_lock', 'lock', @@ -179,20 +179,22 @@ public function updateExistingEventsLock() { $section_storage = $stored_data->data['section_storage']; $sections = $section_storage->getSections(); foreach ($sections as $key => $section) { - if ($section->getLayoutSettings()['label'] == '') { + if ($section->getLayoutSettings()['label'] != 'Title and Metadata') { $sectionToUpdate = $key; } } - $section_storage - ->getSection($sectionToUpdate) - ->setThirdPartySetting( - 'layout_builder_lock', - 'lock', - [ - 6 => 6, - 7 => 7, - ] - ); + if (isset($sectionToUpdate)) { + $section_storage + ->getSection($sectionToUpdate) + ->setThirdPartySetting( + 'layout_builder_lock', + 'lock', + [ + 6 => 6, + 7 => 7, + ] + ); + } } });