Added config for turn on/off display mode (grid and list)
This commit is contained in:
parent
64fe710adc
commit
d30c8eda8a
3 changed files with 56 additions and 10 deletions
|
|
@ -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';
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,6 +25,9 @@ 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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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('<current>', [], [
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue