diff --git a/facets.libraries.yml b/facets.libraries.yml index b6278be..04dd922 100644 --- a/facets.libraries.yml +++ b/facets.libraries.yml @@ -3,3 +3,10 @@ drupal.facets.admin_css: css: theme: css/facets.admin.css: {} + +checkboxWidget: + version: 1.x + js: + js/checkboxWidget.js: {} + dependencies: + - core/jquery \ No newline at end of file diff --git a/js/checkboxWidget.js b/js/checkboxWidget.js new file mode 100644 index 0000000..34c0927 --- /dev/null +++ b/js/checkboxWidget.js @@ -0,0 +1,25 @@ +/** + * @file + * Handles click on "fake" checkboxes on the widget checkboxWidget.php + */ + +(function ($, Drupal, drupalSettings) { + + 'use strict'; + + /** + * Handles the click event on the checboxes, launches the tag. + * + * @type {Drupal~behavior} + * + * @prop {Drupal~behaviorAttach} attach + */ + Drupal.behaviors.CheckBoxWidget = {}; + Drupal.behaviors.CheckBoxWidget.attach = function () { + $('.facets-checkbox').click(function () { + $(this).parent('.form-type-checkbox').find('a')[0].click(); + }); + }; + + +})(jQuery, Drupal, drupalSettings); diff --git a/src/Plugin/facets/widget/CheckboxWidget.php b/src/Plugin/facets/widget/CheckboxWidget.php index 6d8bf49..c279e43 100644 --- a/src/Plugin/facets/widget/CheckboxWidget.php +++ b/src/Plugin/facets/widget/CheckboxWidget.php @@ -46,32 +46,42 @@ class CheckboxWidget implements WidgetInterface { public function build(FacetInterface $facet) { /** @var \Drupal\facets\Result\Result[] $results */ $results = $facet->getResults(); - $items = []; + $form = array(); $configuration = $facet->get('widget_configs'); $show_numbers = (bool) $configuration['show_numbers']; + $options = []; foreach ($results as $result) { - if ($result->getCount()) { - // Get the link. - $text = $result->getDisplayValue(); - if ($show_numbers) { - $text .= ' (' . $result->getCount() . ')'; - } - if ($result->isActive()) { - $text = '(-) ' . $text; - } - $link = $this->linkGenerator()->generate($text, $result->getUrl()); - $items[] = $link; + // Get the link. + $text = $result->getDisplayValue(); + + if ($show_numbers) { + $text .= ' (' . $result->getCount() . ')'; + } + + $url = $result->getUrl()->toString(); + $form[$url] = array( + '#type' => 'checkbox', + '#title' => '' . $text . '', + '#attributes' => ['class' => ['facets-checkbox']] + ); + + if ($result->isActive()) { + $form[$url]['#attributes']['checked'] = 'checked'; } } - $build = [ - '#theme' => 'item_list', - '#items' => $items, - ]; - $build['#prefix'] = $this->t('Checkboxes'); - return $build; + $form['#attached']['library'][] = "facets/checkboxWidget"; + + return $form; + } + + /** + * {@inheritdoc} + */ + public function getQueryType($query_types) { + return $query_types['string']; } /** @@ -95,13 +105,6 @@ class CheckboxWidget implements WidgetInterface { } /** - * {@inheritdoc} - */ - public function getQueryType($query_types) { - return $query_types['string']; - } - - /** * Gets the link generator. * * @return \Drupal\Core\Utility\LinkGeneratorInterface