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

[Categories Block]: Adds attributes and support to show current taxonomy in categories block #68505

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ Display a list of all terms of a given taxonomy. ([Source](https://github.com/Wo
- **Name:** core/categories
- **Category:** widgets
- **Supports:** align, interactivity (clientNavigation), spacing (margin, padding), typography (fontSize, lineHeight), ~~html~~
- **Attributes:** displayAsDropdown, label, showEmpty, showHierarchy, showLabel, showOnlyTopLevel, showPostCounts, taxonomy
- **Attributes:** displayAsDropdown, label, showCurrentTaxonomy, showEmpty, showHierarchy, showLabel, showOnlyTopLevel, showPostCounts, taxonomy

## Code

Expand Down
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 @@ -39,6 +39,10 @@
"showLabel": {
"type": "boolean",
"default": true
},
"showCurrentTaxonomy": {
"type": "boolean",
"default": false
}
},
"usesContext": [ "enhancedPagination" ],
Expand Down
45 changes: 31 additions & 14 deletions packages/block-library/src/categories/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export default function CategoriesEdit( {
label,
showLabel,
taxonomy: taxonomySlug,
showCurrentTaxonomy,
},
setAttributes,
className,
Expand Down Expand Up @@ -132,13 +133,15 @@ export default function CategoriesEdit( {
</VisuallyHidden>
) }
<select id={ selectId }>
<option>
{ sprintf(
/* translators: %s: taxonomy's singular name */
__( 'Select %s' ),
taxonomy.labels.singular_name
) }
</option>
{ ! showCurrentTaxonomy && (
<option>
{ sprintf(
/* translators: %s: taxonomy's singular name */
__( 'Select %s' ),
taxonomy.labels.singular_name
) }
</option>
) }
{ categoriesList.map( ( category ) =>
renderCategoryDropdownItem( category, 0 )
) }
Expand Down Expand Up @@ -209,13 +212,27 @@ export default function CategoriesEdit( {
onChange={ toggleAttribute( 'displayAsDropdown' ) }
/>
{ displayAsDropdown && (
<ToggleControl
__nextHasNoMarginBottom
className="wp-block-categories__indentation"
label={ __( 'Show label' ) }
checked={ showLabel }
onChange={ toggleAttribute( 'showLabel' ) }
/>
<>
<ToggleControl
__nextHasNoMarginBottom
className="wp-block-categories__indentation"
label={ __( 'Show label' ) }
checked={ showLabel }
onChange={ toggleAttribute( 'showLabel' ) }
/>
<ToggleControl
__nextHasNoMarginBottom
className="wp-block-categories__indentation"
label={ __( 'Show Current Taxonomy' ) }
checked={ showCurrentTaxonomy }
onChange={ toggleAttribute(
'showCurrentTaxonomy'
) }
help={ __(
'Select the current taxonomy by default in the archive pages.'
) }
/>
</>
) }
<ToggleControl
__nextHasNoMarginBottom
Expand Down
28 changes: 19 additions & 9 deletions packages/block-library/src/categories/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,25 @@ function render_block_core_categories( $attributes, $content, $block ) {
}

if ( ! empty( $attributes['displayAsDropdown'] ) ) {
$id = 'wp-block-categories-' . $block_id;
$args['id'] = $id;
$args['name'] = $taxonomy->query_var;
$args['value_field'] = 'slug';
$args['show_option_none'] = sprintf(
/* translators: %s: taxonomy's singular name */
__( 'Select %s' ),
$taxonomy->labels->singular_name
);
$id = 'wp-block-categories-' . $block_id;
$args['id'] = $id;
$args['name'] = $taxonomy->query_var;
$args['value_field'] = 'slug';

if (
! empty( $attributes['showCurrentTaxonomy'] ) &&
$attributes['showCurrentTaxonomy'] &&
isset( $taxonomy->query_var ) &&
! empty( $taxonomy->query_var )
) {
$args['selected'] = get_query_var( $taxonomy->query_var );
} else {
$args['show_option_none'] = sprintf(
/* translators: %s: taxonomy's singular name */
__( 'Select %s' ),
$taxonomy->labels->singular_name
);
}

$show_label = empty( $attributes['showLabel'] ) ? ' screen-reader-text' : '';
$default_label = $taxonomy->label;
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 @@ -9,7 +9,8 @@
"showPostCounts": false,
"showOnlyTopLevel": false,
"showEmpty": false,
"showLabel": true
"showLabel": true,
"showCurrentTaxonomy": false
},
"innerBlocks": []
}
Expand Down
Loading