Skip to content

Commit

Permalink
Merge pull request #207 from nysenate/feature/aten-nys-156--resolve-f…
Browse files Browse the repository at this point in the history
…ield-block-caching-issue-to-reenable-microsite-caching

NYS 156: Reenable microsite caching and implement long-term fix for field block caching issue
  • Loading branch information
kzalewski committed Jun 28, 2024
2 parents 4d45870 + 923ef0a commit 5c12a82
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions web/themes/custom/nysenate_theme/nysenate_theme.theme
Original file line number Diff line number Diff line change
Expand Up @@ -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
$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) {
if (in_array($block_content->bundle(), $block_types)) {
unset($variables['items'][$key]);
}
}
}
}

/**
* Implements hook_preprocess_page__node__TYPE().
*/
Expand All @@ -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()
Expand All @@ -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] = [];
}
}
}
Expand Down Expand Up @@ -5027,12 +5046,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') &&
Expand Down

0 comments on commit 5c12a82

Please sign in to comment.