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

[Block Library - Query]: Set inherit value according to detected context #32230

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/block-library/src/query/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"query": "query",
"displayLayout": "displayLayout"
},
"usesContext": [ "templateSlug" ],
"supports": {
"align": [ "wide", "full" ],
"html": false,
Expand Down
20 changes: 16 additions & 4 deletions packages/block-library/src/query/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ import QueryPlaceholder from './query-placeholder';
import { DEFAULTS_POSTS_PER_PAGE } from '../constants';

const TEMPLATE = [ [ 'core/query-loop' ] ];
export function QueryContent( { attributes, setAttributes } ) {
export function QueryContent( {
attributes,
setAttributes,
context: { templateSlug } = {},
} ) {
const {
queryId,
query,
Expand Down Expand Up @@ -83,12 +87,20 @@ export function QueryContent( { attributes, setAttributes } ) {
updateQuery( newQuery );
}
}, [ query.perPage ] );
// We need this for multi-query block pagination.
// Query parameters for each block are scoped to their ID.
/**
* We need to perform some actions during insertion of a Query block.
* 1. Set a unique `queryId` which is needed for multi-query block pagination.
* Query parameters for each block are scoped to their ID.
* 2. Detect the context in which the block is inserted and set the proper
* value to `inherit` property.
*/
useEffect( () => {
if ( ! queryId ) {
__unstableMarkNextChangeAsNotPersistent();
setAttributes( { queryId: instanceId } );
setAttributes( {
queryId: instanceId,
query: { ...query, inherit: !! templateSlug },
} );
}
}, [ queryId, instanceId ] );
const updateQuery = ( newQuery ) =>
Expand Down