Loading config/schema/simple_search_form.schema.yml +6 −0 Original line number Diff line number Diff line Loading @@ -48,3 +48,9 @@ block.settings.simple_search_form_block: input_keep_value: type: boolean label: 'Keep value in search input after form submit' preserve_url_query_parameters: type: sequence label: 'Preserve query parameters from URL' sequence: type: string label: 'URL query parameter to preserve' src/Form/SimpleSearchForm.php +24 −0 Original line number Diff line number Diff line Loading @@ -2,6 +2,7 @@ namespace Drupal\simple_search_form\Form; use Drupal\Component\Utility\UrlHelper; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Url; Loading Loading @@ -41,6 +42,29 @@ class SimpleSearchForm extends FormBase { $this->setupSearchApiAutocomplete($form, $config); } if (!empty($config['preserve_url_query_parameters'])) { $query = \Drupal::request()->query->all(); foreach ($config['preserve_url_query_parameters'] as $name) { if (!isset($form[$name]) && isset($query[$name]) && ($value = $query[$name]) !== '') { if (is_array($value)) { foreach (explode('&', UrlHelper::buildQuery($value, $name)) as $param) { list($name, $value) = explode('=', $param); $form[rawurldecode($name)] = [ '#type' => 'hidden', '#value' => rawurldecode($value), ]; } } else { $form[$name] = [ '#type' => 'hidden', '#value' => $value, ]; } } } } if ($config['submit_display']) { $form['actions'] = [ '#type' => 'container', Loading src/Plugin/Block/SimpleSearchFormBlock.php +25 −1 Original line number Diff line number Diff line Loading @@ -90,7 +90,7 @@ class SimpleSearchFormBlock extends BlockBase implements ContainerFactoryPluginI $config['submit_display'] = TRUE; $config['submit_label'] = 'Find'; $config['input_keep_value'] = FALSE; $config['preserve_url_query_parameters'] = []; return $config; } Loading Loading @@ -219,6 +219,12 @@ class SimpleSearchFormBlock extends BlockBase implements ContainerFactoryPluginI '#title' => $this->t('Keep value in search input after form submit'), '#default_value' => $this->configuration['input_keep_value'], ]; $form['preserve_url_query_parameters'] = [ '#type' => 'textfield', '#title' => $this->t('Preserve query parameters from URL'), '#description' => $this->t('A comma separated list of URL query parameters which should be preserved in the URL during the form submit'), '#default_value' => implode(', ', $this->options['preserve_url_query_parameters']), ]; return $form; } Loading @@ -240,6 +246,24 @@ class SimpleSearchFormBlock extends BlockBase implements ContainerFactoryPluginI $this->configuration['search_api_autocomplete'] = $this->moduleHandler->moduleExists('search_api_autocomplete') ? $form_state->getValue('search_api_autocomplete') : []; $query_parameters = $form_state->getValue('preserve_url_query_parameters'); $query_parameters = array_filter(array_map('trim', explode(',', $query_parameters)), function($value) { return $value !== ''; }); $this->configuration['preserve_url_query_parameters'] = $query_parameters; } /** * {@inheritdoc} */ public function getCacheContexts() { $contexts = []; if (!empty($this->options['preserve_url_query_parameters'])) { foreach ($this->options['preserve_url_query_parameters'] as $name) { $contexts[] = 'url.query_args:' . $name; } } return $contexts; } } Loading
config/schema/simple_search_form.schema.yml +6 −0 Original line number Diff line number Diff line Loading @@ -48,3 +48,9 @@ block.settings.simple_search_form_block: input_keep_value: type: boolean label: 'Keep value in search input after form submit' preserve_url_query_parameters: type: sequence label: 'Preserve query parameters from URL' sequence: type: string label: 'URL query parameter to preserve'
src/Form/SimpleSearchForm.php +24 −0 Original line number Diff line number Diff line Loading @@ -2,6 +2,7 @@ namespace Drupal\simple_search_form\Form; use Drupal\Component\Utility\UrlHelper; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Url; Loading Loading @@ -41,6 +42,29 @@ class SimpleSearchForm extends FormBase { $this->setupSearchApiAutocomplete($form, $config); } if (!empty($config['preserve_url_query_parameters'])) { $query = \Drupal::request()->query->all(); foreach ($config['preserve_url_query_parameters'] as $name) { if (!isset($form[$name]) && isset($query[$name]) && ($value = $query[$name]) !== '') { if (is_array($value)) { foreach (explode('&', UrlHelper::buildQuery($value, $name)) as $param) { list($name, $value) = explode('=', $param); $form[rawurldecode($name)] = [ '#type' => 'hidden', '#value' => rawurldecode($value), ]; } } else { $form[$name] = [ '#type' => 'hidden', '#value' => $value, ]; } } } } if ($config['submit_display']) { $form['actions'] = [ '#type' => 'container', Loading
src/Plugin/Block/SimpleSearchFormBlock.php +25 −1 Original line number Diff line number Diff line Loading @@ -90,7 +90,7 @@ class SimpleSearchFormBlock extends BlockBase implements ContainerFactoryPluginI $config['submit_display'] = TRUE; $config['submit_label'] = 'Find'; $config['input_keep_value'] = FALSE; $config['preserve_url_query_parameters'] = []; return $config; } Loading Loading @@ -219,6 +219,12 @@ class SimpleSearchFormBlock extends BlockBase implements ContainerFactoryPluginI '#title' => $this->t('Keep value in search input after form submit'), '#default_value' => $this->configuration['input_keep_value'], ]; $form['preserve_url_query_parameters'] = [ '#type' => 'textfield', '#title' => $this->t('Preserve query parameters from URL'), '#description' => $this->t('A comma separated list of URL query parameters which should be preserved in the URL during the form submit'), '#default_value' => implode(', ', $this->options['preserve_url_query_parameters']), ]; return $form; } Loading @@ -240,6 +246,24 @@ class SimpleSearchFormBlock extends BlockBase implements ContainerFactoryPluginI $this->configuration['search_api_autocomplete'] = $this->moduleHandler->moduleExists('search_api_autocomplete') ? $form_state->getValue('search_api_autocomplete') : []; $query_parameters = $form_state->getValue('preserve_url_query_parameters'); $query_parameters = array_filter(array_map('trim', explode(',', $query_parameters)), function($value) { return $value !== ''; }); $this->configuration['preserve_url_query_parameters'] = $query_parameters; } /** * {@inheritdoc} */ public function getCacheContexts() { $contexts = []; if (!empty($this->options['preserve_url_query_parameters'])) { foreach ($this->options['preserve_url_query_parameters'] as $name) { $contexts[] = 'url.query_args:' . $name; } } return $contexts; } }