facets-8.x-1.x-dev/modules/facets_summary/facets_summary.module
modules/facets_summary/facets_summary.module
<?php
/**
* @file
* Hook implementations for the facets summary module.
*/
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Site\Settings;
use Drupal\facets_summary\Entity\FacetsSummary;
use Drupal\search_api\Entity\Index;
/**
* Implements hook_theme().
*/
function facets_summary_theme($existing, $type, $theme, $path) {
return [
'facets_summary_count' => [
'variables' => [
'count' => NULL,
],
],
'facets_summary_facet' => [
'render element' => 'elements',
'variables' => [
'label' => NULL,
'separator' => '',
'items' => [],
'facet_id' => NULL,
'facet_admin_label' => NULL,
],
],
'facets_summary_facet_result' => [
'variables' => [
'label' => NULL,
'show_count' => FALSE,
'count' => NULL,
'facet_id' => NULL,
],
],
'facets_summary_empty' => [
'variables' => [
'message' => '',
],
],
'facets_summary_item_list' => [
'variables' => [
'items' => [],
'title' => '',
'list_type' => 'ul',
'wrapper_attributes' => [],
'attributes' => [],
'empty' => NULL,
'context' => [],
'facet_summary_id' => NULL,
],
],
];
}
/**
* Implements hook_theme_suggestions_HOOK().
*/
function facets_summary_theme_suggestions_facets_summary_facet(array $variables) {
// Add suggestions as: facets-summary-facet--{facet_id}.
return ['facets_summary_facet__' . $variables['facet_id']];
}
/**
* Implements hook_theme_suggestions_HOOK().
*/
function facets_summary_theme_suggestions_facets_summary_facet_result(array $variables) {
// Add suggestions as: facets-summary-facet-result--{facet_id}.
return [$variables['theme_hook_original'] . '__' . $variables['facet_id']];
}
/**
* Implements hook_theme_suggestions_HOOK().
*/
function facets_summary_theme_suggestions_facets_summary_item_list(array $variables) {
return [$variables['theme_hook_original'] . '__' . $variables['facet_summary_id']];
}
/**
* Prepares variables for facets summary item list templates.
*
* Default template: facets-summary-item-list.html.twig.
*
* @param array $variables
* An associative array containing:
* - items: An array of items to be displayed in the list. Each item can be
* either a string or a render array. If #type, #theme, or #markup
* properties are not specified for child render arrays, they will be
* inherited from the parent list, allowing callers to specify larger
* nested lists without having to explicitly specify and repeat the
* render properties for all nested child lists.
* - title: A title to be prepended to the list.
* - list_type: The type of list to return (e.g. "ul", "ol").
* - wrapper_attributes: HTML attributes to be applied to the list wrapper.
*
* @see https://www.drupal.org/node/1842756
*/
function facets_summary_preprocess_facets_summary_item_list(array &$variables) {
template_preprocess_item_list($variables);
if (Settings::get('facets_debug_cacheable_metadata', FALSE)) {
if ($facetsSummary = FacetsSummary::load($variables['facet_summary_id'])) {
$facetsSummaryManager = \Drupal::service('facets_summary.manager');
$cacheableMetadata = new CacheableMetadata();
foreach ($facetsSummaryManager->getFacets($facetsSummary) as $facet) {
$cacheableMetadata->addCacheableDependency($facet);
}
$variables['cache_hash'] = substr(base_convert(hash('sha256', uniqid(time())), 16, 36), 0, 6);
$variables['cache_contexts'] = implode(', ', $cacheableMetadata->getCacheContexts());
$variables['cache_tags'] = implode(', ', $cacheableMetadata->getCacheTags());
$variables['cache_max_age'] = $cacheableMetadata->getCacheMaxAge() . ' seconds';
}
}
}
/**
* Implements hook_views_data_alter().
*/
function facets_summary_views_data_alter(array &$data) {
/** @var \Drupal\search_api\IndexInterface $index */
foreach (Index::loadMultiple() as $index) {
$data['search_api_index_' . $index->id()]['facets_summary'] = [
'title' => t('Facets summary'),
'help' => t('Displays facets summary in a filter or area'),
'filter' => [
'id' => 'facets_summary_filter',
],
'area' => [
'id' => 'facets_summary_area',
],
];
}
}
