Added config for turn on/off display mode (grid and list)

This commit is contained in:
Kyle Huynh 2023-02-02 11:15:08 -05:00
parent 64fe710adc
commit d30c8eda8a
3 changed files with 56 additions and 10 deletions

View file

@ -118,7 +118,9 @@ function advanced_search_preprocess_views_view(&$variables) {
// has been created for. // has been created for.
if (in_array([$view->id(), $view->current_display], $views)) { if (in_array([$view->id(), $view->current_display], $views)) {
// Toggle between 'list' and 'grid' display depending on url parameter. // 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}"; $variables['attributes']['class'][] = "view-{$format}";
$view->element['#attached']['library'][] = 'advanced_search/advanced.search.pager'; $view->element['#attached']['library'][] = 'advanced_search/advanced.search.pager';
} }

View file

@ -25,7 +25,10 @@ class SettingsForm extends ConfigFormBase {
const EDISMAX_SEARCH_FLAG = 'lucene_on_off'; const EDISMAX_SEARCH_FLAG = 'lucene_on_off';
const EDISMAX_SEARCH_LABEL = 'lucene_label'; const EDISMAX_SEARCH_LABEL = 'lucene_label';
const SEARCH_ALL_FIELDS_FLAG = 'all_fields_on_off'; 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. * 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'] = [ $form['edismax'] = [
'#type' => 'fieldset', '#type' => 'fieldset',
'#title' => $this->t("Extended DisMax Query"), '#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_FLAG, $form_state->getValue(self::EDISMAX_SEARCH_FLAG))
->set(self::EDISMAX_SEARCH_LABEL, $form_state->getValue(self::EDISMAX_SEARCH_LABEL)) ->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::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(); ->save();
parent::submitForm($form, $form_state); parent::submitForm($form, $form_state);
} }

View file

@ -12,6 +12,7 @@ use Drupal\views\Plugin\views\pager\SqlBase;
use Drupal\views\ViewExecutable; use Drupal\views\ViewExecutable;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Drupal\advanced_search\Form\SettingsForm;
/** /**
* Provides a 'AjaxViewBlock' block. * Provides a 'AjaxViewBlock' block.
@ -212,17 +213,24 @@ class SearchResultsPagerBlock extends BlockBase implements ContainerFactoryPlugi
* A renderable array representing the display links portion of pager. * A renderable array representing the display links portion of pager.
*/ */
protected function buildDisplayLinks(array $query_parameters) { protected function buildDisplayLinks(array $query_parameters) {
$active_display = $query_parameters['display'] ?? 'grid'; $config = \Drupal::config(SettingsForm::CONFIG_NAME);
$display_options = [ $display_options = [];
'list' => [
if ($config->get(SettingsForm::DISPLAY_LIST_FLAG) == 1) {
$display_options['list'] = [
'icon' => 'fa-list', 'icon' => 'fa-list',
'title' => $this->t('List'), 'title' => $this->t('List'),
], ];
'grid' => [ }
if ($config->get(SettingsForm::DISPLAY_GRID_FLAG) ==1) {
$display_options['grid'] = [
'icon' => 'fa-th', 'icon' => 'fa-th',
'title' => $this->t('Grid'), 'title' => $this->t('Grid')
], ];
]; }
$active_display = $query_parameters['display'] ?? $config->get(SettingsForm::DISPLAY_DEFAULT);
$items = []; $items = [];
foreach ($display_options as $display => $options) { foreach ($display_options as $display => $options) {
$url = Url::fromRoute('<current>', [], [ $url = Url::fromRoute('<current>', [], [