diff --git a/config/schema/search_api_autocomplete.schema.yml b/config/schema/search_api_autocomplete.schema.yml index e07675010838352646a87d87103db0726d4753dc..71aa9984c54ddcaac4b5d3af608a0adb4eeb1e53 100644 --- a/config/schema/search_api_autocomplete.schema.yml +++ b/config/schema/search_api_autocomplete.schema.yml @@ -53,6 +53,15 @@ search_api_autocomplete.search.*: min_length: type: integer label: 'The minimum length of user input to trigger autocomplete' + view_all_results_enable: + type: boolean + label: 'Whether to show a "View all results" link' + view_all_results_text: + type: string + label: 'The label for the "View all results" link' + view_all_results_position: + type: string + label: 'The position of the "View all results" link' show_count: type: boolean label: 'Whether to show a predicted result count for suggestions' diff --git a/src/Controller/AutocompleteController.php b/src/Controller/AutocompleteController.php index d2bd4a2f4cf881663329169b8208f4025f800a70..a0c390932c1346c39501f4f3cdae463d4036ce47 100644 --- a/src/Controller/AutocompleteController.php +++ b/src/Controller/AutocompleteController.php @@ -2,13 +2,16 @@ namespace Drupal\search_api_autocomplete\Controller; +use Drupal\Component\Render\FormattableMarkup; use Drupal\Component\Utility\DeprecationHelper; +use Drupal\Component\Utility\Html; use Drupal\Core\Controller\ControllerBase; use Drupal\Core\DependencyInjection\ContainerInjectionInterface; use Drupal\Core\Render\RendererInterface; use Drupal\Core\Utility\Error; use Drupal\search_api_autocomplete\SearchApiAutocompleteException; use Drupal\search_api_autocomplete\SearchInterface; +use Drupal\search_api_autocomplete\Suggestion\Suggestion; use Drupal\search_api_autocomplete\Utility\AutocompleteHelperInterface; use Psr\Log\LoggerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -144,6 +147,25 @@ class AutocompleteController extends ControllerBase implements ContainerInjectio $this->moduleHandler() ->alter('search_api_autocomplete_suggestions', $suggestions, $alter_params); + // Add a "View all results" suggestion, if enabled. + $show_results_link = $search->getOption('view_all_results_enable'); + if ($show_results_link) { + $show_results_text = $search->getOption('view_all_results_text'); + $show_results_text = Html::escape($show_results_text); + $text = new FormattableMarkup($show_results_text, ['@keys' => $keys]); + $suggestion = new Suggestion($keys, NULL, NULL, $text); + + $show_results_link_position = $search->getOption('view_all_results_position'); + // Add the 'Show all' link before suggestions. + if ($show_results_link_position === 'before') { + $suggestions = array_merge([$suggestion], $suggestions); + } + // Add the 'Show all' link after suggestions. + elseif ($show_results_link_position === 'after') { + $suggestions[] = $suggestion; + } + } + // Then, add the suggestions to the $matches return array in the form // expected by Drupal's autocomplete Javascript. $show_count = $search->getOption('show_count'); diff --git a/src/Entity/Search.php b/src/Entity/Search.php index c4559609aa98f7aa0d3fe4cda487110bb13ec7ed..b3787b95eeb9223f8c86c64a2911840149f9f143 100644 --- a/src/Entity/Search.php +++ b/src/Entity/Search.php @@ -175,6 +175,9 @@ class Search extends ConfigEntityBase implements SearchInterface { 'limit' => 10, 'min_length' => 1, 'submit_button_selector' => ':submit', + 'view_all_results_enable' => FALSE, + 'view_all_results_text' => 'View all results for "@keys"', + 'view_all_results_position' => 'before', ]; } diff --git a/src/Form/SearchEditForm.php b/src/Form/SearchEditForm.php index 582283fbf35303d0cbdcaf9696cbfc2f123c9b98..f669a870c8a40b04e02b2b409fc86433dcd5695e 100644 --- a/src/Form/SearchEditForm.php +++ b/src/Form/SearchEditForm.php @@ -143,6 +143,42 @@ class SearchEditForm extends EntityForm { '#default_value' => (bool) $search->getOption('show_count'), ]; + $form['options']['view_all_results_enable'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Show "View all results" link'), + '#description' => $this->t('When enabled, a link to the search page will be displayed after/before the autocomplete suggestions.'), + '#default_value' => $search->getOption('view_all_results_enable'), + ]; + $form['options']['view_all_results'] = [ + '#type' => 'fieldset', + '#title' => $this->t('"View all results" settings'), + '#collapsible' => TRUE, + '#collapsed' => TRUE, + '#states' => [ + 'visible' => [ + ':input[name="options[view_all_results_enable]"]' => ['checked' => TRUE], + ], + ], + ]; + $form['options']['view_all_results']['view_all_results_text'] = [ + '#type' => 'textfield', + '#title' => $this->t('Link text'), + '#description' => $this->t('The text to display for the "View all results" link. Use "@keys" as a placeholder for the entered search keywords.'), + '#default_value' => $search->getOption('view_all_results_text'), + '#parents' => ['options', 'view_all_results_text'], + ]; + $form['options']['view_all_results']['view_all_results_position'] = [ + '#type' => 'select', + '#title' => $this->t('Link position'), + '#description' => $this->t('Select the position for the "View all results" link.'), + '#options' => [ + 'before' => $this->t('Before suggestions'), + 'after' => $this->t('After suggestions'), + ], + '#default_value' => $search->getOption('view_all_results_position'), + '#parents' => ['options', 'view_all_results_position'], + ]; + $form['advanced'] = [ '#type' => 'fieldset', '#title' => $this->t('Advanced settings'),