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

[CIVIC-2089] Mobile search #1336

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
2 changes: 2 additions & 0 deletions web/themes/contrib/civictheme/civictheme.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ config_devel:
- field.field.block_content.civictheme_mobile_navigation.field_c_b_trigger_text
- field.field.block_content.civictheme_mobile_navigation.field_c_b_trigger_theme
- field.field.block_content.civictheme_search.field_c_b_link
- field.field.block_content.civictheme_search.field_c_b_link_in_mobile_menu
- field.field.block_content.civictheme_social_links.field_c_b_social_icons
- field.field.block_content.civictheme_social_links.field_c_b_theme
- field.field.block_content.civictheme_social_links.field_c_b_with_border
Expand Down Expand Up @@ -472,6 +473,7 @@ config_devel:
- field.storage.block_content.field_c_b_components
- field.storage.block_content.field_c_b_featured_image
- field.storage.block_content.field_c_b_link
- field.storage.block_content.field_c_b_link_in_mobile_menu
- field.storage.block_content.field_c_b_social_icons
- field.storage.block_content.field_c_b_theme
- field.storage.block_content.field_c_b_top
Expand Down
2 changes: 1 addition & 1 deletion web/themes/contrib/civictheme/civictheme.theme
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ require_once __DIR__ . '/includes/local_tasks.inc';
require_once __DIR__ . '/includes/libraries.inc';
require_once __DIR__ . '/includes/map.inc';
require_once __DIR__ . '/includes/media.inc';
require_once __DIR__ . '/includes/search.inc';
require_once __DIR__ . '/includes/menu.inc';
require_once __DIR__ . '/includes/mobile_navigation.inc';
require_once __DIR__ . '/includes/next_step.inc';
Expand All @@ -41,7 +42,6 @@ require_once __DIR__ . '/includes/pagination.inc';
require_once __DIR__ . '/includes/paragraphs.inc';
require_once __DIR__ . '/includes/primary_navigation.inc';
require_once __DIR__ . '/includes/promo.inc';
require_once __DIR__ . '/includes/search.inc';
require_once __DIR__ . '/includes/secondary_navigation.inc';
require_once __DIR__ . '/includes/system_main_block.inc';
require_once __DIR__ . '/includes/site_section.inc';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ dependencies:
config:
- block_content.type.civictheme_search
- field.field.block_content.civictheme_search.field_c_b_link
- field.field.block_content.civictheme_search.field_c_b_link_in_mobile_menu
- workflows.workflow.civictheme_editorial
module:
- content_moderation
Expand All @@ -15,23 +16,30 @@ mode: default
content:
field_c_b_link:
type: link_default
weight: 26
weight: 1
region: content
settings:
placeholder_url: ''
placeholder_title: ''
third_party_settings: { }
field_c_b_link_in_mobile_menu:
type: boolean_checkbox
weight: 2
region: content
settings:
display_label: true
third_party_settings: { }
info:
type: string_textfield
weight: -5
weight: 0
region: content
settings:
size: 60
placeholder: ''
third_party_settings: { }
moderation_state:
type: moderation_state_default
weight: 100
weight: 3
region: content
settings: { }
third_party_settings: { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ dependencies:
config:
- block_content.type.civictheme_search
- field.field.block_content.civictheme_search.field_c_b_link
- field.field.block_content.civictheme_search.field_c_b_link_in_mobile_menu
module:
- link
id: block_content.civictheme_search.default
Expand All @@ -28,4 +29,14 @@ content:
third_party_settings: { }
weight: 0
region: content
field_c_b_link_in_mobile_menu:
type: boolean
label: above
settings:
format: default
format_custom_false: ''
format_custom_true: ''
third_party_settings: { }
weight: 1
region: content
hidden: { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
langcode: en
status: true
dependencies:
config:
- block_content.type.civictheme_search
- field.storage.block_content.field_c_b_link_in_mobile_menu
id: block_content.civictheme_search.field_c_b_link_in_mobile_menu
field_name: field_c_b_link_in_mobile_menu
entity_type: block_content
bundle: civictheme_search
label: 'Show link in mobile menu'
description: ''
required: false
translatable: false
default_value: { }
default_value_callback: ''
settings:
on_label: 'On'
off_label: 'Off'
field_type: boolean
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
langcode: en
status: true
dependencies:
module:
- block_content
id: block_content.field_c_b_link_in_mobile_menu
field_name: field_c_b_link_in_mobile_menu
entity_type: block_content
type: boolean
settings: { }
module: core
locked: false
cardinality: 1
translatable: true
indexes: { }
persist_with_no_fields: false
custom_storage: false
16 changes: 16 additions & 0 deletions web/themes/contrib/civictheme/includes/mobile_navigation.inc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ function _civictheme_preprocess_block__civictheme_mobile_navigation(array &$vari
'bottom_menu' => 'field_c_b_bottom',
];

// Retrieve the search link.
$search_link = _civictheme_search_link();

// Get primary and secondary menu links by building menu tree based on
// menu block settings.
$entity_cache_tags = $entity->getCacheTags();
Expand Down Expand Up @@ -80,6 +83,19 @@ function _civictheme_preprocess_block__civictheme_mobile_navigation(array &$vari
$build = $menu_tree_service->build($tree);
$variables[$menu_key] = $build['#items'] ?? [];

// Add search link to end of top menu.
if ($menu_key === 'top_menu' && $search_link) {
$variables[$menu_key][] = [
'url' => $search_link['url'],
'title' => $search_link['text'],
'is_new_window' => FALSE,
'is_external' => FALSE,
'is_expanded' => FALSE,
'icon' => 'magnifier',
'below' => [],
];
}

_civictheme_preprocess_menu_items($variables[$menu_key], $expand_all_items);

$cacheability = $cacheability->merge(CacheableMetadata::createFromRenderArray($build));
Expand Down
9 changes: 9 additions & 0 deletions web/themes/contrib/civictheme/includes/search.inc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ function civictheme_preprocess_block__civictheme_search(array &$variables): void
if ($link !== NULL) {
$variables['url'] = $link->getUrl()->toString();
$variables['text'] = $link->get('title')->getString();
if ($entity->hasField('field_c_b_link_in_mobile_menu') && civictheme_get_field_value($entity, 'field_c_b_link_in_mobile_menu')) {
// Hide search block on mobile view.
$variables['modifier_class'] = 'hide-xxs show-m-flex';
$search_link = [
'url' => $link->getUrl()->toString(),
'text' => $link->get('title')->getString(),
];
_civictheme_search_link($search_link);
}
}

$variables['theme'] = civictheme_get_theme_config_manager()->load('components.header.theme', CivicthemeConstants::HEADER_THEME_DEFAULT);
Expand Down
19 changes: 19 additions & 0 deletions web/themes/contrib/civictheme/includes/utilities.inc
Original file line number Diff line number Diff line change
Expand Up @@ -911,3 +911,22 @@ function _civictheme_feature_optout_flags(): array {
$flags[CivicthemeConstants::OPTOUT_SUMMARY_HIDE_ELLIPSIS] = t('Hide card summary ellipsis');
return $flags;
}

/**
* Helper function to store and retrieve the search link.
*
* @param array|null $link
* The link array to store, or NULL to retrieve the stored link.
*
* @return array|null
* The stored link array, or NULL if no link is stored.
*/
function _civictheme_search_link(array $link = NULL): ?array {
Copy link
Collaborator

@alan-cole alan-cole Feb 14, 2025

Choose a reason for hiding this comment

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

I don't know about this approach. I'd leave it to @richardgaunt to provide feedback.

This seems like it could break if search block isn't rendered before the mobile navigation block, or if there are multiple search blocks.

I think we had discussed about multiple search blocks showing multiple links in mobile menu.

My assumption would have been something like:

use Drupal\block\Entity\Block;

// Load all blocks of type 'search'
$blocks = Block::loadMultiple(['type' => 'search']);

// Initialize an empty array to store the block data
$block_data = [];

// Iterate through the blocks and do something with them
foreach ($blocks as $block) {
  // Get the block's configuration
  $config = $block->getConfiguration();

  // Check if the "field_c_b_link_in_mobile_menu" field checkbox is checked
  $link_in_mobile_menu = $config['field_c_b_link_in_mobile_menu'] ?? false;

  // Get the block's URL and title
  $block_url = $config['url'];
  $block_title = $config['title'];

  if ($link_in_mobile_menu) {
    // Add the block's URL and title to the $block_data array
    $block_data[] = [
      'url' => $block_url,
      'title' => $block_title,
    ];
  }
}

// Now you can use the $block_data array to do something with the block data
print_r($block_data);

in the mobile menu navigation, to identify any search blocks with that option checked.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes this doesn't look right to me

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

thanks @alan-cole for the suggestion.
I tried with $blocks = Block::loadMultiple(['type' => 'search']); but it didn't work for me so I use

  // Load all blocks of type 'civictheme_search'
  $block_storage = \Drupal::entityTypeManager()->getStorage('block_content');
  $blocks = $block_storage->loadByProperties(['type' => 'civictheme_search']);

to get the block search content, it seems work well.
Please review my change.
Thank you!

static $stored_link = NULL;

if ($link !== NULL) {
$stored_link = $link;
}

return $stored_link;
}