Skip to content

Commit

Permalink
Get postType from context
Browse files Browse the repository at this point in the history
  • Loading branch information
mikachan committed Oct 30, 2024
1 parent bd93e9e commit aec7cba
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
3 changes: 2 additions & 1 deletion packages/block-library/src/post-template/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"displayLayout",
"templateSlug",
"previewPostType",
"enhancedPagination"
"enhancedPagination",
"postType"
],
"supports": {
"reusable": false,
Expand Down
6 changes: 5 additions & 1 deletion packages/block-library/src/post-template/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export default function PostTemplateEdit( {
} = {},
templateSlug,
previewPostType,
postType: postTypeFromContext,
},
attributes: { layout },
__unstableLayoutClassNames,
Expand Down Expand Up @@ -186,7 +187,10 @@ export default function PostTemplateEdit( {
}
// When we preview Query Loop blocks we should prefer the current
// block's postType, which is passed through block context.
const usedPostType = previewPostType || postType;
const usedPostType =
postTypeFromContext !== 'page'
? postTypeFromContext
: previewPostType || postType;
return {
posts: getEntityRecords( 'postType', usedPostType, {
...query,
Expand Down
20 changes: 18 additions & 2 deletions packages/block-library/src/query/edit/inspector-controls/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,15 @@ import { useToolsPanelDropdownMenuProps } from '../../../utils/hooks';
const { BlockInfo } = unlock( blockEditorPrivateApis );

export default function QueryInspectorControls( props ) {
const { attributes, setQuery, setDisplayLayout, isSingular } = props;
const { query, displayLayout } = attributes;
const {
attributes,
setQuery,
setDisplayLayout,
postTypeFromContext,
isSingular,
} = props;
const { query, displayLayout } = attributes;
let {
order,
orderBy,
author: authorIds,
Expand All @@ -61,6 +67,16 @@ export default function QueryInspectorControls( props ) {
parents,
format,
} = query;
// If a post type is set in context, update `postType` to match it,
// unless the post type is `page`, as it usually doesn't make sense to loop
// through pages.
if (
postTypeFromContext &&
postTypeFromContext !== 'page' &&
postTypeFromContext !== postType
) {
postType = postTypeFromContext;
}
const allowedControls = useAllowedControls( attributes );
const showSticky = postType === 'post';
const {
Expand Down
9 changes: 1 addition & 8 deletions packages/block-library/src/query/edit/query-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,24 +103,16 @@ export default function QueryContent( {
if ( isSingular && query.inherit ) {
newQuery.inherit = false;
}
// We need to update the query in the Editor if a specific post type is set.
// Unless the post type is `page`, as it usually doesn't make sense to loop
// through pages.
if ( postType && postType !== 'page' && query.postType !== postType ) {
newQuery.postType = postType;
}
if ( !! Object.keys( newQuery ).length ) {
__unstableMarkNextChangeAsNotPersistent();
updateQuery( newQuery );
}
}, [
query.perPage,
query.inherit,
query.postType,
postsPerPage,
inherit,
isSingular,
postType,
__unstableMarkNextChangeAsNotPersistent,
updateQuery,
] );
Expand Down Expand Up @@ -167,6 +159,7 @@ export default function QueryContent( {
setDisplayLayout={ updateDisplayLayout }
setAttributes={ setAttributes }
clientId={ clientId }
postTypeFromContext={ postType }
isSingular={ isSingular }
/>
</InspectorControls>
Expand Down

0 comments on commit aec7cba

Please sign in to comment.