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 - Post Terms]: Add dynamic variations of custom taxonomies #39837

Merged
merged 3 commits into from
May 11, 2022
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
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ A calendar of your site’s posts. ([Source](https://github.com/WordPress/gutenb
- **Supports:** align
- **Attributes:** month, year

## Categories
## Categories List

Display a list of all categories. ([Source](https://github.com/WordPress/gutenberg/tree/trunk/packages/block-library/src/categories))

Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"src/**/*.scss",
"src/navigation-link/index.js",
"src/template-part/index.js",
"src/query/index.js"
"src/query/index.js",
"src/post-terms/index.js"
],
"dependencies": {
"@babel/runtime": "^7.16.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/categories/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"name": "core/categories",
"title": "Categories",
"title": "Categories List",
"category": "widgets",
"description": "Display a list of all categories.",
"textdomain": "default",
Expand Down
27 changes: 27 additions & 0 deletions packages/block-library/src/post-terms/hooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* WordPress dependencies
*/
import { postCategories, postTerms } from '@wordpress/icons';

const variationIconMap = {
category: postCategories,
post_tag: postTerms,
};

// We add `icons` to categories and tags. The remaining ones use
// the block's default icon.
export default function enhanceVariations( settings, name ) {
if ( name !== 'core/post-terms' ) {
return settings;
}
const variations = settings.variations.map( ( variation ) => ( {
...variation,
...( variationIconMap[ variation.name ] && {
icon: variationIconMap[ variation.name ],
} ),
} ) );
return {
...settings,
variations,
};
}
21 changes: 14 additions & 7 deletions packages/block-library/src/post-terms/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
/**
* Internal dependencies
* WordPress dependencies
*/
import metadata from './block.json';
import edit from './edit';
import variations from './variations';
import { postCategories as icon } from '@wordpress/icons';
import { addFilter } from '@wordpress/hooks';

/**
* WordPress dependencies
* Internal dependencies
*/
import { postTerms as icon } from '@wordpress/icons';
import metadata from './block.json';
import edit from './edit';
import enhanceVariations from './hooks';

const { name } = metadata;
export { metadata, name };

export const settings = {
icon,
variations,
edit,
};

// Importing this file includes side effects. This is whitelisted in block-library/package.json under sideEffects
addFilter(
'blocks.registerBlockType',
'core/template-part',
enhanceVariations
);
38 changes: 38 additions & 0 deletions packages/block-library/src/post-terms/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,48 @@ function render_block_core_post_terms( $attributes, $content, $block ) {
* Registers the `core/post-terms` block on the server.
*/
function register_block_core_post_terms() {
$taxonomies = get_taxonomies(
array(
'public' => true,
'show_in_rest' => true,
),
'objects'
);

// Split the available taxonomies to `built_in` and custom ones,
// in order to prioritize the `built_in` taxonomies at the
// search results.
$built_ins = array();
$custom_variations = array();

// Create and register the eligible taxonomies variations.
foreach ( $taxonomies as $taxonomy ) {
$variation = array(
'name' => $taxonomy->name,
'title' => $taxonomy->label,
/* translators: %s: taxonomy's label */
'description' => sprintf( __( 'Display the assigned taxonomy: %s' ), $taxonomy->label ),
'attributes' => array(
'term' => $taxonomy->name,
),
'isActive' => array( 'term' ),
);
// Set the category variation as the default one.
if ( 'category' === $taxonomy->name ) {
$variation['isDefault'] = true;
}
if ( $taxonomy->_builtin ) {
$built_ins[] = $variation;
} else {
$custom_variations[] = $variation;
}
}

register_block_type_from_metadata(
__DIR__ . '/post-terms',
array(
'render_callback' => 'render_block_core_post_terms',
'variations' => array_merge( $built_ins, $custom_variations ),
)
);
}
Expand Down
28 changes: 0 additions & 28 deletions packages/block-library/src/post-terms/variations.js

This file was deleted.