Problem/Motivation
On #2664830: Add search capability to help topics, we found we needed to make a block that would search help. The proposed help search is using the core Search module to index/search keywords in the help topics. However, the core Search module's SearchBlock only allows searching the "default" search page, which means that it cannot be relied upon to search Help.
This is currently blocking us from completing that issue, which is part of the required Roadmap to Beta for the experimental Help Topics module #3027054: Help Topics module roadmap: the path to beta and stable.
Proposed resolution
One of the following:
a) Add configuration to the current SearchBlock plugin that would allow it to search any defined SearchPage config entity in the system (instead of only searching the default SearchPage as it currently does).
b) Define a new Block plugin that could be configured to search any defined SearchPage.
c) Do something like what Menu does -- automatically add a Block for each defined SearchPage in the system.
Remaining tasks
1. Figure out which alternative makes the most sense. a
2. If we choose option (b) or (c), would it be too confusing to leave the existing SearchBlock plugin around too? At least it would probably need a name change to avoid confusion?
3. Implement and add upgrade test.
User interface changes
Site admins will be able to place blocks that will search any defined search page, not just the default search page.

API changes
No
Data model changes
the existing SearchBlock plugin blocks getting settings for default search page
- new configuration setting with schema to be added to it.
Release notes snippet
Search blocks can configure target search page
| Comment | File | Size | Author |
|---|---|---|---|
| #32 | interdiff.txt | 902 bytes | amber himes matz |
| #32 | 3067943-search-block-32.patch | 12.79 KB | amber himes matz |
| #31 | interdiff.txt | 848 bytes | jhodgdon |
| #31 | 3067943-search-block-31.patch | 12.73 KB | jhodgdon |
| #30 | interdiff.txt | 1.23 KB | andypost |
Comments
Comment #2
andypostInteresting, checked how custom projects implement search block (most using search api)...
80% just configure route to submit search to
Makes sense contrib modules too
From BC POV block could add default configuration pointing to default search and allow to configure it
OTOH it could use derivatives and just deprecate old block plugin
Comment #3
jhodgdonYeah, it's easy enough on your own site to configure the URL and configure the default search (probably most sites just have one type of search anyway and can just use the default block, if they are using the Core search module). But here we really want to have a separate search for Help, and it's probably not the default search for the site (content search is usually), so we're kind of stuck. And as a core module, we don't have any control over whether some admin changes the URL on the Help search, or even whether it exists...
Comment #4
pwolanin commentedI think option B makes sense in part becuase it's generically useful. I don't think adding a block for each is necessarily more helpful.
Actually it seems as though B and A are roughly 2 flavors of the same thing
Comment #5
jhodgdonYes, I guess the question would be whether to add configuration to the existing block class, or make a new one. Probably adding to the original one would make more sense, if we can make it backwards compatible without too much trouble.
Thanks for responding!
Comment #6
jhodgdonI am going to work on this today.
Comment #7
jhodgdonThis patch works for me. It also passes the SearchBlock test, with added test to verify you can set the page to a different value and search users instead of nodes... at least locally.
I did add a line to the Standard profile's search block config so it should be compliant with the new setting. I believe that we don't need to update config on existing sites though, because the default (if that setting is empty) is still to use the default search page.
Anyway, let's see what the test bot thinks, and any comments from pwolanin, andypost, or whoever?
Comment #9
andypostInitial "sleepy" review, main points are BC (does it need change record) and update hook
NW for default value
setting supposed to be a string (so default should be '')
isset() not needed because there's defaults
I think $id argument could be replaced with $entity_id and fallback to default if null passed
Not sure how to deal with BC - probably it's safe to just add container injection
but what if contrib or custom already do that in child classes?
means it needs post update hook and upgrade test
Comment #10
jhodgdonThanks for the review!
I agree with the first two items.
Regarding item 3, I think you are correct and we cannot modify SearchBlock in this way, because __construct() is part of the published API. So, I guess the only possible thing is to make a new plugin class and deprecate the old one? Or have 2 types of blocks: one that goes to the currently-defined search page, and the other that is configurable to go to a particular block page.
Or in the terminology of the issue summary, option A won't work because it would introduce a change to the existing API for hypothetical contrib/custom modules that extend SearchBlock. So, option B will have to be done instead: create a new class instead of using the old one.
I'll make a new patch.
Comment #11
jhodgdonMeanwhile here is an interdiff that would fix the first 2 items in the previous patch. I'll use that patch and this interdiff as the basis for the new class.
Comment #12
andypostConstructor is not API according to https://www.drupal.org/core/d8-bc-policy#constructors
But not sure about for this case because nothing in contrib extends it http://grep.xnddx.ru/search?text=SearchBlock
Example upgrade is like https://git.drupalcode.org/project/drupal/commit/281c6f0
Comment #13
jhodgdonOK good, and @alexpott said it is OK in Slack. We can continue here then... Not sure I want to work on the update hook though?
Comment #14
andypostWorking on upgrade path
Comment #15
andypostAdded upgrade path & a bit clean-up, will work on tests soon
Comment #16
andypostComment #17
andypostUpgrade test added, now needs CR
Comment #19
andypostAnd form builder should be injected one constructor changed
Comment #20
andypostAttempt to fix tests
1) block module could be disabled
2) expected - no more cache tag on front
explicit default to content removes cache tag from front page!
Comment #23
jhodgdonIt looks like the tests pass now, and there are no coding standards errors.
I took a look at the changes you have made since my last patch. I have some concerns:
a)
This looks wrong. If the search block is set to use the "Default" search, then the page should still have a dependency on the search settings config, because that is where the default search is set. So if you had to take those out to get the tests to pass, I think our code has a bug in it.
b) I am also confused about this:
I think it should say
That would be the default.
c) Also in core/profiles/demo_umami/config/install/block.block.umami_search.yml and core/profiles/standard/config/install/block.block.bartik_search.yml we should set it to '' not node_search, I think.
Comment #24
andypostI think we should default to node search in profiles because this explicitly tells what our profiles expect.
That's about default but upgrade defaults to empty because existing sites may have other search configured as default and it was only way to change where block points.
Also one less cache tag means no reason to load config for default searcher because blocks pre configured - and I think CR should suggest to configure searcher in block for performance reasons (-1 config cache read on every page)
Comment #25
jhodgdonOK, that makes sense actually.
We do still need to make sure we are testing that the cache tag appears if you are using the "default" option though. Let's see. Yes, it appears it is still being tested in SearchPageCacheTagsTest. So, we are good!
I am +1 for RTBC. Thanks for taking care of the upgrade path and tests for it! I've created a change record:
https://www.drupal.org/node/3070036
Comment #26
jhodgdonAdding blocker tag on this, as it is blocking progress for making Help Topics get to Beta/Release.
Comment #27
pwolanin commentedIn building the options in blockForm() the new description text says "Leave blank to use your default search page.", but seems as though the user would actually need to select the "Default" option? Should that be more distinctly not a search name like "- Default -" ?
Minor, but I think the code would be clearer if the variable and config keys indicate we are using a search page ID, rather than the etity. e.g.:
+ $page = $this->configuration['search_page'] ?? NULL;Would be much clearer as:
+ $entity_id = $this->configuration['search_page_id'] ?? NULL;Since the param to the form function is
$entity_idComment #28
andypostMakes sense to remove "search" prefix as config always bound to search block, should address #28.2
About #28.1 - I see no usages of "- Default -" in core, maybe makes sense to extend description pointing that current default is
So when block is placed end-user will know what is default?
Comment #30
andypostFixed last place and clean-up
Comment #31
jhodgdonLatest patch looks good to me.
Regarding the first part of comment #27... This is in regard to these added lines in core/modules/search/src/Plugin/Block/SearchBlock.php:
I think it is still confusing. Propose changing the description... here's a patch/interdiff.
Comment #32
amber himes matzLooks like all comments have been either integrated or addressed in #31. The only thing I found was a typo ("cacheablity" -> "cacheability") in a comment block. New patch and interdiff attached.
Comment #33
andypostLet's get commiter's attention
Comment #34
pwolanin commented+1 looks good
Comment #35
larowlanCommitted ce189a3 and pushed to 8.8.x. Thanks!
Published the change record