0

This is probably much simpler than I'm imagining.

Client wants to display the user-entered search query string, with a label (like Search Query or You Searched For), at the top of the search results page.

Currently, the search query is displayed in the form input field (a Views exposed form, in a block) but it has <input size="25">, so many queries are too wide to display.

I've been looking at the fields available in Views. Some contain the query data or are based upon it, such as "Search: Solr score debugging (indexed field)" and "Search: Excerpt" (with "Use highlighted field data" checked). But I have not been able to find the straight query string, unaltered. Nor does it appear that this is a field I can add to the Default Solr Index.

This question is a bit old (Drupal 7) but it does make it seem like this is going to be way more trouble than it should be, especially considering that this string is already being rendered on the Search Results page (just not where the client wants to see it).

This question is an apparent duplicate, but it is unanswered. And this question from 2011 concerns Drupal 6 and is old enough that it's likely irrelevant.

I don't think these software versions will make much of a difference, but just in case:

Solr 8.8.2
Drupal 9.3.3
Search API Solr 4.2.6

Update (January 25, 2022)

I edited the View for "Search (Index Default Solr content index)" and added a Header of type Global: Text area.

In the configuration for this Header, I made sure to check the box for "Use replacement tokens from the first row".

In order to access the user-entered value for the Fulltext search input, I set the Header Content to the following Basic HTML:

<p>
  <strong>You searched for:</strong>
  {{ raw_arguments.search_api_fulltext }}
</p>

Here's a screenshot of the Views config: Token replacement for Fulltext search input

This is literally all the client asked for, just about as simple as can be.

Unfortunately, it only works in the Views preview with contextual filters. I can't get the string to render on the actual Search Results page.

Here's a screenshot of a query string in the Preview with contextual filters:

Preview of Search result for "coding standard"

And here's a detail showing the way this is actually rendered:

You searched for: Nothing, apparently.

Note that the user-entered string is still visible in the Fulltext search input field, but not in the Header.

I do note that the actual search results page is at the path:

/search?search_api_fulltext="coding+standard"

In the Views preview, however, the path is shown as:

/search/%22coding%20standard%22

If I manually enter that path instead of the one obtained by submitting the Fulltext search, the header shows the expected results, but the form input field is not used at all!

Search string passed as contextual filter instead of query parameter -- this works!

What am I missing here? I feel certain that I am very close.

1
  • drupal.org/project/drupal/issues/2761273 then you can use it in a custom text area? But it needs to be tested if it does with solr, but I think it does as I've found originally on that topic. But maybe a custom global plugin may be better if you don't want to display everything always. Commented Dec 14, 2021 at 23:31

1 Answer 1

1

Search API Fulltext passes the value as a query parameter, with the URL path:

/search?search_api_fulltext="coding+standard"

Using the View Contextual Filter instead, however, the path uses URL segments instead of query parameters:

/search/%22coding%20standard%22

With the path parameter, I could use the Token to provide the value for "You searched for:" but the "Search documentation ..." text input field could not access the value.

With the query string, the "Search documentation ..." text input field could access the value, but the Token couldn't.

The solution, obvious in hindsight, was to configure Views so that when the filter value is not in the URL, the contextual filter falls back on the default value provided by the query parameter search_api_fulltext:

When the filter value is NOT in the URL, use default value provided by query parameter search_api_fulltext

With this fallback in place, the Token value is now available in the header, and the "Search documentation ..." text input field still contains the previous search string as a default value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.