Skip to content

Commit 23b4c06

Browse files
joshua-salsadigitalJoshua Fernandesrichardgaunt
authored
Issue #3471956 by joshua1234511, richardgaunt, fionamorrison23: Enhanced Civictheme menu theming system (#1350)
Co-authored-by: Joshua Fernandes <“[email protected]”> Co-authored-by: rgaunt <[email protected]>
1 parent ccb2f52 commit 23b4c06

11 files changed

+109
-103
lines changed

web/themes/contrib/civictheme/civictheme.post_update.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,27 @@ function civictheme_post_update_update_editor_allowed_field(): string {
865865
return (string) new TranslatableMarkup('Allowed tags setting not set, aborting update.');
866866
}
867867

868+
/**
869+
* Update civictheme_side_navigation to use civictheme_sidebar_navigation.
870+
*
871+
* @SuppressWarnings(PHPMD.StaticAccess)
872+
*/
873+
function civictheme_post_update_update_sidebar_navigation_suggestion(): string {
874+
$config_factory = \Drupal::configFactory();
875+
$editable = $config_factory->getEditable('block.block.civictheme_side_navigation');
876+
if ($editable->isNew()) {
877+
return 'Sidebar navigation block config does not exist.';
878+
}
879+
$settings = $editable->get('settings') ?? [];
880+
if (empty($settings['suggestion']) || $settings['suggestion'] !== 'civictheme_sidebar_navigation') {
881+
$settings['suggestion'] = 'civictheme_sidebar_navigation';
882+
$editable->set('settings', $settings);
883+
$editable->save();
884+
return 'Updated civictheme_side_navigation block to use civictheme_sidebar_navigation suggestion.';
885+
}
886+
return 'No update needed for civictheme_side_navigation block.';
887+
}
888+
868889
/**
869890
* Add civictheme_message paragraph type and enable it for civictheme page.
870891
*

web/themes/contrib/civictheme/civictheme.theme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ require_once __DIR__ . '/includes/primary_navigation.inc';
4545
require_once __DIR__ . '/includes/promo.inc';
4646
require_once __DIR__ . '/includes/search.inc';
4747
require_once __DIR__ . '/includes/secondary_navigation.inc';
48+
require_once __DIR__ . '/includes/sidebar_navigation.inc';
4849
require_once __DIR__ . '/includes/system_main_block.inc';
4950
require_once __DIR__ . '/includes/site_section.inc';
5051
require_once __DIR__ . '/includes/skip_link.inc';
@@ -214,7 +215,6 @@ function civictheme_preprocess_block(array &$variables): void {
214215
_civictheme_preprocess_block__system_main_block($variables);
215216
_civictheme_preprocess_block__civictheme_banner($variables);
216217
_civictheme_preprocess_block__navigation($variables);
217-
_civictheme_preprocess_block__civictheme_footer_menu($variables);
218218
_civictheme_preprocess_block__content($variables);
219219
_civictheme_preprocess_block__civictheme_mobile_navigation($variables);
220220
_civictheme_preprocess_block__civictheme_social_links($variables);

web/themes/contrib/civictheme/config/optional/block.block.civictheme_side_navigation.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ settings:
2727
depth: 3
2828
expand_all_items: false
2929
parent: 'civictheme-primary-navigation:'
30-
suggestion: civictheme_primary_navigation
30+
suggestion: civictheme_sidebar_navigation
3131
visibility:
3232
request_path:
3333
id: request_path

web/themes/contrib/civictheme/includes/menu.inc

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,6 @@ declare(strict_types=1);
1010
use Drupal\civictheme\CivicthemeConstants;
1111
use Drupal\Core\Url;
1212

13-
/**
14-
* Preprocess for footer menu block.
15-
*/
16-
function _civictheme_preprocess_block__civictheme_footer_menu(array &$variables): void {
17-
if (isset($variables['elements']['#base_plugin_id']) && $variables['base_plugin_id'] != 'menu_block') {
18-
return;
19-
}
20-
21-
$menu_name = strtr($variables['elements']['#derivative_plugin_id'], '-', '_');
22-
if ($menu_name === 'civictheme_footer') {
23-
// Set menu theme as per footer theme.
24-
$variables['theme'] = civictheme_get_theme_config_manager()->load('components.footer.theme', CivicthemeConstants::FOOTER_THEME_DEFAULT);
25-
$variables['items'] = $variables['content']['#items'] ?? [];
26-
$variables['title'] = $variables['configuration']['label_display'] ? $variables['configuration']['label'] : '';
27-
28-
_civictheme_preprocess_menu_items($variables['items']);
29-
}
30-
}
31-
3213
/**
3314
* Add required element for external link.
3415
*
@@ -54,6 +35,8 @@ function _civictheme_preprocess_menu_items(array &$items, bool $expand_all = FAL
5435

5536
/**
5637
* Preprocess for primary navigation menu block.
38+
*
39+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
5740
*/
5841
function _civictheme_preprocess_block__navigation(array &$variables): void {
5942
if (isset($variables['elements']['#base_plugin_id']) && $variables['base_plugin_id'] != 'menu_block') {
@@ -62,6 +45,17 @@ function _civictheme_preprocess_block__navigation(array &$variables): void {
6245

6346
$menu_name = strtr($variables['elements']['#derivative_plugin_id'], '-', '_');
6447

48+
$menu_theme_list = [
49+
'civictheme_primary_navigation',
50+
'civictheme_secondary_navigation',
51+
'civictheme_sidebar_navigation',
52+
'civictheme_footer',
53+
];
54+
55+
if (!empty($variables['elements']['#configuration']['suggestion']) && in_array($variables['elements']['#configuration']['suggestion'], $menu_theme_list)) {
56+
$menu_name = $variables['elements']['#configuration']['suggestion'];
57+
}
58+
6559
if ($menu_name === 'civictheme_primary_navigation' || $menu_name === 'civictheme_secondary_navigation') {
6660
$key = substr($menu_name, strlen('civictheme_'));
6761
// Set menu theme as per footer theme.
@@ -77,4 +71,13 @@ function _civictheme_preprocess_block__navigation(array &$variables): void {
7771
$expand_all_items = $variables['elements']['#configuration']['expand_all_items'] ?? FALSE;
7872
_civictheme_preprocess_menu_items($variables['items'], $expand_all_items);
7973
}
74+
75+
if ($menu_name === 'civictheme_footer') {
76+
// Set menu theme as per footer theme.
77+
$variables['theme'] = civictheme_get_theme_config_manager()->load('components.footer.theme', CivicthemeConstants::FOOTER_THEME_DEFAULT);
78+
$variables['items'] = $variables['content']['#items'] ?? [];
79+
$variables['title'] = $variables['configuration']['label_display'] ? $variables['configuration']['label'] : '';
80+
81+
_civictheme_preprocess_menu_items($variables['items']);
82+
}
8083
}

web/themes/contrib/civictheme/includes/primary_navigation.inc

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,17 @@
77

88
declare(strict_types=1);
99

10-
use Drupal\block\Entity\Block;
1110
use Drupal\civictheme\CivicthemeConstants;
1211

1312
/**
14-
* Implements template_preprocess_block__HOOK().
13+
* Implements template_preprocess_block__HOOK() for header primary navigation.
1514
*
1615
* @SuppressWarnings(PHPMD.StaticAccess)
1716
*/
1817
function civictheme_preprocess_block__menu_block__civictheme_primary_navigation(array &$variables): void {
1918
$variables['theme'] = civictheme_get_theme_config_manager()->load('components.header.theme', CivicthemeConstants::HEADER_THEME_DEFAULT);
2019
$variables['items'] = $variables['content']['#items'] ?? [];
2120
$variables['title'] = $variables['configuration']['label_display'] ? $variables['configuration']['label'] : '';
22-
23-
if (!empty($variables['elements']['#id'])) {
24-
$block = Block::load($variables['elements']['#id']);
25-
if ($block) {
26-
$region = $block->getRegion();
27-
28-
if (str_starts_with($region, 'header')) {
29-
civictheme_add_modifier_class($variables, 'ct-flex-justify-content-end');
30-
}
31-
32-
if (str_starts_with($region, 'sidebar')) {
33-
$variables['in_sidebar'] = TRUE;
34-
$expand_all_items = $variables['configuration']['expand_all_items'] ?? FALSE;
35-
}
36-
}
37-
}
38-
39-
_civictheme_preprocess_menu_items($variables['items'], $expand_all_items ?? FALSE);
21+
civictheme_add_modifier_class($variables, 'ct-flex-justify-content-end');
22+
_civictheme_preprocess_menu_items($variables['items'], FALSE);
4023
}

web/themes/contrib/civictheme/includes/secondary_navigation.inc

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,17 @@
77

88
declare(strict_types=1);
99

10-
use Drupal\block\Entity\Block;
1110
use Drupal\civictheme\CivicthemeConstants;
1211

1312
/**
14-
* Implements template_preprocess_block__HOOK().
13+
* Implements template_preprocess_block__HOOK() for header secondary navigation.
1514
*
1615
* @SuppressWarnings(PHPMD.StaticAccess)
1716
*/
1817
function civictheme_preprocess_block__menu_block__civictheme_secondary_navigation(array &$variables): void {
1918
$variables['theme'] = civictheme_get_theme_config_manager()->load('components.header.theme', CivicthemeConstants::HEADER_THEME_DEFAULT);
2019
$variables['items'] = $variables['content']['#items'] ?? [];
2120
$variables['title'] = $variables['configuration']['label_display'] ? $variables['configuration']['label'] : '';
22-
23-
if (!empty($variables['elements']['#id'])) {
24-
$block = Block::load($variables['elements']['#id']);
25-
if ($block) {
26-
$region = $block->getRegion();
27-
28-
if (str_starts_with($region, 'header')) {
29-
civictheme_add_modifier_class($variables, 'ct-flex-justify-content-end');
30-
}
31-
32-
if (str_starts_with($region, 'sidebar')) {
33-
$variables['in_sidebar'] = TRUE;
34-
$expand_all_items = $variables['configuration']['expand_all_items'] ?? FALSE;
35-
}
36-
}
37-
}
38-
39-
_civictheme_preprocess_menu_items($variables['items'], $expand_all_items ?? FALSE);
21+
civictheme_add_modifier_class($variables, 'ct-flex-justify-content-end');
22+
_civictheme_preprocess_menu_items($variables['items'], FALSE);
4023
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Sidebar navigation block theme alterations.
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
use Drupal\civictheme\CivicthemeConstants;
11+
12+
/**
13+
* Implements template_preprocess_block__HOOK() for sidebar navigation.
14+
*
15+
* @SuppressWarnings(PHPMD.StaticAccess)
16+
*/
17+
function civictheme_preprocess_block__menu_block__civictheme_sidebar_navigation(array &$variables): void {
18+
$variables['theme'] = civictheme_get_theme_config_manager()->load('components.header.theme', CivicthemeConstants::HEADER_THEME_DEFAULT);
19+
$variables['items'] = $variables['content']['#items'] ?? [];
20+
$variables['title'] = $variables['configuration']['label_display'] ? $variables['configuration']['label'] : '';
21+
$variables['in_sidebar'] = TRUE;
22+
$expand_all_items = $variables['configuration']['expand_all_items'] ?? FALSE;
23+
_civictheme_preprocess_menu_items($variables['items'], $expand_all_items);
24+
}

web/themes/contrib/civictheme/templates/block/block--menu-block--civictheme-primary-navigation.html.twig

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,16 @@
22
/**
33
* @file
44
* CivicTheme implementation to display a Primary Navigation menu block.
5-
*
6-
* Note that when primary navigation menu is shown as a side navigation
7-
* component, the theme is hardcoded to the theme of the page (light) rather
8-
* than the theme of the primary navigation.
95
*/
106
#}
11-
{% if in_sidebar %}
12-
{% include 'civictheme:side-navigation' with {
13-
theme: 'light',
14-
items,
15-
modifier_class: 'ct-primary-navigation ' ~ modifier_class|default(''),
16-
} only %}
17-
{% else %}
18-
{% include 'civictheme:navigation' with {
19-
theme,
20-
items,
21-
type: dropdown == 'none' ? 'inline' : dropdown,
22-
dropdown_columns,
23-
dropdown_columns_fill,
24-
is_animated,
25-
menu_id,
26-
variant,
27-
modifier_class: 'ct-primary-navigation ' ~ modifier_class|default(''),
28-
} only %}
29-
{% endif %}
7+
{% include 'civictheme:navigation' with {
8+
theme,
9+
items,
10+
type: dropdown == 'none' ? 'inline' : dropdown,
11+
dropdown_columns,
12+
dropdown_columns_fill,
13+
is_animated,
14+
menu_id,
15+
variant,
16+
modifier_class: 'ct-primary-navigation ' ~ modifier_class|default(''),
17+
} only %}

web/themes/contrib/civictheme/templates/block/block--menu-block--civictheme-secondary-navigation.html.twig

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,14 @@
44
* CivicTheme implementation to display a Secondary Navigation menu block.
55
*/
66
#}
7-
{% if in_sidebar %}
8-
{% include 'civictheme:side-navigation' with {
9-
theme,
10-
items,
11-
modifier_class: 'ct-secondary-navigation ' ~ modifier_class|default(''),
12-
} only %}
13-
{% else %}
14-
{% include 'civictheme:navigation' with {
15-
theme,
16-
items,
17-
type: dropdown == 'none' ? 'inline' : dropdown,
18-
dropdown_columns,
19-
dropdown_columns_fill,
20-
is_animated,
21-
menu_id,
22-
variant,
23-
modifier_class: 'ct-secondary-navigation ' ~ modifier_class|default(''),
24-
} only %}
25-
{% endif %}
7+
{% include 'civictheme:navigation' with {
8+
theme,
9+
items,
10+
type: dropdown == 'none' ? 'inline' : dropdown,
11+
dropdown_columns,
12+
dropdown_columns_fill,
13+
is_animated,
14+
menu_id,
15+
variant,
16+
modifier_class: 'ct-secondary-navigation ' ~ modifier_class|default(''),
17+
} only %}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{#
2+
/**
3+
* @file
4+
* CivicTheme implementation to display a Sidebar Navigation menu block.
5+
*/
6+
#}
7+
{% include 'civictheme:side-navigation' with {
8+
theme,
9+
items,
10+
modifier_class: 'ct-sidebar-navigation ' ~ modifier_class|default(''),
11+
} only %}

0 commit comments

Comments
 (0)