From 5aafffcb17ada0a1262d6ef6fcd6bc5eac41eb74 Mon Sep 17 00:00:00 2001 From: Bernie Reiter <96308+ockham@users.noreply.github.com> Date: Wed, 18 Sep 2024 14:08:08 +0200 Subject: [PATCH] Terms List block: Add Categories-specific variation (#65434) Add two variations to the Terms List block (i.e. `core/categories` -- previously named "Categories List"): One for Categories, and another one for all other taxonomies. This is mostly for better discoverability of what used to be the Categories List block under its new name. Co-authored-by: ockham Co-authored-by: gziolo Co-authored-by: fabiankaegy --- packages/block-library/src/categories/edit.js | 4 +- .../block-library/src/categories/index.js | 2 + .../src/categories/variations.js | 40 +++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) 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..8bd2769b0ec711 100644 --- a/packages/block-library/src/categories/edit.js +++ b/packages/block-library/src/categories/edit.js @@ -196,7 +196,9 @@ export default function CategoriesEdit( { } ) ) } value={ taxonomySlug } onChange={ ( selectedTaxonomy ) => - setAttributes( { taxonomy: selectedTaxonomy } ) + setAttributes( { + taxonomy: selectedTaxonomy, + } ) } /> ) } diff --git a/packages/block-library/src/categories/index.js b/packages/block-library/src/categories/index.js index 8cdcad450862a2..d30c55667d106e 100644 --- a/packages/block-library/src/categories/index.js +++ b/packages/block-library/src/categories/index.js @@ -9,6 +9,7 @@ import { category as icon } from '@wordpress/icons'; import initBlock from '../utils/init-block'; import metadata from './block.json'; import edit from './edit'; +import variations from './variations'; const { name } = metadata; @@ -18,6 +19,7 @@ export const settings = { icon, example: {}, edit, + variations, }; export const init = () => 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..94f3fb8efa6b9a --- /dev/null +++ b/packages/block-library/src/categories/variations.js @@ -0,0 +1,40 @@ +/** + * 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' ], + // 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, + }, +]; + +export default variations;