diff --git a/config/sync/core.extension.yml b/config/sync/core.extension.yml index d8b9eb806a..f8a4043087 100644 --- a/config/sync/core.extension.yml +++ b/config/sync/core.extension.yml @@ -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 diff --git a/docs/SiteBanner.md b/docs/SiteBanner.md index 4c28e31358..a9ff79de45 100644 --- a/docs/SiteBanner.md +++ b/docs/SiteBanner.md @@ -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. -- `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 diff --git a/web/modules/custom/usa_site_banner/usa_site_banner.info.yml b/web/modules/custom/usa_site_banner/usa_site_banner.info.yml new file mode 100644 index 0000000000..89a0278918 --- /dev/null +++ b/web/modules/custom/usa_site_banner/usa_site_banner.info.yml @@ -0,0 +1,4 @@ +name: 'USAGov Site Banner Module' +type: module +description: 'Place the site banner in the site' +core_version_requirement: ^10 diff --git a/web/modules/custom/usa_site_banner/usa_site_banner.module b/web/modules/custom/usa_site_banner/usa_site_banner.module new file mode 100644 index 0000000000..9374a09c97 --- /dev/null +++ b/web/modules/custom/usa_site_banner/usa_site_banner.module @@ -0,0 +1,92 @@ +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); +} diff --git a/web/modules/custom/usa_twig_vars/usa_twig_vars.module b/web/modules/custom/usa_twig_vars/usa_twig_vars.module index 4b22dd24c9..cf078a5d6d 100644 --- a/web/modules/custom/usa_twig_vars/usa_twig_vars.module +++ b/web/modules/custom/usa_twig_vars/usa_twig_vars.module @@ -121,6 +121,7 @@ function usa_twig_vars_preprocess(&$variables, $hook) { basicPagesubType: $pageType ?? NULL, pageType: $datalayer_Page_Type, ); + unset($isFront); } } @@ -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(). */ diff --git a/web/themes/custom/usagov/templates/region--header-top.html.twig b/web/themes/custom/usagov/templates/region--header-top.html.twig deleted file mode 100644 index 5552e01e3a..0000000000 --- a/web/themes/custom/usagov/templates/region--header-top.html.twig +++ /dev/null @@ -1,37 +0,0 @@ -{# -/** - * @file - * Theme override to display a region. - * - * Available variables: - * - content: The content for this region, typically blocks. - * - attributes: HTML attributes for the region
. - * - region: The name of the region variable as defined in the theme's - * .info.yml file. - * - * @see template_preprocess_region() - * - * @ingroup themeable - */ -#} -{% if content %} - - - - {# Add all blocks in the `header_top` region #} - {{ content }} - - {# Check if there are any site_banners #} - {% if site_banners %} - {% for site_banner_id in site_banners %} - {# If the current `site_banner` has the checkbox checked, it adds it to the `header_top` region. #} - {% if site_banner_id %} - {{ drupal_entity('block_content', site_banner_id) }} - {% endif %} - {% endfor %} - {% endif %} - -
- -{% endif %} -