Skip to content

Commit 3957fa8

Browse files
committed
Code suggestion fixes.
1 parent c8439c8 commit 3957fa8

File tree

5 files changed

+72
-17
lines changed

5 files changed

+72
-17
lines changed

web/themes/contrib/civictheme/civictheme.theme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,5 +277,5 @@ function civictheme_page_attachments_alter(array &$attachments) {
277277
catch (ComponentNotFoundException $exception) {
278278
\Drupal::logger('civictheme')->error('Unable to find alert component: %message', ['%message' => $exception->getMessage()]);
279279
}
280-
$attachments['#cache']['contexts'] = Cache::mergeContexts($variables['#cache']['contexts'] ?? [], ['url.query_args']);
280+
$attachments['#cache']['contexts'] = Cache::mergeContexts($attachments['#cache']['contexts'] ?? [], ['url.query_args']);
281281
}

web/themes/contrib/civictheme/config/schema/civictheme.schema.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,15 @@ civictheme.settings:
225225
use_media_name:
226226
label: 'Use name of media'
227227
type: boolean
228+
search:
229+
type: mapping
230+
label: 'Search settings'
231+
mapping:
232+
keyword_fields:
233+
label: 'Keyword fields'
234+
type: sequence
235+
sequence:
236+
type: string
228237
colors:
229238
type: mapping
230239
label: Colors

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ function civictheme_preprocess_block__civictheme_search(array &$variables): void
4040

4141
/**
4242
* Implements hook_preprocess_html().
43+
*
44+
* @SuppressWarnings(PHPMD.StaticAccess)
4345
*/
4446
function _civictheme_preprocess_html__search_head_title(array &$variables): void {
4547
// Load search fields from theme settings.
@@ -61,13 +63,12 @@ function _civictheme_preprocess_html__search_head_title(array &$variables): void
6163
if (empty($keywords)) {
6264
return;
6365
}
64-
$head_title = (string) $variables['head_title']['title'] ?? '';
66+
$head_title = (string) ($variables['head_title']['title'] ?? '');
6567
if (empty($head_title)) {
6668
return;
6769
}
68-
$sanitized_keywords = htmlspecialchars($keywords, ENT_QUOTES, 'UTF-8');
6970
$variables['head_title']['title'] = t("@title - Searching for '@keywords'", [
7071
'@title' => $head_title,
71-
'@keywords' => $sanitized_keywords,
72+
'@keywords' => $keywords,
7273
]);
7374
}

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

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use Drupal\Component\Utility\Html;
1313
use Drupal\Core\Cache\Cache;
1414
use Drupal\Core\Form\FormStateInterface;
1515
use Drupal\Core\Template\Attribute;
16+
use Drupal\views\ViewExecutable;
1617
use Drupal\views\Views;
1718

1819
/**
@@ -162,19 +163,9 @@ function civictheme_preprocess_views_exposed_form(array &$variables): void {
162163
}
163164

164165
if ($field_count == 1) {
165-
// Use inline filter on search and auto pages if a single text field exsits.
166-
if (in_array($view->id(), ['civictheme_search', 'civictheme_automated_list'])) {
167-
$keyword_field = reset($fields);
168-
if ($keyword_field['#type'] === 'textfield') {
169-
$variables['inline_filter'] = TRUE;
170-
$keyword_field['#title_size'] = 'extra-large';
171-
$variables['filter_items'] = $keyword_field;
172-
$submit_field = $variables['form']['actions']['submit'] ?? NULL;
173-
if (!empty($submit_field)) {
174-
$variables['submit_text'] = $submit_field['#value'] ?? '';
175-
}
176-
return;
177-
}
166+
// Use inline filter on search and auto pages if a single text field exists.
167+
if (_civictheme_preprocess_views__exposed_form__inline_filter($variables, $view, $fields)) {
168+
return;
178169
}
179170

180171
// Use single filter.
@@ -185,6 +176,44 @@ function civictheme_preprocess_views_exposed_form(array &$variables): void {
185176
}
186177
}
187178

179+
/**
180+
* Preprocess views exposed form to convert it to the Inline filter.
181+
*
182+
* @param array $variables
183+
* Variables array.
184+
* @param \Drupal\views\ViewExecutable|null $view
185+
* The view object.
186+
* @param array $fields
187+
* Form fields.
188+
*
189+
* @return bool
190+
* TRUE if inline filter was processed, FALSE otherwise.
191+
*/
192+
function _civictheme_preprocess_views__exposed_form__inline_filter(array &$variables, $view, array $fields): bool {
193+
if (!($view instanceof ViewExecutable)) {
194+
return FALSE;
195+
}
196+
197+
if (!in_array($view->id(), ['civictheme_search', 'civictheme_automated_list'])) {
198+
return FALSE;
199+
}
200+
201+
$keyword_field = reset($fields);
202+
if ($keyword_field['#type'] !== 'textfield') {
203+
return FALSE;
204+
}
205+
206+
$variables['inline_filter'] = TRUE;
207+
$keyword_field['#title_size'] = 'extra-large';
208+
$variables['filter_items'] = $keyword_field;
209+
$submit_field = $variables['form']['actions']['submit'] ?? NULL;
210+
if (!empty($submit_field)) {
211+
$variables['submit_text'] = $submit_field['#value'] ?? '';
212+
}
213+
214+
return TRUE;
215+
}
216+
188217
/**
189218
* Preprocess views exposed form to convert it to the Single filter.
190219
*/
@@ -282,6 +311,8 @@ function _civictheme_preprocess_views_view__search_page(array &$variables): void
282311

283312
/**
284313
* Pre-process results count for views.
314+
*
315+
* @SuppressWarnings(PHPMD.StaticAccess)
285316
*/
286317
function _civictheme_preprocess_views_view__results_count(array &$variables): void {
287318
if (empty($variables['results_count'])) {
@@ -294,6 +325,8 @@ function _civictheme_preprocess_views_view__results_count(array &$variables): vo
294325
// Load search fields from theme settings.
295326
$setting_keyword_fields = civictheme_get_theme_config_manager()->load('components.search.keyword_fields') ?? '';
296327
if (empty($setting_keyword_fields)) {
328+
// Strip the @keywords token because it exists but won't be populated.
329+
$variables['results_count'] = str_replace('@keywords', '', $variables['results_count']);
297330
return;
298331
}
299332

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,18 @@ public function submitFooter(array &$form, FormStateInterface $form_state): void
603603
);
604604
}
605605

606+
/**
607+
* Submit callback for theme settings form of Search component.
608+
*
609+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
610+
* @SuppressWarnings(PHPMD.StaticAccess)
611+
*/
612+
public function submitSearch(array &$form, FormStateInterface $form_state): void {
613+
$keyword_fields = $form_state->getValue(['components', 'search', 'keyword_fields'], '');
614+
$keyword_fields = CivicthemeUtility::multilineToArray($keyword_fields);
615+
$form_state->setValue(['components', 'search', 'keyword_fields'], $keyword_fields);
616+
}
617+
606618
/**
607619
* Provide a description for a path field.
608620
*

0 commit comments

Comments
 (0)