From 0d1ec686ebe1bdc89c0faf06e98a387df3a3f750 Mon Sep 17 00:00:00 2001 From: Marc Berger Date: Fri, 21 Jul 2023 12:35:15 -0700 Subject: [PATCH] feat: Replace title with new title and metadata block and layout for posts --- ....entity_view_display.node.post.default.yml | 17 ++- .../src/Controller/TestPageController.php | 2 +- .../src/Plugin/Block/PageMetaBlock.php | 1 - .../src/Plugin/Block/PostMetaBlock.php | 105 +++++++++++++++++ .../UpdateExisting/UpdatePageTwoColumn.php | 111 ------------------ .../ys_layouts/src/UpdateExistingNodes.php | 56 +++++++++ .../templates/ys-post-meta-block.html.twig | 5 + .../custom/ys_layouts/ys_layouts.deploy.php | 3 + .../custom/ys_layouts/ys_layouts.module | 6 + 9 files changed, 191 insertions(+), 115 deletions(-) create mode 100644 web/profiles/custom/yalesites_profile/modules/custom/ys_layouts/src/Plugin/Block/PostMetaBlock.php delete mode 100644 web/profiles/custom/yalesites_profile/modules/custom/ys_layouts/src/UpdateExisting/UpdatePageTwoColumn.php create mode 100644 web/profiles/custom/yalesites_profile/modules/custom/ys_layouts/templates/ys-post-meta-block.html.twig diff --git a/web/profiles/custom/yalesites_profile/config/sync/core.entity_view_display.node.post.default.yml b/web/profiles/custom/yalesites_profile/config/sync/core.entity_view_display.node.post.default.yml index d440315962..aa379e74dc 100644 --- a/web/profiles/custom/yalesites_profile/config/sync/core.entity_view_display.node.post.default.yml +++ b/web/profiles/custom/yalesites_profile/config/sync/core.entity_view_display.node.post.default.yml @@ -19,6 +19,7 @@ dependencies: - layout_builder_restrictions - layout_discovery - user + - ys_layouts third_party_settings: layout_builder: enabled: true @@ -27,9 +28,20 @@ third_party_settings: - layout_id: layout_onecol layout_settings: - label: 'Title Section' + label: 'Title and Metadata' context_mapping: { } - components: { } + components: + 6abf6a0c-ae3d-47f8-bf30-08e382c90124: + uuid: 6abf6a0c-ae3d-47f8-bf30-08e382c90124 + region: content + configuration: + id: post_meta_block + label: 'Post Meta Block' + label_display: '' + provider: ys_layouts + context_mapping: { } + weight: 0 + additional: { } third_party_settings: layout_builder_lock: lock: @@ -39,6 +51,7 @@ third_party_settings: 4: 4 5: 5 6: 6 + 7: 7 8: 8 - layout_id: layout_onecol 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 index 624af71279..9176c08bc1 100644 --- 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 @@ -19,7 +19,7 @@ class TestPageController extends ControllerBase { public function testPage() { $updateExistingNodes = new UpdateExistingNodes(); - $updateExistingNodes->updateExistingPageLock(); + $updateExistingNodes->updateExistingPostMeta(); return [ '#markup' => 'Hello, world', diff --git a/web/profiles/custom/yalesites_profile/modules/custom/ys_layouts/src/Plugin/Block/PageMetaBlock.php b/web/profiles/custom/yalesites_profile/modules/custom/ys_layouts/src/Plugin/Block/PageMetaBlock.php index 9f8019abaf..d865f4ff4d 100644 --- a/web/profiles/custom/yalesites_profile/modules/custom/ys_layouts/src/Plugin/Block/PageMetaBlock.php +++ b/web/profiles/custom/yalesites_profile/modules/custom/ys_layouts/src/Plugin/Block/PageMetaBlock.php @@ -91,7 +91,6 @@ public function build() { // Get the page title. if ($route) { $page_title = $this->titleResolver->getTitle($request, $route); - }; return [ diff --git a/web/profiles/custom/yalesites_profile/modules/custom/ys_layouts/src/Plugin/Block/PostMetaBlock.php b/web/profiles/custom/yalesites_profile/modules/custom/ys_layouts/src/Plugin/Block/PostMetaBlock.php new file mode 100644 index 0000000000..f518fd98b8 --- /dev/null +++ b/web/profiles/custom/yalesites_profile/modules/custom/ys_layouts/src/Plugin/Block/PostMetaBlock.php @@ -0,0 +1,105 @@ +routeMatch = $route_match; + $this->titleResolver = $title_resolver; + $this->requestStack = $request_stack; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('current_route_match'), + $container->get('title_resolver'), + $container->get('request_stack'), + ); + } + + /** + * {@inheritdoc} + */ + public function build() { + + /** @var \Drupal\node\NodeInterface $node */ + $node = $this->routeMatch->getParameter('node'); + if (!($node instanceof NodeInterface)) { + return []; + } + + // Post fields. + $title = $node->getTitle(); + $publishDate = strtotime($node->field_publish_date->first()->getValue()['value']); + $dateFormatted = \Drupal::service('date.formatter')->format($publishDate, '', 'c'); + return [ + '#theme' => 'ys_post_meta_block', + '#label' => $title, + '#date_formatted' => $dateFormatted, + ]; + } + +} diff --git a/web/profiles/custom/yalesites_profile/modules/custom/ys_layouts/src/UpdateExisting/UpdatePageTwoColumn.php b/web/profiles/custom/yalesites_profile/modules/custom/ys_layouts/src/UpdateExisting/UpdatePageTwoColumn.php deleted file mode 100644 index 9592f88a07..0000000000 --- a/web/profiles/custom/yalesites_profile/modules/custom/ys_layouts/src/UpdateExisting/UpdatePageTwoColumn.php +++ /dev/null @@ -1,111 +0,0 @@ -condition('type', 'page')->execute(); -//kint($nids); - - // $node = Node::load(99); - // $layout = $node->get('layout_builder__layout'); - // $sections = $layout->getSections(); - // foreach ($sections as $section) { - // if ($section->getLayoutSettings()['label'] == 'Content Section') { - // kint($section->getThirdPartySettings('layout_builder_lock')); - // } - // } - - - $node = Node::load(59); - - // $section_storage = $this->getSectionStorageForEntity($node); - // $tempStore = \Drupal::service('layout_builder.tempstore_repository'); - // $tempStore->delete($section_storage); - - $tempStore = \Drupal::service('tempstore.shared')->get('layout_builder.section_storage.overrides'); - kint($tempStore->get('node.59.default.en')); - //$tempStore->delete('tempstore.shared.layout_builder.section_storage.overrides'); - - $layout = $node->get('layout_builder__layout'); - $sections = $layout->getSections(); - foreach ($sections as $section) { - if ($section->getLayoutSettings()['label'] == 'Content Section') { - kint($section->getThirdPartySettings('layout_builder_lock')); - $section->setThirdPartySetting('layout_builder_lock', 'lock', []); - } - } - $node->save(); - - - - // 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'] == 'Event Meta') { - // // unset($nids[array_search($nid, $nids)]); - // // } - // // } - // } - - // if ($eventMetaSection instanceof Section) { - - // // Find all event nodes to update existing. - // $nids = \Drupal::entityQuery('node')->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 an overridden layout already contains an Event Meta section, - // // remove it from the update list. - // if ($section->getLayoutSettings()['label'] == 'Event Meta') { - // unset($nids[array_search($nid, $nids)]); - // } - // } - // } - - // foreach ($nids as $nid) { - // $node = Node::load($nid); - // $layout = $node->get('layout_builder__layout'); - - // $section_storage = $this->getSectionStorageForEntity($node); - // $tempStore = \Drupal::service('layout_builder.tempstore_repository'); - // /** @var \Drupal\layout_builder\Field\LayoutSectionItemList $layout */ - // $layout->insertSection(0, $eventMetaSection); - // $tempStore->set($section_storage); - // $node->save(); - // } - // } - } - -} 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 bc9227eb7b..cf95ae958f 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 @@ -153,4 +153,60 @@ public function updateExistingPageLock() { } } + /** + * Updates Post Meta for existing nodes. + */ + 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; + } + } + } + } + + if ($postMetaSection instanceof Section) { + + // Find all post nodes to update existing. + $nids = \Drupal::entityQuery('node')->condition('type', 'post')->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 an overridden layout already contains an Page 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'); + + $section_storage = $this->getSectionStorageForEntity($node); + $tempStore = \Drupal::service('layout_builder.tempstore_repository'); + /** @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); + $tempStore->set($section_storage); + $node->save(); + } + } + } + } diff --git a/web/profiles/custom/yalesites_profile/modules/custom/ys_layouts/templates/ys-post-meta-block.html.twig b/web/profiles/custom/yalesites_profile/modules/custom/ys_layouts/templates/ys-post-meta-block.html.twig new file mode 100644 index 0000000000..e27e61d633 --- /dev/null +++ b/web/profiles/custom/yalesites_profile/modules/custom/ys_layouts/templates/ys-post-meta-block.html.twig @@ -0,0 +1,5 @@ +{% include "@molecules/page-title/yds-page-title.twig" with { + page_title__heading: label, + page_title__meta: content.field_author|render ~ date_formatted|date("l, F j, Y"), + page_title__width: 'content', +} %} 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 2f75c00fec..ccd61394da 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 @@ -21,4 +21,7 @@ function ys_layouts_deploy_9001() { // Adds "Add section" to existing pages. $updateExistingNodes->updateExistingPageLock(); + + // Replaces old title section with new post meta block. + $updateExistingNodes->updateExistingPostMeta(); } diff --git a/web/profiles/custom/yalesites_profile/modules/custom/ys_layouts/ys_layouts.module b/web/profiles/custom/yalesites_profile/modules/custom/ys_layouts/ys_layouts.module index b3a340901d..f5757ead54 100644 --- a/web/profiles/custom/yalesites_profile/modules/custom/ys_layouts/ys_layouts.module +++ b/web/profiles/custom/yalesites_profile/modules/custom/ys_layouts/ys_layouts.module @@ -31,5 +31,11 @@ function ys_layouts_theme($existing, $type, $theme, $path): array { 'page_title_display' => 'visible', ], ], + 'ys_post_meta_block' => [ + 'variables' => [ + 'label' => NULL, + 'date_formatted' => NULL, + ], + ], ]; }