View source
<?php
namespace Drupal\facets\Widget;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Plugin\PluginBase;
use Drupal\Core\Url;
use Drupal\facets\FacetInterface;
use Drupal\facets\Result\Result;
use Drupal\facets\Result\ResultInterface;
abstract class WidgetPluginBase extends PluginBase implements WidgetPluginInterface {
protected $showNumbers;
protected $facet;
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this
->setConfiguration($configuration);
}
public function build(FacetInterface $facet) {
$this->facet = $facet;
$items = array_map(function (Result $result) use ($facet) {
if (empty($result
->getUrl())) {
return $this
->buildResultItem($result);
}
else {
if ($result
->getUrl()
->isRouted() && $result
->getUrl()
->getRouteName() === 'facets.block.ajax') {
$request = \Drupal::request();
$url_object = \Drupal::service('path.validator')
->getUrlIfValid($request
->getPathInfo());
if ($url_object) {
$url = $result
->getUrl();
$options = $url
->getOptions();
$route_params = $url_object
->getRouteParameters();
$route_name = $url_object
->getRouteName();
$result
->setUrl(new Url($route_name, $route_params, $options));
}
}
return $this
->buildListItems($facet, $result);
}
}, $facet
->getResults());
$widget = $facet
->getWidget();
return [
'#theme' => $this
->getFacetItemListThemeHook($facet),
'#facet' => $facet,
'#items' => $items,
'#attributes' => [
'data-drupal-facet-id' => $facet
->id(),
'data-drupal-facet-alias' => $facet
->getUrlAlias(),
'class' => [
$facet
->getActiveItems() ? 'facet-active' : 'facet-inactive',
],
],
'#context' => !empty($widget['type']) ? [
'list_style' => $widget['type'],
] : [],
'#cache' => [
'contexts' => [
'url.path',
'url.query_args',
],
],
];
}
protected function getFacetItemListThemeHook(FacetInterface $facet) {
$type = $facet
->getWidget()['type'] ?? 'std';
return 'facets_item_list__' . $type . '__' . $facet
->id();
}
public function defaultConfiguration() {
return [
'show_numbers' => FALSE,
];
}
public function setConfiguration(array $configuration) {
$this->configuration = NestedArray::mergeDeep($this
->defaultConfiguration(), $configuration);
}
public function getConfiguration() {
return $this->configuration;
}
public function getQueryType() {
return NULL;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state, FacetInterface $facet) {
$form['show_numbers'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Show the amount of results'),
'#default_value' => $this
->getConfiguration()['show_numbers'],
];
return $form;
}
public function calculateDependencies() {
return [];
}
protected function buildListItems(FacetInterface $facet, ResultInterface $result) {
$classes = [
'facet-item',
];
$items = $this
->prepareLink($result);
$children = $result
->getChildren();
if ($children && ($this->facet
->getExpandHierarchy() || $result
->isActive() || $result
->hasActiveChildren())) {
$child_items = [];
$classes[] = 'facet-item--expanded';
foreach ($children as $child) {
$child_items[] = $this
->buildListItems($facet, $child);
}
$items['children'] = [
'#theme' => $this
->getFacetItemListThemeHook($facet),
'#items' => $child_items,
];
if ($result
->hasActiveChildren()) {
$classes[] = 'facet-item--active-trail';
}
}
else {
if ($children) {
$classes[] = 'facet-item--collapsed';
}
}
if ($result
->isActive()) {
$items['#attributes']['class'][] = 'is-active';
}
$items['#wrapper_attributes'] = [
'class' => $classes,
];
$items['#attributes']['data-drupal-facet-item-id'] = Html::getClass($this->facet
->getUrlAlias() . '-' . strtr($result
->getRawValue(), ' \'\\"', '---'));
$items['#attributes']['data-drupal-facet-item-value'] = $result
->getRawValue();
$items['#attributes']['data-drupal-facet-item-count'] = $result
->getCount();
return $items;
}
protected function prepareLink(ResultInterface $result) {
$item = $this
->buildResultItem($result);
if (!is_null($result
->getUrl())) {
$item = (new Link($item, $result
->getUrl()))
->toRenderable();
}
return $item;
}
protected function buildResultItem(ResultInterface $result) {
$count = $result
->getCount();
return [
'#theme' => 'facets_result_item',
'#is_active' => $result
->isActive(),
'#value' => $result
->getDisplayValue(),
'#show_count' => $this
->getConfiguration()['show_numbers'] && $count !== NULL,
'#count' => $count,
'#facet' => $result
->getFacet(),
'#raw_value' => $result
->getRawValue(),
];
}
public function isPropertyRequired($name, $type) {
return FALSE;
}
public function supportsFacet(FacetInterface $facet) {
return TRUE;
}
}