From 534ace097c4a5bf15f7b1a6f9b8fef37d460a2a3 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 18 Sep 2024 09:41:45 +0200 Subject: [PATCH 1/4] Terms List block: Add Categories-specific variation --- packages/block-library/src/categories/edit.js | 39 +++++++++++-------- .../block-library/src/categories/index.js | 2 + .../src/categories/variations.js | 36 +++++++++++++++++ 3 files changed, 61 insertions(+), 16 deletions(-) create mode 100644 packages/block-library/src/categories/variations.js diff --git a/packages/block-library/src/categories/edit.js b/packages/block-library/src/categories/edit.js index ccbf9194339408..5df4faaf26854e 100644 --- a/packages/block-library/src/categories/edit.js +++ b/packages/block-library/src/categories/edit.js @@ -46,7 +46,11 @@ export default function CategoriesEdit( { 'taxonomy' ); - const taxonomies = allTaxonomies?.filter( ( t ) => t.visibility.public ); + const taxonomies = allTaxonomies?.filter( + ( t ) => + t.visibility.public && + ( taxonomySlug === 'category' ) === ( t.slug === 'category' ) + ); const taxonomy = taxonomies?.find( ( t ) => t.slug === taxonomySlug ); @@ -185,21 +189,24 @@ export default function CategoriesEdit( { - { Array.isArray( taxonomies ) && ( - ( { - label: t.name, - value: t.slug, - } ) ) } - value={ taxonomySlug } - onChange={ ( selectedTaxonomy ) => - setAttributes( { taxonomy: selectedTaxonomy } ) - } - /> - ) } + { taxonomySlug !== 'category' && // For back-compat, the Category taxonomy has its own block variation. + Array.isArray( taxonomies ) && ( + ( { + label: t.name, + value: t.slug, + } ) ) } + value={ taxonomySlug } + onChange={ ( selectedTaxonomy ) => + setAttributes( { + taxonomy: selectedTaxonomy, + } ) + } + /> + ) } initBlock( { name, metadata, settings } ); diff --git a/packages/block-library/src/categories/variations.js b/packages/block-library/src/categories/variations.js new file mode 100644 index 00000000000000..b4e7e4c56ae026 --- /dev/null +++ b/packages/block-library/src/categories/variations.js @@ -0,0 +1,36 @@ +/** + * WordPress dependencies + */ +import { __ } from '@wordpress/i18n'; +import { category as icon } from '@wordpress/icons'; + +const variations = [ + { + name: 'terms', + title: __( 'Terms List' ), + icon, + attributes: { + // We need to set an attribute here that will be set when inserting the block. + // We cannot leave this empty, as that would be interpreted as the default value, + // which is `category` -- for which we're defining a distinct variation below, + // for backwards compatibility reasons. + // The logical fallback is thus the only other built-in and public taxonomy: Tags. + taxonomy: 'post_tag', + }, + isActive: ( blockAttributes ) => + // This variation is used for any taxonomy other than `category`. + blockAttributes.taxonomy !== 'category', + }, + { + name: 'categories', + title: __( 'Categories List' ), + description: __( 'Display a list of all categories.' ), + icon, + attributes: { + taxonomy: 'category', + }, + isActive: [ 'taxonomy' ], + }, +]; + +export default variations; From cd379f75c6dbee1e756dee79369f6f50c3d2e29f Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 18 Sep 2024 10:00:44 +0200 Subject: [PATCH 2/4] Make Terms List the default variation --- packages/block-library/src/categories/variations.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/block-library/src/categories/variations.js b/packages/block-library/src/categories/variations.js index b4e7e4c56ae026..d35aa21fc31a9a 100644 --- a/packages/block-library/src/categories/variations.js +++ b/packages/block-library/src/categories/variations.js @@ -30,6 +30,7 @@ const variations = [ taxonomy: 'category', }, isActive: [ 'taxonomy' ], + isDefault: true, }, ]; From 6a8c539de47ee84a978b1e0c7b415e2c73a697ab Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 18 Sep 2024 10:03:43 +0200 Subject: [PATCH 3/4] Add explanatory comment --- packages/block-library/src/categories/variations.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/block-library/src/categories/variations.js b/packages/block-library/src/categories/variations.js index d35aa21fc31a9a..94f3fb8efa6b9a 100644 --- a/packages/block-library/src/categories/variations.js +++ b/packages/block-library/src/categories/variations.js @@ -30,6 +30,9 @@ const variations = [ taxonomy: 'category', }, isActive: [ 'taxonomy' ], + // The following is needed to prevent "Terms List" from showing up twice in the inserter + // (once for the block, once for the variation). Fortunately, it does not collide with + // `categories` being the default value of the `taxonomy` attribute. isDefault: true, }, ]; From 9f70db6587555b9e437f00edf4bffeff570e8f82 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 18 Sep 2024 10:11:22 +0200 Subject: [PATCH 4/4] Keep Categories in the dropdown --- packages/block-library/src/categories/edit.js | 41 ++++++++----------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/packages/block-library/src/categories/edit.js b/packages/block-library/src/categories/edit.js index 5df4faaf26854e..8bd2769b0ec711 100644 --- a/packages/block-library/src/categories/edit.js +++ b/packages/block-library/src/categories/edit.js @@ -46,11 +46,7 @@ export default function CategoriesEdit( { 'taxonomy' ); - const taxonomies = allTaxonomies?.filter( - ( t ) => - t.visibility.public && - ( taxonomySlug === 'category' ) === ( t.slug === 'category' ) - ); + const taxonomies = allTaxonomies?.filter( ( t ) => t.visibility.public ); const taxonomy = taxonomies?.find( ( t ) => t.slug === taxonomySlug ); @@ -189,24 +185,23 @@ export default function CategoriesEdit( { - { taxonomySlug !== 'category' && // For back-compat, the Category taxonomy has its own block variation. - Array.isArray( taxonomies ) && ( - ( { - label: t.name, - value: t.slug, - } ) ) } - value={ taxonomySlug } - onChange={ ( selectedTaxonomy ) => - setAttributes( { - taxonomy: selectedTaxonomy, - } ) - } - /> - ) } + { Array.isArray( taxonomies ) && ( + ( { + label: t.name, + value: t.slug, + } ) ) } + value={ taxonomySlug } + onChange={ ( selectedTaxonomy ) => + setAttributes( { + taxonomy: selectedTaxonomy, + } ) + } + /> + ) }