From 98e638e8d1cb185210d51a4b8715597eb803bac3 Mon Sep 17 00:00:00 2001 From: Joshua Fernandes <“joshua.1234511@yahoo.in”> Date: Thu, 19 Sep 2024 23:02:38 +0530 Subject: [PATCH 1/3] [3471956] Enhanced Civictheme menu theming system --- .../config/install/civictheme.settings.yml | 3 + .../config/schema/civictheme.schema.yml | 9 +++ .../contrib/civictheme/includes/menu.inc | 79 ++++++++++++------- .../civictheme/src/CivicthemeConstants.php | 15 ++++ ...ivicthemeSettingsFormSectionComponents.php | 27 +++++++ 5 files changed, 103 insertions(+), 30 deletions(-) diff --git a/web/themes/contrib/civictheme/config/install/civictheme.settings.yml b/web/themes/contrib/civictheme/config/install/civictheme.settings.yml index 2af0e27b6..ff5d0734c 100644 --- a/web/themes/contrib/civictheme/config/install/civictheme.settings.yml +++ b/web/themes/contrib/civictheme/config/install/civictheme.settings.yml @@ -38,16 +38,19 @@ components: dropdown_columns: 4 dropdown_columns_fill: false is_animated: true + default_menu: 'civictheme-primary-navigation' secondary_navigation: dropdown: dropdown dropdown_columns: 4 dropdown_columns_fill: false is_animated: true + default_menu: 'civictheme-secondary-navigation' footer: theme: dark logo_type: inline_stacked background_image: path: '' + default_menu: 'civictheme-footer' link: new_window: false external_new_window: true diff --git a/web/themes/contrib/civictheme/config/schema/civictheme.schema.yml b/web/themes/contrib/civictheme/config/schema/civictheme.schema.yml index 66b0debcc..daf813faa 100644 --- a/web/themes/contrib/civictheme/config/schema/civictheme.schema.yml +++ b/web/themes/contrib/civictheme/config/schema/civictheme.schema.yml @@ -125,6 +125,9 @@ civictheme.settings: is_animated: label: 'Animate transition' type: boolean + default_menu: + label: 'Default Menu' + type: string secondary_navigation: type: mapping label: 'Secondary navigation settings' @@ -141,6 +144,9 @@ civictheme.settings: is_animated: label: 'Animate transition' type: boolean + default_menu: + label: 'Default Menu' + type: string footer: type: mapping label: 'Footer settings' @@ -158,6 +164,9 @@ civictheme.settings: path: label: 'Footer background image path' type: string + default_menu: + label: 'Default Menu' + type: string link: type: mapping label: 'Link settings' diff --git a/web/themes/contrib/civictheme/includes/menu.inc b/web/themes/contrib/civictheme/includes/menu.inc index b16fb312f..3606d188b 100644 --- a/web/themes/contrib/civictheme/includes/menu.inc +++ b/web/themes/contrib/civictheme/includes/menu.inc @@ -19,14 +19,57 @@ function _civictheme_preprocess_block__civictheme_footer_menu(array &$variables) } $menu_name = strtr($variables['elements']['#derivative_plugin_id'], '-', '_'); - if ($menu_name === 'civictheme_footer') { - // Set menu theme as per footer theme. - $variables['theme'] = civictheme_get_theme_config_manager()->load('components.footer.theme', CivicthemeConstants::FOOTER_THEME_DEFAULT); - $variables['items'] = $variables['content']['#items'] ?? []; - $variables['title'] = $variables['configuration']['label_display'] ? $variables['configuration']['label'] : ''; + $default_menu = civictheme_get_theme_config_manager()->load('components.footer.default_menu', CivicthemeConstants::FOOTER_DEFAULT_MENU); + $default_menu = strtr($default_menu, '-', '_'); + if ($menu_name === $default_menu) { + _civictheme_preprocess_footer_menu($variables); + } +} - _civictheme_preprocess_menu_items($variables['items']); +/** + * Preprocess for primary navigation menu block. + */ +function _civictheme_preprocess_block__navigation(array &$variables): void { + if (isset($variables['elements']['#base_plugin_id']) && $variables['base_plugin_id'] != 'menu_block') { + return; } + + $menu_name = strtr($variables['elements']['#derivative_plugin_id'], '-', '_'); + $key = substr($menu_name, strlen('civictheme_')); + $variables['dropdown_columns'] = civictheme_get_theme_config_manager()->load(sprintf('components.navigation.%s.dropdown_columns', $key), 4); + $default_menu = civictheme_get_theme_config_manager()->load(sprintf('components.navigation.%s.default_menu', $key), CivicthemeConstants::PRIMARY_NAVIGATION_DEFAULT_MENU); + $default_menu = strtr($default_menu, '-', '_'); + if ($menu_name === $default_menu) { + _civictheme_preprocess_header_navigation_menu($variables, $menu_name); + } +} + +/** + * Helper function to preprocess footer menu. + */ +function _civictheme_preprocess_footer_menu(array &$variables): void { + // Set menu theme as per footer theme. + $variables['theme'] = civictheme_get_theme_config_manager()->load('components.footer.theme', CivicthemeConstants::FOOTER_THEME_DEFAULT); + $variables['items'] = $variables['content']['#items'] ?? []; + $variables['title'] = $variables['configuration']['label_display'] ? $variables['configuration']['label'] : ''; + + _civictheme_preprocess_menu_items($variables['items']); +} + +/** + * Helper function to preprocess header navigation menu. + */ +function _civictheme_preprocess_header_navigation_menu(array &$variables, string $menu_name): void { + $key = substr($menu_name, strlen('civictheme_')); + // Set menu theme as per footer theme. + $variables['theme'] = civictheme_get_theme_config_manager()->load('components.header.theme', CivicthemeConstants::HEADER_THEME_DEFAULT); + $variables['items'] = $variables['content']['#items'] ?? []; + $variables['dropdown'] = civictheme_get_theme_config_manager()->load(sprintf('components.navigation.%s.dropdown', $key), $menu_name === 'civictheme_primary_navigation' ? CivicthemeConstants::NAVIGATION_DROPDOWN_DRAWER : CivicthemeConstants::NAVIGATION_DROPDOWN_DROPDOWN); + $variables['dropdown_columns'] = civictheme_get_theme_config_manager()->load(sprintf('components.navigation.%s.dropdown_columns', $key), 4); + $variables['dropdown_columns_fill'] = civictheme_get_theme_config_manager()->load(sprintf('components.navigation.%s.dropdown_columns_fill', $key), FALSE); + $variables['is_animated'] = civictheme_get_theme_config_manager()->load(sprintf('components.navigation.%s.is_animated', $key), FALSE); + $expand_all_items = $variables['elements']['#configuration']['expand_all_items'] ?? FALSE; + _civictheme_preprocess_menu_items($variables['items'], $expand_all_items); } /** @@ -51,27 +94,3 @@ function _civictheme_preprocess_menu_items(array &$items, bool $expand_all = FAL } } } - -/** - * Preprocess for primary navigation menu block. - */ -function _civictheme_preprocess_block__navigation(array &$variables): void { - if (isset($variables['elements']['#base_plugin_id']) && $variables['base_plugin_id'] != 'menu_block') { - return; - } - - $menu_name = strtr($variables['elements']['#derivative_plugin_id'], '-', '_'); - - if ($menu_name === 'civictheme_primary_navigation' || $menu_name === 'civictheme_secondary_navigation') { - $key = substr($menu_name, strlen('civictheme_')); - // Set menu theme as per footer theme. - $variables['theme'] = civictheme_get_theme_config_manager()->load('components.header.theme', CivicthemeConstants::HEADER_THEME_DEFAULT); - $variables['items'] = $variables['content']['#items'] ?? []; - $variables['dropdown'] = civictheme_get_theme_config_manager()->load(sprintf('components.navigation.%s.dropdown', $key), $menu_name === 'civictheme_primary_navigation' ? CivicthemeConstants::NAVIGATION_DROPDOWN_DRAWER : CivicthemeConstants::NAVIGATION_DROPDOWN_DROPDOWN); - $variables['dropdown_columns'] = civictheme_get_theme_config_manager()->load(sprintf('components.navigation.%s.dropdown_columns', $key), 4); - $variables['dropdown_columns_fill'] = civictheme_get_theme_config_manager()->load(sprintf('components.navigation.%s.dropdown_columns_fill', $key), FALSE); - $variables['is_animated'] = civictheme_get_theme_config_manager()->load(sprintf('components.navigation.%s.is_animated', $key), FALSE); - $expand_all_items = $variables['elements']['#configuration']['expand_all_items'] ?? FALSE; - _civictheme_preprocess_menu_items($variables['items'], $expand_all_items); - } -} diff --git a/web/themes/contrib/civictheme/src/CivicthemeConstants.php b/web/themes/contrib/civictheme/src/CivicthemeConstants.php index 68c1ba28d..9c1dd4626 100644 --- a/web/themes/contrib/civictheme/src/CivicthemeConstants.php +++ b/web/themes/contrib/civictheme/src/CivicthemeConstants.php @@ -186,4 +186,19 @@ final class CivicthemeConstants { */ const OPTOUT_VIEWS_STYLE_TABLE = 'CivicThemeOptoutViewsStyleTable'; + /** + * Defines Tooter Navigation Menu "default_menu" value. + */ + const FOOTER_DEFAULT_MENU = 'civictheme-footer'; + + /** + * Defines Primary Navigation Menu "default_menu" value. + */ + const PRIMARY_NAVIGATION_DEFAULT_MENU = 'civictheme-primary-navigation'; + + /** + * Defines Secondary Navigation Menu "default_menu" value. + */ + const SECONDARY_NAVIGATION_DEFAULT_MENU = 'civictheme-secondary-navigation'; + } diff --git a/web/themes/contrib/civictheme/src/Settings/CivicthemeSettingsFormSectionComponents.php b/web/themes/contrib/civictheme/src/Settings/CivicthemeSettingsFormSectionComponents.php index 92534bf69..add7936ac 100644 --- a/web/themes/contrib/civictheme/src/Settings/CivicthemeSettingsFormSectionComponents.php +++ b/web/themes/contrib/civictheme/src/Settings/CivicthemeSettingsFormSectionComponents.php @@ -6,6 +6,8 @@ use Drupal\civictheme\CivicthemeUtility; use Drupal\Component\Utility\UrlHelper; use Drupal\Core\Form\FormStateInterface; +use Drupal\system\Entity\Menu; +use Drupal\system\MenuInterface; /** * CivicTheme settings section to display components. @@ -175,6 +177,20 @@ public function form(array &$form, FormStateInterface $form_state): void { '#default_value' => $this->themeConfigManager->load('components.footer.logo_type', CivicthemeConstants::LOGO_TYPE_DEFAULT), ]; + $menu_options = array_map(function (MenuInterface $menu) { + return $menu->label(); + }, Menu::loadMultiple()); + asort($menu_options); + + $form['components']['footer']['default_menu'] = [ + '#title' => $this->t('Default Menu'), + '#description' => $this->t('Select the menu to use as the default for this navigation.'), + '#type' => 'select', + '#required' => TRUE, + '#options' => $menu_options, + '#default_value' => $this->themeConfigManager->load('components.footer.default_menu', CivicthemeConstants::FOOTER_DEFAULT_MENU), + ]; + $form['components']['footer']['background_image'] = [ '#type' => 'fieldset', '#title' => $this->t('Footer background image'), @@ -216,6 +232,7 @@ public function form(array &$form, FormStateInterface $form_state): void { 'dropdown_columns' => 4, 'dropdown_columns_fill' => FALSE, 'is_animated' => FALSE, + 'default_menu' => CivicthemeConstants::PRIMARY_NAVIGATION_DEFAULT_MENU, ], 'secondary_navigation' => [ 'title' => $this->t('Secondary navigation'), @@ -223,6 +240,7 @@ public function form(array &$form, FormStateInterface $form_state): void { 'dropdown_columns' => 4, 'dropdown_columns_fill' => FALSE, 'is_animated' => FALSE, + 'default_menu' => CivicthemeConstants::SECONDARY_NAVIGATION_DEFAULT_MENU, ], ]; @@ -234,6 +252,15 @@ public function form(array &$form, FormStateInterface $form_state): void { '#open' => TRUE, ]; + $form['components']['navigation'][$navigation_name]['default_menu'] = [ + '#title' => $this->t('Default Menu'), + '#description' => $this->t('Select the menu to use as the default for this navigation.'), + '#type' => 'select', + '#required' => TRUE, + '#options' => $menu_options, + '#default_value' => $this->themeConfigManager->load(sprintf('components.navigation.%s.default_menu', $navigation_name), $navigation_defaults['default_menu']), + ]; + $form['components']['navigation'][$navigation_name]['dropdown'] = [ '#title' => $this->t('Dropdown type'), '#description' => $this->t('Select how the menu sub-tree items would appear in the menu.'), From f73385fa65e684db4e574df152c6169e37b0b3d2 Mon Sep 17 00:00:00 2001 From: Joshua Fernandes <“joshua.1234511@yahoo.in”> Date: Tue, 24 Sep 2024 15:08:30 +0530 Subject: [PATCH 2/3] Updated failing test due to new fields added. --- tests/behat/features/theme.settings.vertical_tabs.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/behat/features/theme.settings.vertical_tabs.feature b/tests/behat/features/theme.settings.vertical_tabs.feature index 16d73bedf..b215b1fb8 100644 --- a/tests/behat/features/theme.settings.vertical_tabs.feature +++ b/tests/behat/features/theme.settings.vertical_tabs.feature @@ -14,7 +14,7 @@ Feature: Correct vertical tab does not focus on form validation And I scroll to an element with id "edit-components-footer" Then I click on ".vertical-tabs__menu-item a[href='#edit-components-footer']" element And I fill in "Footer background image path" with "dummy-text" - Then I click on ".vertical-tabs__menu-item a[href='#edit-components-navigation']" element + Then I click on ".vertical-tabs__menu-item a[href='#edit-components-skip-link']" element And I press "Save configuration" And I scroll to an element with id "edit-components-footer" From 19791267b6adf8c4985948fbeb03e48116a61054 Mon Sep 17 00:00:00 2001 From: Joshua Fernandes <“joshua.1234511@yahoo.in”> Date: Tue, 24 Sep 2024 15:33:22 +0530 Subject: [PATCH 3/3] Updated Default Configs. --- .../contrib/civictheme/config/install/civictheme.settings.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/web/themes/contrib/civictheme/config/install/civictheme.settings.yml b/web/themes/contrib/civictheme/config/install/civictheme.settings.yml index ff5d0734c..2af0e27b6 100644 --- a/web/themes/contrib/civictheme/config/install/civictheme.settings.yml +++ b/web/themes/contrib/civictheme/config/install/civictheme.settings.yml @@ -38,19 +38,16 @@ components: dropdown_columns: 4 dropdown_columns_fill: false is_animated: true - default_menu: 'civictheme-primary-navigation' secondary_navigation: dropdown: dropdown dropdown_columns: 4 dropdown_columns_fill: false is_animated: true - default_menu: 'civictheme-secondary-navigation' footer: theme: dark logo_type: inline_stacked background_image: path: '' - default_menu: 'civictheme-footer' link: new_window: false external_new_window: true