Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usagov 1685 fix site banner #1769

Draft
wants to merge 25 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
36ac5be
USAGOV-1685-Fix-Site-banner: Testing cache
YaritzaGarcia Jul 26, 2024
1491687
USAGOV-1685-Fix-Site-banner: Merge branch 'dev' into USAGOV-1685-Fix-…
YaritzaGarcia Jul 26, 2024
8c1ae10
USAGOV-1685-Fix-Site-banner: Issue fixed. Added new if statement that…
YaritzaGarcia Jul 26, 2024
07c9f75
Merge branch 'dev' into USAGOV-1685-Fix-Site-banner
YaritzaGarcia Jul 30, 2024
87b4367
USAGOV-1685-Fix-Site-banner: Fix lint errors and remove comment
YaritzaGarcia Jul 30, 2024
f4c3338
Merge branch 'dev' into USAGOV-1685-Fix-Site-banner
akf Jul 31, 2024
37416cf
Merge branch 'dev' into USAGOV-1685-Fix-Site-banner
YaritzaGarcia Jul 31, 2024
0437155
USAGOV-1685-Fix-Site-banner: Moving the logic to another hook, still …
YaritzaGarcia Aug 1, 2024
2a3dc1c
USAGOV-1685-Fix-Site-banner: old testing stuff
YaritzaGarcia Aug 2, 2024
42eb5a5
USAGOV-1685-Fix-Site-banner: Merge branch 'dev' into USAGOV-1685-Fix-…
YaritzaGarcia Aug 5, 2024
e3ff3f9
USAGOV-1685-Fix-Site-banner: Merge branch 'dev' into USAGOV-1685-Fix-…
YaritzaGarcia Aug 13, 2024
23f906a
USAGOV-1685-Fix-Site-banner: Merge branch 'dev' into USAGOV-1685-Fix-…
YaritzaGarcia Aug 16, 2024
ba57bd8
USAGOV-1685-Fix-Site-banner: testing
YaritzaGarcia Aug 19, 2024
9ccff1f
Merge branch 'dev' into USAGOV-1685-Fix-Site-banner
YaritzaGarcia Aug 20, 2024
69e63f1
USAGOV-1685-Fix-Site-banner: Working. Placing the block by creating i…
YaritzaGarcia Aug 23, 2024
dd38546
USAGOV-1685-Fix-Site-banner: Move everything to a separate module and…
YaritzaGarcia Aug 23, 2024
b927650
USAGOV-1685-Fix-Site-banner: Fix lint errors
YaritzaGarcia Aug 23, 2024
7bbd9c0
USAGOV-1685-Fix-Site-banner: FIX LINT ERRORS
YaritzaGarcia Aug 23, 2024
701abce
USAGOV-1685-Fix-Site-banner: Merge branch 'dev' into USAGOV-1685-Fix-…
YaritzaGarcia Aug 23, 2024
c1a5eed
USAGOV-1685-Fix-Site-banner: Install new site banner module
YaritzaGarcia Aug 23, 2024
f839cdf
USAGOV-1685-Fix-Site-banner: Fix condition in functions
YaritzaGarcia Aug 23, 2024
c713225
USAGOV-1685-Fix-Site-banner: Update readme
YaritzaGarcia Aug 29, 2024
91d875e
Merge branch 'dev' into USAGOV-1685-Fix-Site-banner
YaritzaGarcia Aug 29, 2024
2d3986c
USAGOV-1685-Fix-Site-banner: Fix error in readme
YaritzaGarcia Aug 29, 2024
066d1b0
Merge branch 'dev' into USAGOV-1685-Fix-Site-banner
akf Aug 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/sync/core.extension.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ module:
usa_contact_center_api: 0
usa_content_moderation_notifications: 0
usa_orphaned_entities: 0
usa_site_banner: 0
usa_translation: 0
usagov_benefit_category_search: 0
usagov_benefit_finder: 0
Expand Down
9 changes: 5 additions & 4 deletions docs/SiteBanner.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ The feature introduces a new block type called **Site Banner**. This block uses
To control the placement of Site Banners without giving full block placement permissions, a checkbox has been added to the Site Banner edit form. This checkbox allows content admins to decide if the banner should be placed in the `header_top` region or removed from it.

### Files Involved
- `web/modules/custom/usa_twig_vars/usa_twig_vars.module`
- The `usa_twig_vars_preprocess_region` hook checks the value of the placement checkbox for each site banner in the site and builds an array called `site_banners`. The value of each element in this array can be the id of the site banner if the checkbox `place_above_header` is checked or `NULL` if the checkbox is not checked.
- `web/modules/custom/usa_site_banner/usa_site_banner.module`
- The `usa_site_banner_entity_insert` is a hook that runs when a Content Admin creates a site_banner. This hook creates a block by calling the `usa_site_banner_create_block` function and then places it in the `header_top` region if necessary by calling the `usa_site_banner_place_block` function.
- The `usa_site_banner_entity_update` is a hook that runs when a Content Admin updates a site_banner. This hook creates a block if necessary by calling the `usa_site_banner_create_block` function and then places it in the `header_top` region if necessary by calling the `usa_site_banner_place_block` function.
- The `usa_site_banner_place_block` is a fuction that places a Block in a region by setting the `status` value of the Block. Since in order to change the value you need to have a Block created, this function calls the `usa_site_banner_create_block` function in case there is no Block for the site_banner.
- The `usa_site_banner_create_block` is a function that creates a Block for a site_banner based on the BlockContent data.
- `web/themes/custom/usagov/templates/field--block-content--field-place-above-header.html.twig`
- For some reason the checkbox added text below the site banner so I added this empty file to prevent this text from appearing.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I can explain this one! The checkbox is part of the block content, so the block will render it, and it will fall back on the default theme if nothing is supplied. This is a fine way to suppress it; it still shows up in the admin area (where we want to see it!) because the admin area has a different theme.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious what all of this means for the Benefits Redirect Modal. Should I move forward with the implementation that mirrors how the gov banner is currently done on the prod? Or should I change it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just closing the loop for @YaritzaGarcia, Chris and I DM'd about this. He'll move forward with the existing implementation and we'll expect to update both when we have a better approach (probably based on this newer work).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update and for reviewing! I will take a look.

- `web/themes/custom/usagov/templates/region--header-top.html.twig`
- The `site_banners` array created in the hook is used in this file to add or remove the banner from the `header_top` region. To do this we use Twig Tweak, specifically the `drupal_entity` function.

## Adding a Site Banner: Step-by-Step Guide

Expand Down
4 changes: 4 additions & 0 deletions web/modules/custom/usa_site_banner/usa_site_banner.info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name: 'USAGov Site Banner Module'
type: module
description: 'Place the site banner in the site'
core_version_requirement: ^10
92 changes: 92 additions & 0 deletions web/modules/custom/usa_site_banner/usa_site_banner.module
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

use Drupal\block\Entity\Block;
use Drupal\block_content\Entity\BlockContent;

/**
* Implements hook_entity_insert()
* When a Content Admin creates a site_banner, this function creates a block
* and places it in the header_top region if necessary.
*/
function usa_site_banner_entity_insert($entity) {
if ($entity->getEntityTypeId() == 'block_content' && $entity->bundle() == "site_banner") {
usa_site_banner_place_block($entity);
}
}

/**
* Implements hook_entity_update()
* When a Content Admin updates a site_banner, this function creates a block, if necessary,
* and places it in the header_top region, if necessary.
*/
function usa_site_banner_entity_update($entity) {
if ($entity->getEntityTypeId() == 'block_content' && $entity->bundle() == "site_banner") {
usa_site_banner_place_block($entity);
}
}

/**
* Places a Block in a region using the BlockContent data to create the Block.
* @param BlockContent $block_content The BlockContent you want to place in the region.
*/
function usa_site_banner_place_block(BlockContent $block_content) {
// Get the block if any
$block = Block::load('site_banner_' . $block_content->id());
// Get the value of the checkbox in the site_banner form
$place_above_header = $block_content->field_place_above_header->value;

// If the block has not been created, it is created so that the block can be placed in the region.
if (!$block) {
$block = usa_site_banner_create_block($block_content);
}

// Place the block in the "header_top" region if the checkbox is checked.
if ($place_above_header) {
$block->set('status', 1);
$block->save();
}
// Removes the block from the "header_top" region if the checkbox is unchecked.
else {
$block->set('status', 0);
$block->save();
}
}

/**
* Creates a Block based on the BlockContent data.
* @param BlockContent $block_content The BlockContent for which you want to create the block.
* @return Block The new generated Block.
*/
function usa_site_banner_create_block(BlockContent $block_content) {

$block_content_langcode = $block_content->langcode->value;

// Set visibility to appear based on language.
$visibility = [
"language" => [
"id" => "language",
"negate" => FALSE,
"context_mapping" => [
"language" => "@language.current_language_context:language_interface"
],
"langcodes" => [
$block_content_langcode => $block_content_langcode
]
]
];

// Set all the values ​​needed to create a block, including which region it will be placed in.
$values = [
'id' => 'site_banner_' . $block_content->id(),
'theme' => 'usagov',
'plugin' => 'block_content:' . $block_content->uuid(),
'region' => 'header_top',
'visibility' => $visibility,
'status' => 0,
'weight' => 0,
'settings' => [],
];

// Create and return the new block.
return Block::create($values);
}
42 changes: 1 addition & 41 deletions web/modules/custom/usa_twig_vars/usa_twig_vars.module
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ function usa_twig_vars_preprocess(&$variables, $hook) {
basicPagesubType: $pageType ?? NULL,
pageType: $datalayer_Page_Type,
);

unset($isFront);
}
}
Expand Down Expand Up @@ -287,47 +288,6 @@ function usa_twig_vars_preprocess_views_view_list__federal_agencies(&$variables,
$variables['alpha_context'] = $alpha_context;
}

/**
* Create an array called `site_banners` that contains all the site banners. The
* value of each element in this array can be the id of the site banner if the
* checkbox `place_above_header` is checked or `null` if the checkbox is not checked.
*/
function usa_twig_vars_preprocess_region(&$variables) {

// Check if we are in the `header_top` region
if ($variables['region'] === 'header_top') {

$site_language = \Drupal::languageManager()->getCurrentLanguage()->getId();
// Get all site_banners from the website
$site_banners_blocks = \Drupal::entityTypeManager()->getStorage('block_content')->loadByProperties(['type' => 'site_banner']);
if ($site_banners_blocks) {

foreach ($site_banners_blocks as $site_banner) {
/**
* Check if the language of the banner is the same as the website.
* This prevents incorrect language banners from being displayed on the site.
* */
$site_banner_lang = $site_banner->langcode->value;
if ($site_banner_lang === $site_language) {
// Get the value of the checkbox in the site_banner form
$place_above_header = $site_banner->field_place_above_header->value;

// Check if the checkbox in the site_banner form is checked or not.
if ($place_above_header) {
// Set the value to the site_banner id to add the site_banner to the `header_top` region.
$variables['site_banners'][$site_banner->id()] = $site_banner->id();
}
else {
// Set the value to null to remove the site_banner with id `$site_banner->id()` from the region `header_top`
$variables['site_banners'][$site_banner->id()] = NULL;
}
}

}
}
}
}

/**
* Implements hook_form_alter().
*/
Expand Down
37 changes: 0 additions & 37 deletions web/themes/custom/usagov/templates/region--header-top.html.twig

This file was deleted.