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 - Categories]: Add support for showing only top level categories #35726

Merged
merged 1 commit into from
Oct 18, 2021
Merged
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
4 changes: 4 additions & 0 deletions packages/block-library/src/categories/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
"showPostCounts": {
"type": "boolean",
"default": false
},
"showOnlyTopLevel": {
"type": "boolean",
"default": false
}
},
"supports": {
Expand Down
54 changes: 36 additions & 18 deletions packages/block-library/src/categories/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,33 @@ import { pin } from '@wordpress/icons';
import { store as coreStore } from '@wordpress/core-data';

export default function CategoriesEdit( {
attributes: { displayAsDropdown, showHierarchy, showPostCounts },
attributes: {
displayAsDropdown,
showHierarchy,
showPostCounts,
showOnlyTopLevel,
},
setAttributes,
} ) {
const selectId = useInstanceId( CategoriesEdit, 'blocks-category-select' );
const { categories, isRequesting } = useSelect( ( select ) => {
const { getEntityRecords, isResolving } = select( coreStore );
const query = { per_page: -1, hide_empty: true, context: 'view' };
return {
categories: getEntityRecords( 'taxonomy', 'category', query ),
isRequesting: isResolving( 'getEntityRecords', [
'taxonomy',
'category',
query,
] ),
};
}, [] );
const { categories, isRequesting } = useSelect(
( select ) => {
const { getEntityRecords, isResolving } = select( coreStore );
const query = { per_page: -1, hide_empty: true, context: 'view' };
if ( showOnlyTopLevel ) {
query.parent = 0;
}
return {
categories: getEntityRecords( 'taxonomy', 'category', query ),
isRequesting: isResolving( 'getEntityRecords', [
'taxonomy',
'category',
query,
] ),
};
},
[ showOnlyTopLevel ]
);
const getCategoriesList = ( parentId ) => {
if ( ! categories?.length ) {
return [];
Expand Down Expand Up @@ -133,16 +144,23 @@ export default function CategoriesEdit( {
checked={ displayAsDropdown }
onChange={ toggleAttribute( 'displayAsDropdown' ) }
/>
<ToggleControl
label={ __( 'Show hierarchy' ) }
checked={ showHierarchy }
onChange={ toggleAttribute( 'showHierarchy' ) }
/>
<ToggleControl
label={ __( 'Show post counts' ) }
checked={ showPostCounts }
onChange={ toggleAttribute( 'showPostCounts' ) }
/>
<ToggleControl
label={ __( 'Show only top level categories' ) }
checked={ showOnlyTopLevel }
onChange={ toggleAttribute( 'showOnlyTopLevel' ) }
/>
{ ! showOnlyTopLevel && (
<ToggleControl
label={ __( 'Show hierarchy' ) }
checked={ showHierarchy }
onChange={ toggleAttribute( 'showHierarchy' ) }
/>
) }
</PanelBody>
</InspectorControls>
{ isRequesting && (
Expand Down
3 changes: 3 additions & 0 deletions packages/block-library/src/categories/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ function render_block_core_categories( $attributes ) {
'show_count' => ! empty( $attributes['showPostCounts'] ),
'title_li' => '',
);
if ( ! empty( $attributes['showOnlyTopLevel'] ) && $attributes['showOnlyTopLevel'] ) {
$args['parent'] = 0;
}

if ( ! empty( $attributes['displayAsDropdown'] ) ) {
$id = 'wp-block-categories-' . $block_id;
Expand Down
3 changes: 2 additions & 1 deletion test/integration/fixtures/blocks/core__categories.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"attributes": {
"displayAsDropdown": false,
"showHierarchy": false,
"showPostCounts": false
"showPostCounts": false,
"showOnlyTopLevel": false
},
"innerBlocks": [],
"originalContent": ""
Expand Down