Skip to content

Commit

Permalink
Contextual websearch on tags and categories. DDFFORM-788
Browse files Browse the repository at this point in the history
Embedding a contextualized version of the editorial search view directly
on 'Categories' and 'Tags' individual term pages.
This allows the user to find content that is associated with this term.
  • Loading branch information
rasben committed Sep 12, 2024
1 parent 92a6d5e commit 341bd86
Show file tree
Hide file tree
Showing 4 changed files with 209 additions and 0 deletions.
22 changes: 22 additions & 0 deletions config/sync/pathauto.pattern.category_term.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
uuid: 6d9e0463-1e18-45be-b9cd-9e30b50bdf53
langcode: en
status: true
dependencies:
module:
- taxonomy
id: category_term
label: 'Category term - embedded search'
type: 'canonical_entities:taxonomy_term'
pattern: '/search/web/categories/[term:name]'
selection_criteria:
0f0f2190-bf04-4b11-9d33-5404ab1352ef:
id: 'entity_bundle:taxonomy_term'
negate: false
uuid: 0f0f2190-bf04-4b11-9d33-5404ab1352ef
context_mapping:
taxonomy_term: taxonomy_term
bundles:
categories: categories
selection_logic: and
weight: -5
relationships: { }
22 changes: 22 additions & 0 deletions config/sync/pathauto.pattern.tag_term.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
uuid: 7b51f435-12a6-4a18-8f2b-6421bccf8ea2
langcode: en
status: true
dependencies:
module:
- taxonomy
id: tag_term
label: 'Tag term - embedded search'
type: 'canonical_entities:taxonomy_term'
pattern: '/search/web/tag/[term:name]'
selection_criteria:
570dcc15-8a7a-4d87-8d5b-1d56e4290331:
id: 'entity_bundle:taxonomy_term'
negate: false
uuid: 570dcc15-8a7a-4d87-8d5b-1d56e4290331
context_mapping:
taxonomy_term: taxonomy_term
bundles:
tags: tags
selection_logic: and
weight: -5
relationships: { }
93 changes: 93 additions & 0 deletions config/sync/views.view.editorial_search.yml
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ display:
query_tags: { }
relationships: { }
use_ajax: true
show_admin_links: false
header: { }
footer: { }
display_extenders: { }
Expand Down Expand Up @@ -259,3 +260,95 @@ display:
- 'config:field.storage.node.field_branch'
- 'config:search_api.index.content_events'
- 'search_api_list:content_events'
term_page:
id: term_page
display_title: 'Context by tag'
display_plugin: block
position: 2
display_options:
arguments:
categories:
id: categories
table: search_api_index_content_events
field: categories
relationship: none
group_type: group
admin_label: ''
plugin_id: search_api
default_action: empty
exception:
value: all
title_enable: false
title: All
title_enable: false
title: ''
default_argument_type: fixed
default_argument_options:
argument: ''
summary_options: { }
summary:
sort_order: asc
number_of_records: 0
format: default_summary
specify_validation: false
validate:
type: none
fail: 'not found'
validate_options: { }
break_phrase: false
not: false
tags:
id: tags
table: search_api_index_content_events
field: tags
relationship: none
group_type: group
admin_label: ''
plugin_id: search_api
default_action: empty
exception:
value: all
title_enable: false
title: All
title_enable: false
title: ''
default_argument_type: fixed
default_argument_options:
argument: ''
summary_options: { }
summary:
sort_order: asc
number_of_records: 0
format: default_summary
specify_validation: false
validate:
type: none
fail: 'not found'
validate_options: { }
break_phrase: false
not: false
filters: { }
filter_groups:
operator: AND
groups:
1: AND
defaults:
arguments: false
filters: false
filter_groups: false
display_description: ''
display_extenders: { }
allow:
items_per_page: false
cache_metadata:
max-age: -1
contexts:
- 'languages:language_content'
- 'languages:language_interface'
- url
- url.query_args
- 'user.node_grants:view'
tags:
- 'config:field.storage.node.field_branch'
- 'config:search_api.index.content_events'
- 'search_api_list:content_events'
72 changes: 72 additions & 0 deletions web/modules/custom/dpl_search/dpl_search.module
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Drupal\Core\Url;
use Drupal\dpl_react_apps\Controller\DplReactAppsController;
use Drupal\dpl_search\DplSearchSettings;
use Drupal\taxonomy\TermInterface;
use Drupal\views\ViewExecutable;
use Drupal\views\Views;

Expand Down Expand Up @@ -73,6 +74,10 @@ function dpl_search_preprocess_views_view(array &$variables): void {
return;
}

if ($view->current_display !== 'page') {
return;
}

// Hiding the exposed search filter, as we display it in the header.
unset($variables['exposed']);

Expand Down Expand Up @@ -137,6 +142,73 @@ function dpl_search_preprocess_page(array &$variables): void {
];
}

/**
* Implements theme_preprocess_taxonomy_term().
*
* Show a view of tagged content on terms of certain vocabularies.
* The view is identical to the editorial view on /search/web, but with
* contextual filters applied.
*/
function dpl_search_preprocess_taxonomy_term(array &$variables): void {
$view_mode = $variables['view_mode'] ?? NULL;

// If we're not on the full display, we don't want to load the search view.
if ($view_mode !== 'full') {
return;
}

$term = $variables['term'] ?? NULL;
$bundle = $term?->bundle();

// The vocabularies we allow as contextual filters in the embedded view.
// ! IMPORTANT ! - The order in this array matters, as views does not allow
// for named contextual filters - it MUST match the order that is set in
// the Views UI.
$allowed_contextual_vocabularies = [
'categories',
'tags',
];

if (!($term instanceof TermInterface) ||
!in_array($bundle, $allowed_contextual_vocabularies)) {
return;
}

$contextual_filters = [];

// Looping through the allowed vocabularies, and setting the current
// term as a contextual filter, if the term is in that vocabulary.
foreach ($allowed_contextual_vocabularies as $vocabulary) {
if ($vocabulary === $bundle) {
$contextual_filters[] = $term->id();
}
else {
$contextual_filters[] = 'all';
}
}

$view = Views::getView(DplSearchSettings::EDITORIAL_VIEW_ID);

if (!($view instanceof ViewExecutable)) {
return;
}

$view->setDisplay('term_page');
$view->setArguments($contextual_filters);
$view->execute();
$view->setTitle(t(
'Showing web results related to "@term" (@total_results)',
['@term' => $term->label(), '@total_results' => $view->total_rows],
['context' => 'dpl_search']
));

// We only want the view to show up - not the title, which cannot be removed
// through the Drupal admin UI.
unset($variables['content']);

$variables['content']['view'] = $view->buildRenderable('term_page');
}

/**
* Implements hook_theme().
*/
Expand Down

0 comments on commit 341bd86

Please sign in to comment.