Skip to content

Commit

Permalink
nys-156: move field_block render array manipulations from node prepro…
Browse files Browse the repository at this point in the history
…cess to field preprocess, to avoid caching issues
  • Loading branch information
nathanielwoodland committed Jun 26, 2024
1 parent 7bda454 commit 09f4bd8
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 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
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().
*/
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

0 comments on commit 09f4bd8

Please sign in to comment.