Skip to content

Commit

Permalink
added user facet for log
Browse files Browse the repository at this point in the history
  • Loading branch information
mkalkbrenner committed Jan 16, 2025
1 parent 37237d5 commit 8e19658
Show file tree
Hide file tree
Showing 2 changed files with 237 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,61 @@ display:
sort: 50
settings:
sort: ASC
facets_uid:
id: facets_uid
table: search_api_index_search_api_solr_log
field: facets_uid
relationship: none
group_type: group
admin_label: ''
plugin_id: facets_filter
operator: '='
value: ''
group: 1
exposed: true
expose:
operator_id: ''
label: User
description: ''
use_operator: false
operator: ''
operator_limit_selection: false
operator_list: { }
identifier: uid
required: false
remember: false
multiple: true
remember_roles:
authenticated: authenticated
is_grouped: false
group_info:
label: ''
description: ''
identifier: ''
optional: true
widget: select
multiple: false
remember: false
default_group: All
default_group_multiple: { }
group_items: { }
hierarchy: false
label_display: visible
facet:
query_operator: or
min_count: 1
show_numbers: true
processor_configs:
search_api_solr_log_user:
weights:
post_query: 50
settings:
keep_uid: 1
raw_value_widget_order:
weights:
sort: 50
settings:
sort: ASC
facets_timestamp:
id: facets_timestamp
table: search_api_index_search_api_solr_log
Expand Down Expand Up @@ -2042,6 +2097,61 @@ display:
sort: 50
settings:
sort: ASC
facets_uid:
id: facets_uid
table: search_api_index_search_api_solr_log
field: facets_uid
relationship: none
group_type: group
admin_label: ''
plugin_id: facets_filter
operator: '='
value: ''
group: 1
exposed: true
expose:
operator_id: ''
label: User
description: ''
use_operator: false
operator: ''
operator_limit_selection: false
operator_list: { }
identifier: uid
required: false
remember: false
multiple: true
remember_roles:
authenticated: authenticated
is_grouped: false
group_info:
label: ''
description: ''
identifier: ''
optional: true
widget: select
multiple: false
remember: false
default_group: All
default_group_multiple: { }
group_items: { }
hierarchy: false
label_display: visible
facet:
query_operator: or
min_count: 1
show_numbers: true
processor_configs:
search_api_solr_log_user:
weights:
post_query: 50
settings:
keep_uid: 1
raw_value_widget_order:
weights:
sort: 50
settings:
sort: ASC
facets_timestamp:
id: facets_timestamp
table: search_api_index_search_api_solr_log
Expand Down Expand Up @@ -2390,6 +2500,61 @@ display:
sort: 50
settings:
sort: ASC
facets_uid:
id: facets_uid
table: search_api_index_search_api_solr_log
field: facets_uid
relationship: none
group_type: group
admin_label: ''
plugin_id: facets_filter
operator: '='
value: ''
group: 1
exposed: true
expose:
operator_id: ''
label: User
description: ''
use_operator: false
operator: ''
operator_limit_selection: false
operator_list: { }
identifier: uid
required: false
remember: false
multiple: true
remember_roles:
authenticated: authenticated
is_grouped: false
group_info:
label: ''
description: ''
identifier: ''
optional: true
widget: select
multiple: false
remember: false
default_group: All
default_group_multiple: { }
group_items: { }
hierarchy: false
label_display: visible
facet:
query_operator: or
min_count: 1
show_numbers: true
processor_configs:
search_api_solr_log_user:
weights:
post_query: 50
settings:
keep_uid: 1
raw_value_widget_order:
weights:
sort: 50
settings:
sort: ASC
facets_timestamp:
id: facets_timestamp
table: search_api_index_search_api_solr_log
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Drupal\search_api_solr_log\Plugin\facets\processor;

use Drupal\Core\Cache\UnchangingCacheableDependencyTrait;
use Drupal\Core\Form\FormStateInterface;
use Drupal\facets\FacetInterface;
use Drupal\facets\Processor\PostQueryProcessorInterface;
use Drupal\facets\Processor\ProcessorPluginBase;
use Drupal\user\Entity\User;

/**
* Replaces facet values based on a given mapping.
*
* @FacetsProcessor(
* id = "search_api_solr_log_user",
* label = @Translation("Translate User IDs to names."),
* description = @Translation("Display user names instead of user IDs provided as raw integers, for example in recent log messages."),
* stages = {
* "post_query" = 50,
* },
* )
*/
class UserProcessor extends ProcessorPluginBase implements PostQueryProcessorInterface {

use UnchangingCacheableDependencyTrait;

/**
* {@inheritdoc}
*/
public function defaultConfiguration(): array {
return [
'keep_uid' => TRUE,
] + parent::defaultConfiguration();
}

/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state, FacetInterface $facet): array {
$keep_uid = $this->getConfiguration()['keep_uid'];
$build['keep_uid'] = [
'#title' => $this->t('Keep UID'),
'#type' => 'checkbox',
'#default_value' => $keep_uid,
'#description' => $this->t("Display user ID in addition to the name."),
];
return $build;
}

/**
* {@inheritdoc}
*/
public function postQuery(FacetInterface $facet): void {
$keep_uid = (bool) $this->getConfiguration()['keep_uid'];
foreach ($facet->getResults() as $result) {
$user_id = $result->getRawValue();
// Load the user entity.
$user = User::load($user_id);

if ($user) {
// Generate the URL to the user profile.
$name = $user->getDisplayName();
if ($keep_uid) {
$name .= ' [' . $user_id . ']';
}
$result->setDisplayValue($name);
}
}
}

}

0 comments on commit 8e19658

Please sign in to comment.