From 12e3ca0a03b77bd2a166c236ea2994f1195b1e6b Mon Sep 17 00:00:00 2001 From: Nathaniel Woodland Date: Thu, 20 Jun 2024 14:54:38 -0400 Subject: [PATCH 1/3] nys-156: reenable microsite page caching --- web/themes/custom/nysenate_theme/nysenate_theme.theme | 6 ------ 1 file changed, 6 deletions(-) diff --git a/web/themes/custom/nysenate_theme/nysenate_theme.theme b/web/themes/custom/nysenate_theme/nysenate_theme.theme index 958243d30..e52698301 100644 --- a/web/themes/custom/nysenate_theme/nysenate_theme.theme +++ b/web/themes/custom/nysenate_theme/nysenate_theme.theme @@ -5027,12 +5027,6 @@ function get_item_cta($node) { * Implements hook_preprocess_page(). */ function nysenate_theme_preprocess_node__microsite_page__full(&$variables) { - // Disable page caching, to ensure field_block modifications in - // nysenate_theme_preprocess_page__node__microsite_page always run. - // @todo reimplement field_block modifications and turn page caching back on. - $variables['#cache']['max-age'] = 0; - \Drupal::service('page_cache_kill_switch')->trigger(); - $node = \Drupal::routeMatch()->getParameter('node'); if (!empty($node) && $node->hasField('field_microsite_page_type') && From 09f4bd873c4be49ae8f5fa715373d3662b7f8cf6 Mon Sep 17 00:00:00 2001 From: Nathaniel Woodland Date: Wed, 26 Jun 2024 14:17:17 -0400 Subject: [PATCH 2/3] nys-156: move field_block render array manipulations from node preprocess to field preprocess, to avoid caching issues --- .../nysenate_theme/nysenate_theme.theme | 29 +++++++++++++++---- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/web/themes/custom/nysenate_theme/nysenate_theme.theme b/web/themes/custom/nysenate_theme/nysenate_theme.theme index e52698301..7c322fe06 100644 --- a/web/themes/custom/nysenate_theme/nysenate_theme.theme +++ b/web/themes/custom/nysenate_theme/nysenate_theme.theme @@ -2912,6 +2912,28 @@ function get_issue_card($term) { return $issue_card; } +/** + * Implements hook_preprocess_HOOK(). + */ +function nysenate_theme_preprocess_field__node__field_block(&$variables) { + // Remove microsite_hero and senator_microsite_menu blocks from field + // rendering, as these blocks are rendered directly through + // page--node--microsite-page.html.twig + foreach ($variables['items'] ?? [] as $key => $item) { + $block_types = [ + 'microsite_hero', + 'senator_microsite_menu', + ]; + /** @var \Drupal\block_content\Entity\BlockContent $block_content */ + $block_content = ($item['content']['#block_content'] ?? []); + if ($block_content instanceof BlockContent) { + if (in_array($block_content->bundle(), $block_types)) { + unset($variables['items'][$key]); + } + } + } +} + /** * Implements hook_preprocess_page__node__TYPE(). */ @@ -2930,7 +2952,8 @@ function nysenate_theme_preprocess_page__node__microsite_page(&$variables) { $block_content = $value->entity; if ($block_content instanceof BlockContent) { - // Alter the field_block contents to be displayed. + // Provide microsite_hero and senator_microsite_menu blocks directly to + // page--node--microsite-page.html.twig. if (in_array($block_content->bundle(), $block_types)) { if ($block_content->bundle() == 'microsite_hero') { $variables['microsite_hero'] = \Drupal::entityTypeManager() @@ -2942,10 +2965,6 @@ function nysenate_theme_preprocess_page__node__microsite_page(&$variables) { ->getViewBuilder('block_content') ->view($block_content); } - - // Set both microsite blocks to empty array - // to prevent from rendering in the display. - $node->get('field_block')[$key] = []; } } } From 923ef0a991224d6a095668c84f183eda2d344569 Mon Sep 17 00:00:00 2001 From: Nathaniel Woodland Date: Thu, 27 Jun 2024 14:52:07 -0400 Subject: [PATCH 3/3] nys-156: code clean-up --- web/themes/custom/nysenate_theme/nysenate_theme.theme | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/web/themes/custom/nysenate_theme/nysenate_theme.theme b/web/themes/custom/nysenate_theme/nysenate_theme.theme index 7c322fe06..98c3aa5b2 100644 --- a/web/themes/custom/nysenate_theme/nysenate_theme.theme +++ b/web/themes/custom/nysenate_theme/nysenate_theme.theme @@ -2919,11 +2919,11 @@ function nysenate_theme_preprocess_field__node__field_block(&$variables) { // Remove microsite_hero and senator_microsite_menu blocks from field // rendering, as these blocks are rendered directly through // page--node--microsite-page.html.twig - foreach ($variables['items'] ?? [] as $key => $item) { - $block_types = [ - 'microsite_hero', - 'senator_microsite_menu', - ]; + $block_types = [ + 'microsite_hero', + 'senator_microsite_menu', + ]; + foreach (($variables['items'] ?? []) as $key => $item) { /** @var \Drupal\block_content\Entity\BlockContent $block_content */ $block_content = ($item['content']['#block_content'] ?? []); if ($block_content instanceof BlockContent) {