diff --git a/js/dropdown-widget.js b/js/dropdown-widget.js index 2c8fc60..9f75dee 100644 --- a/js/dropdown-widget.js +++ b/js/dropdown-widget.js @@ -7,7 +7,6 @@ 'use strict'; - var $dropdown = ''); - $links.once('facets-dropdown-transform').each(Drupal.facets.makeOption); + // Add empty text option first. + var default_option_label = $(this).data('facet-default-option-label'); + var $default_option = $(''; + // Append dropdown. + $(this).find('.content').html($dropdown); + }); }; })(jQuery); diff --git a/src/Plugin/facets/widget/DropdownWidget.php b/src/Plugin/facets/widget/DropdownWidget.php index 0e48bdc..fb53ba8 100644 --- a/src/Plugin/facets/widget/DropdownWidget.php +++ b/src/Plugin/facets/widget/DropdownWidget.php @@ -51,7 +51,7 @@ class DropdownWidget extends LinksWidget{ $build = [ '#theme' => 'item_list', '#items' => $items, - '#attributes' => ['class' => ['js-facets-dropdown-links']], + '#attributes' => ['class' => ['js-facets-dropdown-links'], 'data-facet-default-option-label' => $configuration['default_option_label']], '#cache' => [ 'contexts' => [ 'url.path', @@ -64,4 +64,36 @@ class DropdownWidget extends LinksWidget{ return $build; } + /** + * {@inheritdoc} + */ + public function buildConfigurationForm(array $form, FormStateInterface $form_state, $config) { + $message = $this->t('This widget requires "Make sure only one result can be shown." to be enabled to behave as a standard dropdown.'); + $form['warning'] = [ + '#markup' => '
' . $message . '
', + ]; + + $form['show_numbers'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Show the amount of results'), + ]; + + $form['default_option_label'] = [ + '#type' => 'textfield', + '#title' => $this->t('Default option label'), + ]; + + 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']; + } + if (isset($widget_configs['default_option_label'])) { + $form['default_option_label']['#default_value'] = $widget_configs['default_option_label']; + } + } + + return $form; + } + }