diff --git a/advanced_search.module b/advanced_search.module index 437dfba..ed42223 100644 --- a/advanced_search.module +++ b/advanced_search.module @@ -118,7 +118,9 @@ function advanced_search_preprocess_views_view(&$variables) { // has been created for. if (in_array([$view->id(), $view->current_display], $views)) { // Toggle between 'list' and 'grid' display depending on url parameter. - $format = \Drupal::request()->query->get('display') ?? 'grid'; + $config = \Drupal::config(SettingsForm::CONFIG_NAME); + + $format = \Drupal::request()->query->get('display') ?? $config->get(SettingsForm::DISPLAY_DEFAULT); $variables['attributes']['class'][] = "view-{$format}"; $view->element['#attached']['library'][] = 'advanced_search/advanced.search.pager'; } diff --git a/src/Form/SettingsForm.php b/src/Form/SettingsForm.php index 1518c4f..1ddc211 100644 --- a/src/Form/SettingsForm.php +++ b/src/Form/SettingsForm.php @@ -25,7 +25,10 @@ class SettingsForm extends ConfigFormBase { const EDISMAX_SEARCH_FLAG = 'lucene_on_off'; const EDISMAX_SEARCH_LABEL = 'lucene_label'; const SEARCH_ALL_FIELDS_FLAG = 'all_fields_on_off'; - + const DISPLAY_LIST_FLAG = 'list_on_off'; + const DISPLAY_GRID_FLAG = 'grid_on_off'; + const DISPLAY_DEFAULT = 'default-display-mode'; + /** * Constructs a \Drupal\system\ConfigFormBase object. * @@ -105,6 +108,36 @@ class SettingsForm extends ConfigFormBase { ], ]; + $form['display-mode'] = [ + '#type' => 'fieldset', + '#title' => $this->t("Display Mode"), + ]; + + $form['display-mode'][self::DISPLAY_LIST_FLAG] = [ + '#type' => 'checkbox', + '#title' => $this + ->t('Enable List View.'), + '#default_value' => self::getConfig(self::DISPLAY_LIST_FLAG, 0), + ]; + + $form['display-mode'][self::DISPLAY_GRID_FLAG] = [ + '#type' => 'checkbox', + '#title' => $this + ->t('Enable Grid View.'), + '#default_value' => self::getConfig(self::DISPLAY_GRID_FLAG, 0), + ]; + + $form['display-mode'][self::DISPLAY_DEFAULT] = [ + '#type' => 'select', + '#title' => $this + ->t('Default display mode:'), + '#options' => [ + 'list' => 'List', + 'grid' => 'Grid' + ], + '#default_value' => self::getConfig(self::DISPLAY_DEFAULT, 'grid'), + ]; + $form['edismax'] = [ '#type' => 'fieldset', '#title' => $this->t("Extended DisMax Query"), @@ -160,6 +193,9 @@ class SettingsForm extends ConfigFormBase { ->set(self::EDISMAX_SEARCH_FLAG, $form_state->getValue(self::EDISMAX_SEARCH_FLAG)) ->set(self::EDISMAX_SEARCH_LABEL, $form_state->getValue(self::EDISMAX_SEARCH_LABEL)) ->set(self::SEARCH_ALL_FIELDS_FLAG, $form_state->getValue(self::SEARCH_ALL_FIELDS_FLAG)) + ->set(self::DISPLAY_LIST_FLAG, $form_state->getValue(self::DISPLAY_LIST_FLAG)) + ->set(self::DISPLAY_GRID_FLAG, $form_state->getValue(self::DISPLAY_GRID_FLAG)) + ->set(self::DISPLAY_DEFAULT, $form_state->getValue(self::DISPLAY_DEFAULT)) ->save(); parent::submitForm($form, $form_state); } diff --git a/src/Plugin/Block/SearchResultsPagerBlock.php b/src/Plugin/Block/SearchResultsPagerBlock.php index 972851c..d8f9b6b 100644 --- a/src/Plugin/Block/SearchResultsPagerBlock.php +++ b/src/Plugin/Block/SearchResultsPagerBlock.php @@ -12,6 +12,7 @@ use Drupal\views\Plugin\views\pager\SqlBase; use Drupal\views\ViewExecutable; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\Request; +use Drupal\advanced_search\Form\SettingsForm; /** * Provides a 'AjaxViewBlock' block. @@ -212,17 +213,24 @@ class SearchResultsPagerBlock extends BlockBase implements ContainerFactoryPlugi * A renderable array representing the display links portion of pager. */ protected function buildDisplayLinks(array $query_parameters) { - $active_display = $query_parameters['display'] ?? 'grid'; - $display_options = [ - 'list' => [ + $config = \Drupal::config(SettingsForm::CONFIG_NAME); + $display_options = []; + + if ($config->get(SettingsForm::DISPLAY_LIST_FLAG) == 1) { + $display_options['list'] = [ 'icon' => 'fa-list', 'title' => $this->t('List'), - ], - 'grid' => [ + ]; + } + + if ($config->get(SettingsForm::DISPLAY_GRID_FLAG) ==1) { + $display_options['grid'] = [ 'icon' => 'fa-th', - 'title' => $this->t('Grid'), - ], - ]; + 'title' => $this->t('Grid') + ]; + } + + $active_display = $query_parameters['display'] ?? $config->get(SettingsForm::DISPLAY_DEFAULT); $items = []; foreach ($display_options as $display => $options) { $url = Url::fromRoute('', [], [