Skip to content

Commit

Permalink
ELA-695: Facets checkbox widget.
Browse files Browse the repository at this point in the history
  • Loading branch information
julien- committed Aug 31, 2023
1 parent 2cdfbb9 commit d6b56d1
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 63 deletions.
7 changes: 7 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@
"build/themes/contrib/{$name}": ["type:drupal-theme"]
},
"patches": {},
"patches-ignore": {
"openeuropa/open_vocabularies" : {
"drupal/core" : {
"Entity display entities are incorrectly unserialized @see https://www.drupal.org/project/drupal/issues/3171333" : "https://www.drupal.org/files/issues/2020-09-17/3171333-6.patch"
}
}
},
"drupal-scaffold": {
"locations": {
"web-root": "./build"
Expand Down
2 changes: 1 addition & 1 deletion src/Plugin/MultiselectFilterField/ListField.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function getDefaultValuesLabel(): string {
return '';
}

$filter_value = parent::getDefaultValues();
$filter_value = array_filter(parent::getDefaultValues());
return implode(', ', array_map(function ($value) use ($field_definition) {
return $field_definition->getSetting('allowed_values')[$value];
}, $filter_value));
Expand Down
62 changes: 0 additions & 62 deletions src/Plugin/facets/widget/CheckboxWidget.php

This file was deleted.

94 changes: 94 additions & 0 deletions src/Plugin/facets/widget/MultiselectCheckboxWidget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

declare(strict_types = 1);

namespace Drupal\oe_list_pages\Plugin\facets\widget;

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\facets\FacetInterface;
use Drupal\facets\Result\Result;
use Drupal\oe_list_pages\ListPresetFilter;

/**
* The checkbox widget.
*
* @FacetsWidget(
* id = "oe_list_pages_checkbox",
* label = @Translation("List pages checkbox"),
* description = @Translation("A checkbox filter widget"),
* )
*/
class MultiselectCheckboxWidget extends MultiselectWidget implements ContainerFactoryPluginInterface {

/**
* {@inheritdoc}
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function buildDefaultValueForm(array $form, FormStateInterface $form_state, FacetInterface $facet, ListPresetFilter $preset_filter = NULL): array {
$form = parent::buildDefaultValueForm($form, $form_state, $facet, $preset_filter);
// No need to set operator for checkboxes.
$form['oe_list_pages_filter_operator']['#access'] = FALSE;
$form['oe_list_pages_filter_operator']['#default_value'] = 'or';

$list_source = $form_state->get('list_source');
$plugin_id = $this->multiselectPluginManager->getPluginIdForFacet($facet, $list_source);
$filter_values = $preset_filter ? $preset_filter->getValues() : [];
$form[$facet->id()] = $form[$facet->id()][$plugin_id];
$form[$facet->id()]['#type'] = 'checkboxes';
$form[$facet->id()]['#default_value'] = array_filter($filter_values);
return $form;
}

/**
* {@inheritdoc}
*/
public function prepareDefaultFilterValue(FacetInterface $facet, array $form, FormStateInterface $form_state): array {
echo (__FUNCTION__);
return [
'operator' => $form_state->getValue('oe_list_pages_filter_operator'),
'values' => $form_state->getValue($facet->id()) ?? [],
];
}

/**
* {@inheritdoc}
*/
public function build(FacetInterface $facet) {
$results = $facet->getResults();
$results = array_filter($results, function (Result $result) {
return $result->getDisplayValue() !== "";
});

$options = $this->transformResultsToOptions($results);

if ($options) {
$build[$facet->id()] = [
'#type' => 'checkboxes',
'#title' => $facet->getName(),
'#options' => $options,
'#default_value' => $facet->getActiveItems(),
];
}

$build['#cache']['contexts'] = [
'url.query_args',
'url.path',
];

return $build;
}

/**
* {@inheritdoc}
*/
public function prepareValueForUrl(FacetInterface $facet, array &$form, FormStateInterface $form_state): array {
$values = $form_state->getValue($facet->id());
// Remove "0" values, unchecked checkboxes.
$values = array_filter($values);
return $values;
}

}

0 comments on commit d6b56d1

Please sign in to comment.