Skip to content

Commit 10efd8c

Browse files
committed
Issue #3549220: Search & Auto list page title & search results message.
1 parent 748d00d commit 10efd8c

File tree

7 files changed

+98
-9
lines changed

7 files changed

+98
-9
lines changed

web/themes/contrib/civictheme/civictheme.theme

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
declare(strict_types=1);
99

1010
use Drupal\civictheme\CivicthemeConstants;
11+
use Drupal\Core\Cache\Cache;
1112
use Drupal\Core\Render\Component\Exception\ComponentNotFoundException;
1213

1314
require_once __DIR__ . '/includes/utilities.inc';
@@ -161,6 +162,7 @@ function civictheme_preprocess_last(array &$variables, string $hook): void {
161162
function civictheme_preprocess_html(array &$variables): void {
162163
_civictheme_preprocess_html__skip_link($variables);
163164
_civictheme_preprocess_html__site_section($variables);
165+
_civictheme_preprocess_html__search_head_title($variables);
164166

165167
// Disable modifier_class as this template is provided by Drupal.
166168
$variables['modifier_class'] = FALSE;
@@ -275,4 +277,5 @@ function civictheme_page_attachments_alter(array &$attachments) {
275277
catch (ComponentNotFoundException $exception) {
276278
\Drupal::logger('civictheme')->error('Unable to find alert component: %message', ['%message' => $exception->getMessage()]);
277279
}
280+
$attachments['#cache']['contexts'] = Cache::mergeContexts($variables['#cache']['contexts'] ?? [], ['url.query_args']);
278281
}

web/themes/contrib/civictheme/config/install/civictheme.settings.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ components:
6767
summary_length: 160
6868
attachment:
6969
use_media_name: true
70+
search:
71+
keyword_fields:
72+
- keywords
73+
- title
7074
colors:
7175
use_color_selector: true
7276
use_brand_colors: true

web/themes/contrib/civictheme/config/install/views.view.civictheme_automated_list.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ display:
531531
admin_label: ''
532532
plugin_id: result
533533
empty: false
534-
content: 'Showing @start - @end of @total'
534+
content: 'Showing @start - @end of @total @keywords'
535535
footer: { }
536536
display_extenders: { }
537537
cache_metadata:

web/themes/contrib/civictheme/config/optional/views.view.civictheme_search.yml

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -218,19 +218,16 @@ display:
218218
query_tags: { }
219219
relationships: { }
220220
header:
221-
area:
222-
id: area
221+
result:
222+
id: result
223223
table: views
224-
field: area
224+
field: result
225225
relationship: none
226226
group_type: group
227227
admin_label: ''
228-
plugin_id: text
228+
plugin_id: result
229229
empty: false
230-
content:
231-
value: '<h3 class="ct-heading ct-list__title">Search results...</h3>'
232-
format: civictheme_rich_text
233-
tokenize: false
230+
content: 'Showing @start - @end of @total @keywords'
234231
footer: { }
235232
display_extenders: { }
236233
cache_metadata:

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
declare(strict_types=1);
99

1010
use Drupal\civictheme\CivicthemeConstants;
11+
use Drupal\civictheme\CivicthemeUtility;
1112

1213
/**
1314
* Implements template_preprocess_block().
@@ -36,3 +37,37 @@ function civictheme_preprocess_block__civictheme_search(array &$variables): void
3637

3738
$variables['theme'] = civictheme_get_theme_config_manager()->load('components.header.theme', CivicthemeConstants::HEADER_THEME_DEFAULT);
3839
}
40+
41+
/**
42+
* Implements hook_preprocess_html().
43+
*/
44+
function _civictheme_preprocess_html__search_head_title(array &$variables): void {
45+
// Load search fields from theme settings.
46+
$setting_keyword_fields = civictheme_get_theme_config_manager()->load('components.search.keyword_fields') ?? '';
47+
if (empty($setting_keyword_fields)) {
48+
return;
49+
}
50+
51+
// Add search keywords to head title.
52+
$keywords = NULL;
53+
$search_fields = CivicthemeUtility::multilineToArray($setting_keyword_fields);
54+
foreach ($search_fields as $field) {
55+
$query_field = \Drupal::request()->query->get($field);
56+
if (!empty($query_field)) {
57+
$keywords = $query_field;
58+
break;
59+
}
60+
}
61+
if (empty($keywords)) {
62+
return;
63+
}
64+
$head_title = (string) $variables['head_title']['title'] ?? '';
65+
if (empty($head_title)) {
66+
return;
67+
}
68+
$sanitized_keywords = htmlspecialchars($keywords, ENT_QUOTES, 'UTF-8');
69+
$variables['head_title']['title'] = t("@title - Searching for '@keywords'", [
70+
'@title' => $head_title,
71+
'@keywords' => $sanitized_keywords,
72+
]);
73+
}

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
declare(strict_types=1);
99

1010
use Drupal\civictheme\CivicthemeConstants;
11+
use Drupal\civictheme\CivicthemeUtility;
1112
use Drupal\Component\Utility\Html;
1213
use Drupal\Core\Cache\Cache;
1314
use Drupal\Core\Form\FormStateInterface;
@@ -21,6 +22,7 @@ function civictheme_preprocess_views_view(array &$variables): void {
2122
_civictheme_preprocess_views_view__view($variables);
2223
_civictheme_preprocess_views_view__pager($variables);
2324
_civictheme_preprocess_views_view__search_page($variables);
25+
_civictheme_preprocess_views_view__results_count($variables);
2426
}
2527

2628
/**
@@ -261,3 +263,36 @@ function _civictheme_preprocess_views_view__search_page(array &$variables): void
261263
$variables['vertical_spacing'] = 'top';
262264
}
263265
}
266+
267+
/**
268+
* Pre-process results count for views.
269+
*/
270+
function _civictheme_preprocess_views_view__results_count(array &$variables): void {
271+
if (empty($variables['results_count'])) {
272+
return;
273+
}
274+
if (!str_contains($variables['results_count'], '@keywords')) {
275+
return;
276+
}
277+
278+
// Load search fields from theme settings.
279+
$setting_keyword_fields = civictheme_get_theme_config_manager()->load('components.search.keyword_fields') ?? '';
280+
if (empty($setting_keyword_fields)) {
281+
return;
282+
}
283+
284+
$variables['#cache']['contexts'] = Cache::mergeContexts($variables['#cache']['contexts'] ?? [], ['url.query_args']);
285+
286+
$keywords = '';
287+
$exposed_input = $variables['view']->getExposedInput();
288+
$search_fields = CivicthemeUtility::multilineToArray($setting_keyword_fields);
289+
290+
foreach ($search_fields as $field) {
291+
if (!empty($exposed_input[$field])) {
292+
$keywords = t("for '@keywords'", ['@keywords' => $exposed_input[$field]])->__toString();
293+
break;
294+
}
295+
}
296+
297+
$variables['results_count'] = str_replace('@keywords', $keywords, $variables['results_count']);
298+
}

web/themes/contrib/civictheme/src/Settings/CivicthemeSettingsFormSectionComponents.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,21 @@ public function form(array &$form, FormStateInterface $form_state): void {
442442
'#default_value' => $this->themeConfigManager->loadForComponent('attachment', 'use_media_name', TRUE),
443443
];
444444

445+
$form['components']['search'] = [
446+
'#type' => 'details',
447+
'#title' => $this->t('Search'),
448+
'#group' => 'components',
449+
'#tree' => TRUE,
450+
];
451+
452+
$form['components']['search']['keyword_fields'] = [
453+
'#type' => 'textarea',
454+
'#title' => $this->t('Search and Automated list keyword fields'),
455+
'#description' => $this->t('A list of machine names for views exposed filters keyword search.<br/>Fields added here will populate the "Showing @start - @end of @total @keywords" where @keywords is replaced with "for [keyword_field_value]" if a value exists.<br/>It will also be used in the browser page title as "@title - Searching for \'@keywords\' | @suffix".<br/>One keyword field per line.'),
456+
'#default_value' => CivicthemeUtility::arrayToMultiline($this->themeConfigManager->load('components.search.keyword_fields', [])),
457+
'#rows' => 4,
458+
];
459+
445460
$form['#process'][] = $this->processForm(...);
446461

447462
// Auto-discover per-component validation and submit handlers.

0 commit comments

Comments
 (0)