Newer
Older

Joris Vercammen
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
namespace Drupal\facets\Widget;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Plugin\PluginBase;
use Drupal\facets\FacetInterface;
use Drupal\facets\Result\Result;
use Drupal\facets\Result\ResultInterface;
/**
* A base class for widgets that implements most of the boilerplate.
*/
abstract class WidgetPluginBase extends PluginBase implements WidgetPluginInterface {
/**
* Show the amount of results next to the result.
*
* @var bool
*/
protected $showNumbers;
/**
* The facet the widget is being built for.
*
* @var \Drupal\facets\FacetInterface
*/
protected $facet;
/**
* Constructs a plugin object.
*
* @param array $configuration
* (optional) An optional configuration to be passed to the plugin. If
* empty, the plugin is initialized with its default plugin configuration.
*/
public function __construct(array $configuration = []) {
$plugin_id = $this->getPluginId();
$plugin_definition = $this->getPluginDefinition();
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->setConfiguration($configuration);
}
/**
* {@inheritdoc}
*/
public function build(FacetInterface $facet) {
$this->facet = $facet;
$items = array_map(function (Result $result) {
if (empty($result->getUrl())) {
return $this->buildResultItem($result);

Joris Vercammen
committed
}
else {
return $this->buildListItems($result);
}
}, $facet->getResults());
return [
'#theme' => 'item_list',
'#items' => $items,

Brandon Williams
committed
'#attributes' => [
'data-drupal-facet-id' => $facet->id(),
'data-drupal-facet-alias' => $facet->getUrlAlias(),
],

Joris Vercammen
committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
'#cache' => [
'contexts' => [
'url.path',
'url.query_args',
],
],
];
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return ['show_numbers' => FALSE];
}
/**
* {@inheritdoc}
*/
public function setConfiguration(array $configuration) {
$this->configuration = NestedArray::mergeDeep(
$this->defaultConfiguration(),
$configuration
);
}
/**
* {@inheritdoc}
*/
public function getConfiguration() {
return $this->configuration;
}
/**
* {@inheritdoc}
*/
public function getQueryType(array $query_types) {

Joris Vercammen
committed
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
return $query_types['string'];
}
/**
* {@inheritdoc}
*/
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;
}
/**
* {@inheritdoc}
*/
public function calculateDependencies() {
return [];
}
/**
* Builds a renderable array of result items.
*
* @param \Drupal\facets\Result\ResultInterface $result
* A result item.
*
* @return array
* A renderable array of the result.
*/
protected function buildListItems(ResultInterface $result) {
$classes = ['facet-item'];

Jimmy Henderickx
committed
$items = $this->prepareLink($result);

Joris Vercammen
committed

Jimmy Henderickx
committed
$children = $result->getChildren();
// Check if we need to expand this result.
if ($children && ($this->facet->getExpandHierarchy() || $result->isActive() || $result->hasActiveChildren())) {
$child_items = [];
$classes[] = 'facet-item--expanded';

Joris Vercammen
committed
foreach ($children as $child) {

Jimmy Henderickx
committed
$child_items[] = $this->buildListItems($child);

Joris Vercammen
committed
}

Jimmy Henderickx
committed
$items['children'] = [
'#theme' => 'item_list',
'#items' => $child_items,
];

Joris Vercammen
committed

Jimmy Henderickx
committed
if ($result->hasActiveChildren()) {
$classes[] = 'facet-item--active-trail';

Joris Vercammen
committed
}

Jimmy Henderickx
committed

Joris Vercammen
committed
}
else {

Jimmy Henderickx
committed
if ($children) {
$classes[] = 'facet-item--collapsed';

Joris Vercammen
committed
}
}

Jimmy Henderickx
committed
if ($result->isActive()) {
$items['#attributes'] = ['class' => 'is-active'];
}

Joris Vercammen
committed
$items['#wrapper_attributes'] = ['class' => $classes];
$items['#attributes']['data-drupal-facet-item-id'] = $this->facet->getUrlAlias() . '-' . $result->getRawValue();

Brandon Williams
committed
$items['#attributes']['data-drupal-facet-item-value'] = $result->getRawValue();

Joris Vercammen
committed
return $items;
}
/**
* Returns the text or link for an item.
*
* @param \Drupal\facets\Result\ResultInterface $result
* A result item.
*
* @return array
* The item as a render array.

Joris Vercammen
committed
*/
protected function prepareLink(ResultInterface $result) {
$item = $this->buildResultItem($result);
if (!is_null($result->getUrl())) {
$item = (new Link($item, $result->getUrl()))->toRenderable();

Joris Vercammen
committed
}
return $item;

Joris Vercammen
committed
}
/**
* Builds a facet result item.

Joris Vercammen
committed
*
* @param \Drupal\facets\Result\ResultInterface $result
* The result item.

Joris Vercammen
committed
*
* @return array
* The facet result item as a render array.

Joris Vercammen
committed
*/
protected function buildResultItem(ResultInterface $result) {
$count = $result->getCount();
return [

Joris Vercammen
committed
'#theme' => 'facets_result_item',
'#is_active' => $result->isActive(),
'#value' => $result->getDisplayValue(),
'#show_count' => $this->getConfiguration()['show_numbers'] && ($count !== NULL),
'#count' => $count,
];