diff --git a/web/profiles/custom/yalesites_profile/modules/custom/ys_layouts/src/Controller/TestPageController.php b/web/profiles/custom/yalesites_profile/modules/custom/ys_layouts/src/Controller/TestPageController.php deleted file mode 100644 index 1a38fbd8e1..0000000000 --- a/web/profiles/custom/yalesites_profile/modules/custom/ys_layouts/src/Controller/TestPageController.php +++ /dev/null @@ -1,29 +0,0 @@ -updatePageTempStoreLock(); - - return [ - '#markup' => 'Hello, world', - ]; - } - -} 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 deb431bc36..ab4eb96774 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 @@ -19,78 +19,46 @@ class UpdateExistingNodes { use LayoutEntityHelperTrait; /** - * Updates Event Meta for existing nodes. + * Page Meta Section. + * + * @var Drupal\layout_builder\Section */ - public function updateExistingEventMeta() { - - // Gets the main event meta section to clone. - $entityTypeManager = \Drupal::service('entity_type.manager'); - $eventMetaSection = NULL; - - if ($eventViewDisplay = $entityTypeManager->getStorage('entity_view_display')->load('node.event.default')) { - if ($eventViewDisplay->isLayoutBuilderEnabled()) { - $eventSections = $eventViewDisplay->getSections(); - foreach ($eventSections as $section) { - if ($section->getLayoutSettings()['label'] == 'Title and Metadata') { - $eventMetaSection = $section; - } - } - } - } - - if ($eventMetaSection instanceof Section) { - - // Find all event nodes to update existing. - $nids = \Drupal::entityQuery('node')->condition('type', 'event')->execute(); + protected $pageMetaSection; - 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 an overridden layout already contains an Event Meta section, - // remove it from the update list. - if ($section->getLayoutSettings()['label'] == 'Title and Metadata') { - unset($nids[array_search($nid, $nids)]); - } - } - } - - foreach ($nids as $nid) { - $node = Node::load($nid); - $layout = $node->get('layout_builder__layout'); + /** + * Post Meta Section. + * + * @var Drupal\layout_builder\Section + */ + protected $postMetaSection; - /** @var \Drupal\layout_builder\Field\LayoutSectionItemList $layout */ - $layout->insertSection(0, $eventMetaSection); - $this->updateEventTempStore($eventMetaSection); - $node->save(); - } - } - } + /** + * Event Meta Section. + * + * @var Drupal\layout_builder\Section + */ + protected $eventMetaSection; /** - * Updates Event Meta for existing nodes. + * Updates Page Meta for existing nodes. */ public function updateExistingPageMeta() { // Gets the main page meta section to clone. $entityTypeManager = \Drupal::service('entity_type.manager'); - $pageMetaSection = NULL; if ($pageViewDisplay = $entityTypeManager->getStorage('entity_view_display')->load('node.page.default')) { if ($pageViewDisplay->isLayoutBuilderEnabled()) { $pageSections = $pageViewDisplay->getSections(); foreach ($pageSections as $section) { if ($section->getLayoutId() == 'ys_layout_page_meta') { - $pageMetaSection = $section; + $this->pageMetaSection = $section; } } } } - if ($pageMetaSection instanceof Section) { + if ($this->pageMetaSection instanceof Section) { // Find all page nodes to update existing. $nids = \Drupal::entityQuery('node')->condition('type', 'page')->execute(); @@ -117,8 +85,21 @@ public function updateExistingPageMeta() { /** @var \Drupal\layout_builder\Field\LayoutSectionItemList $layout */ // For existing pages, remove the old title and breadcrumb block first. $layout->removeSection(1); - $layout->insertSection(1, $pageMetaSection); - $this->updatePageTempStore($pageMetaSection); + $layout->insertSection(1, $this->pageMetaSection); + + $this->updateTempStore(function (&$stored_data) { + + $section_storage = $stored_data->data['section_storage']; + if ( + $section_storage->getSections()[1]->getLayoutSettings()['label'] == 'Title Section' + && $stored_data->data['section_storage']->getContext('entity')->getContextData()->getEntity()->bundle() == 'page') { + + $section_storage = $stored_data->data['section_storage']; + $section_storage->removeSection(1); + $section_storage->insertSection(1, $this->pageMetaSection); + } + }); + $node->save(); } } @@ -140,7 +121,20 @@ public function updateExistingPageLock() { foreach ($sections as $section) { if ($section->getLayoutSettings()['label'] == 'Content Section') { $section->unsetThirdPartySetting('layout_builder_lock', 'lock'); - $this->updatePageTempStoreLock(); + + $this->updateTempStore(function (&$stored_data) { + if ($stored_data->data['section_storage']->getContext('entity')->getContextData()->getEntity()->bundle() == 'page') { + $section_storage = $stored_data->data['section_storage']; + $sections = $section_storage->getSections(); + foreach ($sections as $key => $section) { + if ($section->getLayoutSettings()['label'] == 'Content Section') { + $sectionToUpdate = $key; + } + } + $section_storage->getSection($sectionToUpdate)->unsetThirdPartySetting('layout_builder_lock', 'lock'); + } + }); + $node->save(); } } @@ -154,20 +148,19 @@ public function updateExistingPostMeta() { // Gets the main page meta section to clone. $entityTypeManager = \Drupal::service('entity_type.manager'); - $postMetaSection = NULL; if ($postViewDisplay = $entityTypeManager->getStorage('entity_view_display')->load('node.post.default')) { if ($postViewDisplay->isLayoutBuilderEnabled()) { $postSections = $postViewDisplay->getSections(); foreach ($postSections as $section) { if ($section->getLayoutSettings()['label'] == 'Title and Metadata') { - $postMetaSection = $section; + $this->postMetaSection = $section; } } } } - if ($postMetaSection instanceof Section) { + if ($this->postMetaSection instanceof Section) { // Find all post nodes to update existing. $nids = \Drupal::entityQuery('node')->condition('type', 'post')->execute(); @@ -194,134 +187,84 @@ public function updateExistingPostMeta() { /** @var \Drupal\layout_builder\Field\LayoutSectionItemList $layout */ // For existing pages, remove the old title and breadcrumb block first. $layout->removeSection(0); - $layout->insertSection(0, $postMetaSection); - $this->updatePostTempStore($postMetaSection); + $layout->insertSection(0, $this->postMetaSection); + + $this->updateTempStore(function (&$stored_data) { + + if ($stored_data->data['section_storage']->getContext('entity')->getContextData()->getEntity()->bundle() == 'post') { + $section_storage = $stored_data->data['section_storage']; + $section_storage->removeSection(0); + $section_storage->insertSection(0, $this->postMetaSection); + } + }); + $node->save(); } } } /** - * Updates Page temp store with new section. + * Updates Event Meta for existing nodes. */ - public function updatePageTempStore(Section $sectionToClone) { - - $db = \Drupal::database(); - if ($db->schema()->tableExists('key_value_expire')) { - $query = $db->select('key_value_expire', 'kve'); - $query = $query->condition('collection', 'tempstore.shared.layout_builder.section_storage.overrides', '=') - ->fields('kve'); - $results = $query->execute(); - foreach ($results as $record) { - $stored_data = unserialize($record->value, [ - 'allowed_classes' => TRUE, - ]); - /** @var \Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage $section_storage */ - $section_storage = $stored_data->data['section_storage']; - - if ( - $section_storage->getSections()[1]->getLayoutSettings()['label'] == 'Title Section' - && $stored_data->data['section_storage']->getContext('entity')->getContextData()->getEntity()->bundle() == 'page') { - - $section_storage->removeSection(1); - $section_storage->insertSection(1, $sectionToClone); + public function updateExistingEventMeta() { - $stored_data->data['section_storage'] = $section_storage; + // Gets the main event meta section to clone. + $entityTypeManager = \Drupal::service('entity_type.manager'); - // Insert the updated tempstore. - $db->update('key_value_expire') - ->condition('name', $record->name, '=') - ->condition('expire', $record->expire, '=') - ->fields([ - 'value' => serialize($stored_data), - ]) - ->execute(); + if ($eventViewDisplay = $entityTypeManager->getStorage('entity_view_display')->load('node.event.default')) { + if ($eventViewDisplay->isLayoutBuilderEnabled()) { + $eventSections = $eventViewDisplay->getSections(); + foreach ($eventSections as $section) { + if ($section->getLayoutSettings()['label'] == 'Title and Metadata') { + $this->eventMetaSection = $section; + } } - } - } - } - /** - * Updates Page temp store with new section. - */ - public function updatePostTempStore(Section $sectionToClone) { + if ($this->eventMetaSection instanceof Section) { - $db = \Drupal::database(); - if ($db->schema()->tableExists('key_value_expire')) { - $query = $db->select('key_value_expire', 'kve'); - $query = $query->condition('collection', 'tempstore.shared.layout_builder.section_storage.overrides', '=') - ->fields('kve'); - $results = $query->execute(); - foreach ($results as $record) { - $stored_data = unserialize($record->value, [ - 'allowed_classes' => TRUE, - ]); - /** @var \Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage $section_storage */ - $section_storage = $stored_data->data['section_storage']; - - if ($stored_data->data['section_storage']->getContext('entity')->getContextData()->getEntity()->bundle() == 'post') { - - $section_storage->removeSection(0); - $section_storage->insertSection(0, $sectionToClone); + // Find all event nodes to update existing. + $nids = \Drupal::entityQuery('node')->condition('type', 'event')->execute(); - $stored_data->data['section_storage'] = $section_storage; + foreach ($nids as $nid) { + $node = Node::load($nid); + $layout = $node->get('layout_builder__layout'); + /** @var \Drupal\layout_builder\Field\LayoutSectionItemList $layout */ + $sections = $layout->getSections(); - // Insert the updated tempstore. - $db->update('key_value_expire') - ->condition('name', $record->name, '=') - ->condition('expire', $record->expire, '=') - ->fields([ - 'value' => serialize($stored_data), - ]) - ->execute(); + foreach ($sections as $section) { + // If an overridden layout already contains an Event Meta section, + // remove it from the update list. + if ($section->getLayoutSettings()['label'] == 'Title and Metadata') { + unset($nids[array_search($nid, $nids)]); + } } - } - } - } + foreach ($nids as $nid) { + $node = Node::load($nid); + $layout = $node->get('layout_builder__layout'); - /** - * Updates Event temp store with new section. - */ - public function updateEventTempStore(Section $sectionToClone) { + /** @var \Drupal\layout_builder\Field\LayoutSectionItemList $layout */ + $layout->insertSection(0, $this->eventMetaSection); - $db = \Drupal::database(); - if ($db->schema()->tableExists('key_value_expire')) { - $query = $db->select('key_value_expire', 'kve'); - $query = $query->condition('collection', 'tempstore.shared.layout_builder.section_storage.overrides', '=') - ->fields('kve'); - $results = $query->execute(); - foreach ($results as $record) { - $stored_data = unserialize($record->value, [ - 'allowed_classes' => TRUE, - ]); - /** @var \Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage $section_storage */ - $section_storage = $stored_data->data['section_storage']; - if ($stored_data->data['section_storage']->getContext('entity')->getContextData()->getEntity()->bundle() == 'event') { - $section_storage->insertSection(0, $sectionToClone); - - $stored_data->data['section_storage'] = $section_storage; - - // Insert the updated tempstore. - $db->update('key_value_expire') - ->condition('name', $record->name, '=') - ->condition('expire', $record->expire, '=') - ->fields([ - 'value' => serialize($stored_data), - ]) - ->execute(); - } + $this->updateTempStore(function (&$stored_data) { + if ($stored_data->data['section_storage']->getContext('entity')->getContextData()->getEntity()->bundle() == 'event') { + $section_storage = $stored_data->data['section_storage']; + $section_storage->insertSection(0, $this->eventMetaSection); + } + }); + + $node->save(); } } } /** - * Updates Event temp store with new section. + * Updates temp store. */ - public function updatePageTempStoreLock() { + public function updateTempStore(callable $process) { $db = \Drupal::database(); if ($db->schema()->tableExists('key_value_expire')) { @@ -335,27 +278,20 @@ public function updatePageTempStoreLock() { ]); /** @var \Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage $section_storage */ $section_storage = $stored_data->data['section_storage']; - if ($stored_data->data['section_storage']->getContext('entity')->getContextData()->getEntity()->bundle() == 'page') { - $sections = $section_storage->getSections(); - foreach ($sections as $key => $section) { - if ($section->getLayoutSettings()['label'] == 'Content Section') { - $sectionToUpdate = $key; - } - } - $section_storage->getSection($sectionToUpdate)->unsetThirdPartySetting('layout_builder_lock', 'lock'); - - $stored_data->data['section_storage'] = $section_storage; - - // Insert the updated tempstore. - $db->update('key_value_expire') - ->condition('name', $record->name, '=') - ->condition('expire', $record->expire, '=') - ->fields([ - 'value' => serialize($stored_data), - ]) - ->execute(); - } + call_user_func_array($process, [&$stored_data]); + $stored_data->data['section_storage'] = $section_storage; + + // Insert the updated tempstore. + $db->update('key_value_expire') + ->condition('name', $record->name, '=') + ->condition('expire', $record->expire, '=') + ->fields([ + 'value' => serialize($stored_data), + ]) + ->execute(); + } + } } diff --git a/web/profiles/custom/yalesites_profile/modules/custom/ys_layouts/ys_layouts.routing.yml b/web/profiles/custom/yalesites_profile/modules/custom/ys_layouts/ys_layouts.routing.yml deleted file mode 100644 index 50cb0b58a6..0000000000 --- a/web/profiles/custom/yalesites_profile/modules/custom/ys_layouts/ys_layouts.routing.yml +++ /dev/null @@ -1,7 +0,0 @@ -ys_layouts.test_page: - path: '/ys-layouts/test-page' - defaults: - _controller: '\Drupal\ys_layouts\Controller\TestPageController::testPage' - _title: 'Testing Page Only' - requirements: - _permission: 'access content'