From 8779ca7723a5f98257b77944966741f3931f1078 Mon Sep 17 00:00:00 2001 From: Jeff He Date: Tue, 26 Sep 2023 15:40:50 -0400 Subject: [PATCH 1/6] added display settings form to result page --- src/Form/AdvancedSearchForm.php | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/Form/AdvancedSearchForm.php b/src/Form/AdvancedSearchForm.php index 1dc20c0..95691e5 100644 --- a/src/Form/AdvancedSearchForm.php +++ b/src/Form/AdvancedSearchForm.php @@ -427,6 +427,37 @@ public function buildForm(array $form, FormStateInterface $form_state, View $vie 'class' => [$block_class_prefix . '__search'], ], ]; + + $form['display-mode'] = [ + '#type' => 'fieldset', + '#title' => $this->t("Pager Block"), + ]; + + $form['display-mode'][SettingsForm::DISPLAY_LIST_FLAG] = [ + '#type' => 'checkbox', + '#title' => $this + ->t('Expose "List view" option.'), + '#default_value' => self::getConfig(SettingsForm::DISPLAY_LIST_FLAG, 0), + ]; + + $form['display-mode'][SettingsForm::DISPLAY_GRID_FLAG] = [ + '#type' => 'checkbox', + '#title' => $this + ->t('Expose "Grid view" option.'), + '#default_value' => self::getConfig(SettingsForm::DISPLAY_GRID_FLAG, 0), + ]; + + $form['display-mode'][SettingsForm::DISPLAY_DEFAULT] = [ + '#type' => 'select', + '#title' => $this + ->t('Default view mode:'), + '#options' => [ + 'list' => 'List', + 'grid' => 'Grid', + ], + '#default_value' => self::getConfig(SettingsForm::DISPLAY_DEFAULT, 'grid'), + ]; + return $form; } From 609616caa1fffed0b9d9c8d88b1e0aed228f041f Mon Sep 17 00:00:00 2001 From: Jeff He Date: Thu, 28 Sep 2023 15:24:28 -0400 Subject: [PATCH 2/6] moved form to correct location, added defaults --- src/Form/AdvancedSearchForm.php | 33 +----------- src/Plugin/Block/SearchResultsPagerBlock.php | 57 ++++++++++++++++++++ 2 files changed, 58 insertions(+), 32 deletions(-) diff --git a/src/Form/AdvancedSearchForm.php b/src/Form/AdvancedSearchForm.php index 95691e5..c2c153e 100644 --- a/src/Form/AdvancedSearchForm.php +++ b/src/Form/AdvancedSearchForm.php @@ -427,37 +427,6 @@ public function buildForm(array $form, FormStateInterface $form_state, View $vie 'class' => [$block_class_prefix . '__search'], ], ]; - - $form['display-mode'] = [ - '#type' => 'fieldset', - '#title' => $this->t("Pager Block"), - ]; - - $form['display-mode'][SettingsForm::DISPLAY_LIST_FLAG] = [ - '#type' => 'checkbox', - '#title' => $this - ->t('Expose "List view" option.'), - '#default_value' => self::getConfig(SettingsForm::DISPLAY_LIST_FLAG, 0), - ]; - - $form['display-mode'][SettingsForm::DISPLAY_GRID_FLAG] = [ - '#type' => 'checkbox', - '#title' => $this - ->t('Expose "Grid view" option.'), - '#default_value' => self::getConfig(SettingsForm::DISPLAY_GRID_FLAG, 0), - ]; - - $form['display-mode'][SettingsForm::DISPLAY_DEFAULT] = [ - '#type' => 'select', - '#title' => $this - ->t('Default view mode:'), - '#options' => [ - 'list' => 'List', - 'grid' => 'Grid', - ], - '#default_value' => self::getConfig(SettingsForm::DISPLAY_DEFAULT, 'grid'), - ]; - return $form; } @@ -499,4 +468,4 @@ public function submitForm(array &$form, FormStateInterface $form_state) { } } -} +} \ No newline at end of file diff --git a/src/Plugin/Block/SearchResultsPagerBlock.php b/src/Plugin/Block/SearchResultsPagerBlock.php index 687822d..800fb2d 100644 --- a/src/Plugin/Block/SearchResultsPagerBlock.php +++ b/src/Plugin/Block/SearchResultsPagerBlock.php @@ -3,6 +3,7 @@ namespace Drupal\advanced_search\Plugin\Block; use Drupal\Core\Block\BlockBase; +use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Render\Markup; use Drupal\Core\Url; @@ -316,6 +317,62 @@ protected function buildSortByForm(array $sort_criteria, array $query_parameters ]; } + /** + * {@inheritdoc} + */ + public function defaultConfiguration() { + $config = \Drupal::config(SettingsForm::CONFIG_NAME); + return [ + 'display-mode', + SettingsForm::DISPLAY_LIST_FLAG => $config->get(SettingsForm::DISPLAY_LIST_FLAG), + SettingsForm::DISPLAY_GRID_FLAG => $config->get(SettingsForm::DISPLAY_GRID_FLAG), + SettingsForm::DISPLAY_DEFAULT => $config->get(SettingsForm::DISPLAY_DEFAULT), + ]; + } + + /** + * {@inheritdoc} + */ + public function blockForm($form, FormStateInterface $form_state) { + $form['display-mode'] = [ + '#type' => 'fieldset', + '#title' => $this->t("Pager Block"), + ]; + + $form['display-mode'][SettingsForm::DISPLAY_LIST_FLAG] = [ + '#type' => 'checkbox', + '#title' => $this + ->t('Expose "List view" option.'), + '#default_value' => $this->configuration[SettingsForm::DISPLAY_LIST_FLAG], + ]; + + $form['display-mode'][SettingsForm::DISPLAY_GRID_FLAG] = [ + '#type' => 'checkbox', + '#title' => $this + ->t('Expose "Grid view" option.'), + '#default_value' => $this->configuration[SettingsForm::DISPLAY_GRID_FLAG], + ]; + + $form['display-mode'][SettingsForm::DISPLAY_DEFAULT] = [ + '#type' => 'select', + '#title' => $this + ->t('Default view mode:'), + '#options' => [ + 'list' => 'List', + 'grid' => 'Grid', + ], + '#default_value' => $this->configuration[SettingsForm::DISPLAY_DEFAULT], + ]; + + return $form; + } + + /** + * {@inheritdoc} + */ + public function blockSubmit($form, FormStateInterface $form_state) { + } + /** * {@inheritdoc} */ From a2a46cf5583b940226581ddbcab74e721470c441 Mon Sep 17 00:00:00 2001 From: Jeff He Date: Fri, 29 Sep 2023 11:17:32 -0400 Subject: [PATCH 3/6] made settings save on submit --- src/Plugin/Block/SearchResultsPagerBlock.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/Plugin/Block/SearchResultsPagerBlock.php b/src/Plugin/Block/SearchResultsPagerBlock.php index 800fb2d..3c21da0 100644 --- a/src/Plugin/Block/SearchResultsPagerBlock.php +++ b/src/Plugin/Block/SearchResultsPagerBlock.php @@ -324,9 +324,6 @@ public function defaultConfiguration() { $config = \Drupal::config(SettingsForm::CONFIG_NAME); return [ 'display-mode', - SettingsForm::DISPLAY_LIST_FLAG => $config->get(SettingsForm::DISPLAY_LIST_FLAG), - SettingsForm::DISPLAY_GRID_FLAG => $config->get(SettingsForm::DISPLAY_GRID_FLAG), - SettingsForm::DISPLAY_DEFAULT => $config->get(SettingsForm::DISPLAY_DEFAULT), ]; } @@ -334,6 +331,7 @@ public function defaultConfiguration() { * {@inheritdoc} */ public function blockForm($form, FormStateInterface $form_state) { + $config = \Drupal::config(SettingsForm::CONFIG_NAME); $form['display-mode'] = [ '#type' => 'fieldset', '#title' => $this->t("Pager Block"), @@ -343,14 +341,14 @@ public function blockForm($form, FormStateInterface $form_state) { '#type' => 'checkbox', '#title' => $this ->t('Expose "List view" option.'), - '#default_value' => $this->configuration[SettingsForm::DISPLAY_LIST_FLAG], + '#default_value' => $config->get(SettingsForm::DISPLAY_LIST_FLAG), ]; $form['display-mode'][SettingsForm::DISPLAY_GRID_FLAG] = [ '#type' => 'checkbox', '#title' => $this ->t('Expose "Grid view" option.'), - '#default_value' => $this->configuration[SettingsForm::DISPLAY_GRID_FLAG], + '#default_value' => $config->get(SettingsForm::DISPLAY_GRID_FLAG), ]; $form['display-mode'][SettingsForm::DISPLAY_DEFAULT] = [ @@ -361,7 +359,7 @@ public function blockForm($form, FormStateInterface $form_state) { 'list' => 'List', 'grid' => 'Grid', ], - '#default_value' => $this->configuration[SettingsForm::DISPLAY_DEFAULT], + '#default_value' => $config->get(SettingsForm::DISPLAY_DEFAULT), ]; return $form; @@ -371,6 +369,11 @@ public function blockForm($form, FormStateInterface $form_state) { * {@inheritdoc} */ public function blockSubmit($form, FormStateInterface $form_state) { + $config = \Drupal::service('config.factory')->getEditable(SettingsForm::CONFIG_NAME); + $config->set(SettingsForm::DISPLAY_LIST_FLAG, $form_state->getValues()['display-mode'][SettingsForm::DISPLAY_LIST_FLAG]); + $config->set(SettingsForm::DISPLAY_GRID_FLAG, $form_state->getValues()['display-mode'][SettingsForm::DISPLAY_GRID_FLAG]); + $config->set(SettingsForm::DISPLAY_DEFAULT, $form_state->getValues()['display-mode'][SettingsForm::DISPLAY_DEFAULT]); + $config->save(); } /** From d2023d132d52d6d4515120d18bc7cfa4e4f94cdb Mon Sep 17 00:00:00 2001 From: Jeff He Date: Fri, 29 Sep 2023 11:22:51 -0400 Subject: [PATCH 4/6] removed unused function --- src/Plugin/Block/SearchResultsPagerBlock.php | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/Plugin/Block/SearchResultsPagerBlock.php b/src/Plugin/Block/SearchResultsPagerBlock.php index 3c21da0..dad2ddf 100644 --- a/src/Plugin/Block/SearchResultsPagerBlock.php +++ b/src/Plugin/Block/SearchResultsPagerBlock.php @@ -317,16 +317,6 @@ protected function buildSortByForm(array $sort_criteria, array $query_parameters ]; } - /** - * {@inheritdoc} - */ - public function defaultConfiguration() { - $config = \Drupal::config(SettingsForm::CONFIG_NAME); - return [ - 'display-mode', - ]; - } - /** * {@inheritdoc} */ From 646f8b1e050787460034dd6e00a9755be5e57e60 Mon Sep 17 00:00:00 2001 From: Jeff He Date: Fri, 29 Sep 2023 16:24:12 -0400 Subject: [PATCH 5/6] began modifying to use block settings --- src/Plugin/Block/SearchResultsPagerBlock.php | 35 +++++++++++++------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/src/Plugin/Block/SearchResultsPagerBlock.php b/src/Plugin/Block/SearchResultsPagerBlock.php index dad2ddf..5f7b80e 100644 --- a/src/Plugin/Block/SearchResultsPagerBlock.php +++ b/src/Plugin/Block/SearchResultsPagerBlock.php @@ -106,6 +106,7 @@ public function build() { 'pager' => array_merge($pager->render($exposed_input), ['#wrapper_attributes' => ['class' => ['container']]]), ], ]; + dpm($this->configuration); return $build; } @@ -220,21 +221,21 @@ protected function buildDisplayLinks(array $query_parameters) { $config = \Drupal::config(SettingsForm::CONFIG_NAME); $display_options = []; - if ($config->get(SettingsForm::DISPLAY_LIST_FLAG) == 1) { + if ($this->configuration[SettingsForm::DISPLAY_LIST_FLAG] == 1) { $display_options['list'] = [ 'icon' => 'fa-list', 'title' => $this->t('List'), ]; } - if ($config->get(SettingsForm::DISPLAY_GRID_FLAG) == 1) { + if ($this->configuration[SettingsForm::DISPLAY_GRID_FLAG] == 1) { $display_options['grid'] = [ 'icon' => 'fa-th', 'title' => $this->t('Grid'), ]; } - $active_display = $query_parameters['display'] ?? $config->get(SettingsForm::DISPLAY_DEFAULT); + $active_display = $query_parameters['display'] ?? $this->configuration[SettingsForm::DISPLAY_DEFAULT]; $items = []; foreach ($display_options as $display => $options) { $url = Url::fromRoute('', [], [ @@ -317,28 +318,39 @@ protected function buildSortByForm(array $sort_criteria, array $query_parameters ]; } + /** + * {@inheritdoc} + */ + public function defaultConfiguration() { + $config = \Drupal::service('config.factory')->getEditable(SettingsForm::CONFIG_NAME); + return [ + SettingsForm::DISPLAY_LIST_FLAG => $config->get(SettingsForm::DISPLAY_LIST_FLAG), + SettingsForm::DISPLAY_GRID_FLAG => $config->get(SettingsForm::DISPLAY_GRID_FLAG), + SettingsForm::DISPLAY_DEFAULT => $config->get(SettingsForm::DISPLAY_DEFAULT), + ]; + } + /** * {@inheritdoc} */ public function blockForm($form, FormStateInterface $form_state) { - $config = \Drupal::config(SettingsForm::CONFIG_NAME); $form['display-mode'] = [ '#type' => 'fieldset', '#title' => $this->t("Pager Block"), + '#default_value' => $this->configuration[SettingsForm::DISPLAY_LIST_FLAG], ]; $form['display-mode'][SettingsForm::DISPLAY_LIST_FLAG] = [ '#type' => 'checkbox', '#title' => $this ->t('Expose "List view" option.'), - '#default_value' => $config->get(SettingsForm::DISPLAY_LIST_FLAG), + '#default_value' => $this->configuration[SettingsForm::DISPLAY_GRID_FLAG], ]; $form['display-mode'][SettingsForm::DISPLAY_GRID_FLAG] = [ '#type' => 'checkbox', '#title' => $this ->t('Expose "Grid view" option.'), - '#default_value' => $config->get(SettingsForm::DISPLAY_GRID_FLAG), ]; $form['display-mode'][SettingsForm::DISPLAY_DEFAULT] = [ @@ -349,7 +361,7 @@ public function blockForm($form, FormStateInterface $form_state) { 'list' => 'List', 'grid' => 'Grid', ], - '#default_value' => $config->get(SettingsForm::DISPLAY_DEFAULT), + '#default_value' => $this->configuration[SettingsForm::DISPLAY_DEFAULT], ]; return $form; @@ -359,11 +371,10 @@ public function blockForm($form, FormStateInterface $form_state) { * {@inheritdoc} */ public function blockSubmit($form, FormStateInterface $form_state) { - $config = \Drupal::service('config.factory')->getEditable(SettingsForm::CONFIG_NAME); - $config->set(SettingsForm::DISPLAY_LIST_FLAG, $form_state->getValues()['display-mode'][SettingsForm::DISPLAY_LIST_FLAG]); - $config->set(SettingsForm::DISPLAY_GRID_FLAG, $form_state->getValues()['display-mode'][SettingsForm::DISPLAY_GRID_FLAG]); - $config->set(SettingsForm::DISPLAY_DEFAULT, $form_state->getValues()['display-mode'][SettingsForm::DISPLAY_DEFAULT]); - $config->save(); + $values = $form_state->getValues(); + $this->configuration[SettingsForm::DISPLAY_LIST_FLAG] = $values['display-mode'][SettingsForm::DISPLAY_LIST_FLAG]; + $this->configuration[SettingsForm::DISPLAY_GRID_FLAG] = $values['display-mode'][SettingsForm::DISPLAY_GRID_FLAG]; + $this->configuration[SettingsForm::DISPLAY_DEFAULT] = $values['display-mode'][SettingsForm::DISPLAY_DEFAULT]; } /** From 27e7d0bfec18ad0f0bbf0fa5798ea072d1fce36d Mon Sep 17 00:00:00 2001 From: Jeff He Date: Mon, 2 Oct 2023 11:13:22 -0400 Subject: [PATCH 6/6] finished with modifications --- src/Plugin/Block/SearchResultsPagerBlock.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Plugin/Block/SearchResultsPagerBlock.php b/src/Plugin/Block/SearchResultsPagerBlock.php index 5f7b80e..b15f8a9 100644 --- a/src/Plugin/Block/SearchResultsPagerBlock.php +++ b/src/Plugin/Block/SearchResultsPagerBlock.php @@ -106,7 +106,6 @@ public function build() { 'pager' => array_merge($pager->render($exposed_input), ['#wrapper_attributes' => ['class' => ['container']]]), ], ]; - dpm($this->configuration); return $build; } @@ -337,20 +336,20 @@ public function blockForm($form, FormStateInterface $form_state) { $form['display-mode'] = [ '#type' => 'fieldset', '#title' => $this->t("Pager Block"), - '#default_value' => $this->configuration[SettingsForm::DISPLAY_LIST_FLAG], ]; $form['display-mode'][SettingsForm::DISPLAY_LIST_FLAG] = [ '#type' => 'checkbox', '#title' => $this ->t('Expose "List view" option.'), - '#default_value' => $this->configuration[SettingsForm::DISPLAY_GRID_FLAG], + '#default_value' => $this->configuration[SettingsForm::DISPLAY_LIST_FLAG], ]; $form['display-mode'][SettingsForm::DISPLAY_GRID_FLAG] = [ '#type' => 'checkbox', '#title' => $this ->t('Expose "Grid view" option.'), + '#default_value' => $this->configuration[SettingsForm::DISPLAY_GRID_FLAG], ]; $form['display-mode'][SettingsForm::DISPLAY_DEFAULT] = [