diff --git a/facets.libraries.yml b/facets.libraries.yml
index 62daaf3..23cbc63 100644
--- a/facets.libraries.yml
+++ b/facets.libraries.yml
@@ -28,3 +28,13 @@ drupal.facets.checkbox-widget:
dependencies:
- core/jquery
- core/drupal
+ - core/jquery.once
+
+drupal.facets.dropdown-widget:
+ version: VERSION
+ js:
+ js/dropdown-widget.js: {}
+ dependencies:
+ - core/jquery
+ - core/drupal
+ - core/jquery.once
\ No newline at end of file
diff --git a/js/dropdown-widget.js b/js/dropdown-widget.js
new file mode 100644
index 0000000..8eb113f
--- /dev/null
+++ b/js/dropdown-widget.js
@@ -0,0 +1,50 @@
+/**
+ * @file
+ * Transforms links into a dropdown list.
+ */
+
+(function ($) {
+
+ "use strict";
+
+ var $dropdown = '";
+
+ $('.js-facets-dropdown-links .item-list').html($dropdown);
+
+ // Go to the selected option when it's clicked.
+ $('.facets-dropdown').on('change', function() {
+ window.location.href = this.value;
+ });
+ };
+
+ /**
+ * Create an option for the dropdown list and append it to it.
+ */
+ Drupal.facets.makeOption = function () {
+ var $link = $(this)[0];
+ var href = $($link.innerHTML)[0].href;
+ var text = $link.innerText.replace('(-) ','');
+ var option = '';
+
+ $dropdown += option;
+ };
+
+})(jQuery);
diff --git a/src/Form/DropdownWidgetForm.php b/src/Form/DropdownWidgetForm.php
deleted file mode 100644
index afd09d9..0000000
--- a/src/Form/DropdownWidgetForm.php
+++ /dev/null
@@ -1,111 +0,0 @@
-facet = $facet;
- }
-
- /**
- * {@inheritdoc}
- */
- public function getBaseFormId() {
- return 'facets_dropdown_widget';
- }
-
- /**
- * {@inheritdoc}
- */
- public function getFormId() {
- return $this->getBaseFormId() . '__' . $this->facet->id();
- }
-
- /**
- * {@inheritdoc}
- */
- public function buildForm(array $form, FormStateInterface $form_state) {
- $results = $this->facet->getResults();
-
- $configuration = $this->facet->getWidgetConfigs();
- $form[$this->facet->getFieldAlias()] = [
- '#type' => 'select',
- '#title' => $this->facet->getName(),
- '#default_value' => '_none',
- ];
-
- $options = [];
- $active_result_url = '_none';
- foreach ($results as $result) {
- $result_url = $result->getUrl()->setAbsolute()->toString();
-
- $text = $result->getDisplayValue();
- if (!empty($configuration['show_numbers'])) {
- $text .= ' (' . $result->getCount() . ')';
- }
-
- if ($result->isActive()) {
- $options['_none'] = $text;
- $active_result_url = $result_url;
- }
- else {
- $options[$result_url] = $text;
- }
- }
-
- $options = [$active_result_url => $this->t('- All -')] + $options;
-
- $form[$this->facet->getFieldAlias()]['#options'] = $options;
-
- $form[$this->facet->getFieldAlias() . '_submit'] = [
- '#type' => 'submit',
- '#value' => 'submit',
- ];
-
- return $form;
- }
-
- /**
- * {@inheritdoc}
- */
- public function validateForm(array &$form, FormStateInterface $form_state) {
-
- }
-
- /**
- * {@inheritdoc}
- */
- public function submitForm(array &$form, FormStateInterface $form_state) {
- $field_alias = $this->facet->getFieldAlias();
- $form_value = $form_state->getValue($field_alias);
- if ($form_value != '_none') {
- $form_state->setResponse(new RedirectResponse($form_value));
- }
- }
-
-}
diff --git a/src/Plugin/facets/widget/DropdownWidget.php b/src/Plugin/facets/widget/DropdownWidget.php
index 5a90347..0e48bdc 100644
--- a/src/Plugin/facets/widget/DropdownWidget.php
+++ b/src/Plugin/facets/widget/DropdownWidget.php
@@ -2,11 +2,8 @@
namespace Drupal\facets\Plugin\facets\widget;
-use Drupal\Core\Form\FormStateInterface;
-use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\facets\FacetInterface;
-use Drupal\facets\Form\DropdownWidgetForm;
-use Drupal\facets\Widget\WidgetInterface;
+use Drupal\Core\StringTranslation\StringTranslationTrait;
/**
* The dropdown widget.
@@ -17,44 +14,54 @@ use Drupal\facets\Widget\WidgetInterface;
* description = @Translation("A configurable widget that shows a dropdown."),
* )
*/
-class DropdownWidget implements WidgetInterface {
- use StringTranslationTrait;
+class DropdownWidget extends LinksWidget{
/**
- * {@inheritdoc}
+ * The facet the widget is being built for.
+ *
+ * @var \Drupal\facets\FacetInterface
*/
- public function build(FacetInterface $facet) {
- $form_builder = \Drupal::getContainer()->get('form_builder');
- $form_object = new DropdownWidgetForm($facet);
- return $form_builder->getForm($form_object);
- }
+ protected $facet;
/**
* {@inheritdoc}
*/
- public function getQueryType($query_types) {
- return $query_types['string'];
- }
+ public function build(FacetInterface $facet) {
- /**
- * {@inheritdoc}
- */
- public function buildConfigurationForm(array $form, FormStateInterface $form_state, $config) {
+ $this->facet = $facet;
- $form['show_numbers'] = [
- '#type' => 'checkbox',
- '#title' => $this->t('Show the amount of results'),
- ];
+ /** @var \Drupal\facets\Result\Result[] $results */
+ $results = $facet->getResults();
+ $items = [];
+
+ $configuration = $facet->getWidgetConfigs();
+ $this->showNumbers = empty($configuration['show_numbers']) ? FALSE : (bool) $configuration['show_numbers'];
- if (!is_null($config)) {
- $widget_configs = $config->get('widget_configs');
- if (isset($widget_configs['show_numbers'])) {
- $form['show_numbers']['#default_value'] = $widget_configs['show_numbers'];
+ foreach ($results as $result) {
+ if (is_null($result->getUrl())) {
+ $text = $this->extractText($result);
+ $items[] = ['#markup' => $text];
+ }
+ else {
+ $items[] = $this->buildListItems($result);
}
}
- return $form;
+ $build = [
+ '#theme' => 'item_list',
+ '#items' => $items,
+ '#attributes' => ['class' => ['js-facets-dropdown-links']],
+ '#cache' => [
+ 'contexts' => [
+ 'url.path',
+ 'url.query_args',
+ ],
+ ],
+ ];
+ $build['#attached']['library'][] = 'facets/drupal.facets.dropdown-widget';
+
+ return $build;
}
}