Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search Block - ability to specify additional search query vars #40585

Conversation

tomasztunik
Copy link
Contributor

What?

Add ability to control search query vars of the core/search block.

Fixes #40583

Why?

This will enable creating core/search block variations that can limit their search results based on query vars provided. Ie. searching only among post_type=product for WooCommerce or searching only through a particular category of posts, or a particular author on a WordPress page.

How?

Adds query attribute to core/search block and renders hidden inputs in the search block with provided query vars and values.

Testing Instructions

  1. register new core/search block variation with query attributes

ie. to search only for pages specify post_type: 'page' if WooCommerce is available one could use post_type: 'product' - it is possible to use all public query vars and also custom taxonomies where custom taxonomy name slug is query key and value will be a slug representing custom taxonomy entry.

registerBlockVariation( 'core/search', {
   name: 'test/custom-search',
   title: 'Test Search Block',
   category: 'test',
   attributes: {
      query: {
         post_type: 'page'
      }
   }
} );
  1. Add Test Search Block with inserter
  2. Test search and confirm that post_type query parameter is included in browser address bar and that results are limited to given query.

Screenshots or screencast

@tomasztunik
Copy link
Contributor Author

tomasztunik commented Apr 25, 2022

Some questions/ideas I have here.

  • Should we allow providing raw query param names as query object keys or should we enter them as camelCase and convert them to snake_case when creating inputs? Thinking if this could possibly be problematic with custom taxonomies or some custom slug names.
  • Should we provide any defaults? we could provide null values for empty to expose suggested values for query vars and then don't render inputs if there is no value provided. Thinking if this wouldn't pollute the comment strings for block instances on pages/posts/templates.

@tomasztunik tomasztunik changed the title Add/40583 core search block query attribute Search Block ability to specify additional search query vars Apr 25, 2022
@skorasaurus skorasaurus added the [Block] Search Affects the Search Block - used to display a search field label Apr 25, 2022
@tomasztunik
Copy link
Contributor Author

Just FYI - opened PR to @types/wordpress__blocks to include variations registration API which is required to make the best out of this feature before we introduce the UI (DefinitelyTyped/DefinitelyTyped#60059)

@tomasztunik tomasztunik force-pushed the add/40583-core-search-block-query-attribute branch from 4fc1c52 to f86c249 Compare May 6, 2022 08:50
@tomasztunik tomasztunik added the [Feature] Extensibility The ability to extend blocks or the editing experience label May 6, 2022
Comment on lines 237 to 245
{ !! query &&
Object.keys( query ).map( ( key ) => (
<input
key={ key }
type="hidden"
name={ key }
value={ query[ key ] }
/>
) ) }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, @tomasztunik

Sorry if I'm missing something, but what's the reason for outputting hidden inputs in the editor?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added for consistency in the DOM between the editor/front-end but can be easily removed if we don't see a reason for them to be here.

Copy link
Member

@Mamaduka Mamaduka May 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation, @tomasztunik 🙇

The hidden inputs shouldn't affect anything visually; they also serve no purpose in the editor besides consistency. So it's probably safe to remove.

Copy link
Contributor Author

@tomasztunik tomasztunik May 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had something ringing the bell but double checked and they are completely ignored in rendering, for CSS selectors etc. Will remove them 👍

@adamziel
Copy link
Contributor

In a follow-up PR it could be worth exploring editor controls for this.

@tomasztunik
Copy link
Contributor Author

Agreed, in the issue I've shared my findings that there already is query-controls component but as mentioned there:

it's not really that generic or doesn't provide freedom as to which options we want to expose nor a simple way to provide autocomplete for custom taxonomies etc.

Having this first would enable us to create variations already and allow more time for the interface to mature at its own pace

@Mamaduka
Copy link
Member

Having this first would enable us to create variations already and allow more time for the interface to mature at its own pace.

Good point 👍

@tomasztunik tomasztunik force-pushed the add/40583-core-search-block-query-attribute branch from f86c249 to 6d8325a Compare May 18, 2022 14:53
It didn't add any value to the editor view.
Query attribute is now used only by php
to render the block on the frontend
Copy link
Member

@Mamaduka Mamaduka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me 👍 Thanks, @tomasztunik!

packages/block-library/src/search/edit.js Outdated Show resolved Hide resolved
@tomasztunik
Copy link
Contributor Author

@Mamaduka and @adamziel - thanks for the quick review 🙇

@tomasztunik tomasztunik changed the title Search Block ability to specify additional search query vars Search Block - ability to specify additional search query vars May 18, 2022
@tomasztunik tomasztunik merged commit aaa680d into WordPress:trunk May 18, 2022
@tomasztunik tomasztunik deleted the add/40583-core-search-block-query-attribute branch May 18, 2022 17:17
@mburridge
Copy link
Contributor

Added Needs Dev Note label in case this needs a dev note for 6.1 release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Search Affects the Search Block - used to display a search field [Feature] Extensibility The ability to extend blocks or the editing experience Needs Dev Note Requires a developer note for a major WordPress release cycle
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Search Block extensibility: search query vars
5 participants