Skip to content

Commit

Permalink
Add post format variation to navigation link block (#30403)
Browse files Browse the repository at this point in the history
* Add post format variation to nav link block

* post format is a taxonomy

* Switch to using custom post type icon for format variation

* Override some values for post_format and post_tag

* Update test snapshot for fallback variations

* Add comment

* Use post_format as name and type for variation

* Remove post format fallback variation
  • Loading branch information
talldan authored Apr 1, 2021
1 parent aa05bd4 commit 0cbab3a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
2 changes: 2 additions & 0 deletions packages/block-library/src/navigation-link/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ function getSuggestionsQuery( type, kind ) {
return { type: 'term', subtype: 'category' };
case 'tag':
return { type: 'term', subtype: 'post_tag' };
case 'post_format':
return { type: 'post-format' };
default:
if ( kind === 'taxonomy' ) {
return { type: 'term', subtype: type };
Expand Down
46 changes: 39 additions & 7 deletions packages/block-library/src/navigation-link/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,6 @@ function render_block_core_navigation_link( $attributes, $content, $block ) {
* @return array
*/
function build_variation_for_navigation_link( $entity, $kind ) {
$name = 'post_tag' === $entity->name ? 'tag' : $entity->name;

$title = '';
$description = '';

Expand All @@ -245,15 +243,45 @@ function build_variation_for_navigation_link( $entity, $kind ) {
$description = $entity->labels->item_link_description;
}

return array(
'name' => $name,
$variation = array(
'name' => $entity->name,
'title' => $title,
'description' => $description,
'attributes' => array(
'type' => $name,
'type' => $entity->name,
'kind' => $kind,
),
);

// Tweak some value for the variations.
$variation_overrides = array(
'post_tag' => array(
'name' => 'tag',
'attributes' => array(
'type' => 'tag',
'kind' => $kind,
),
),
'post_format' => array(
// The item_link and item_link_description for post formats is the
// same as for tags, so need to be overridden.
'title' => __( 'Post Format Link' ),
'description' => __( 'A link to a post format' ),
'attributes' => array(
'type' => 'post_format',
'kind' => $kind,
),
),
);

if ( array_key_exists( $entity->name, $variation_overrides ) ) {
$variation = array_merge(
$variation,
$variation_overrides[ $entity->name ]
);
}

return $variation;
}

/**
Expand All @@ -263,9 +291,13 @@ function build_variation_for_navigation_link( $entity, $kind ) {
* @throws WP_Error An WP_Error exception parsing the block definition.
*/
function register_block_core_navigation_link() {

$post_types = get_post_types( array( 'show_in_nav_menus' => true ), 'objects' );
$taxonomies = get_taxonomies( array( 'show_in_nav_menus' => true ), 'objects' );

// Use two separate arrays as a way to order the variations in the UI.
// Known variations (like Post Link and Page Link) are added to the
// `built_ins` array. Variations for custom post types and taxonomies are
// added to the `variations` array and will always appear after `built-ins.
$built_ins = array();
$variations = array();

Expand All @@ -282,7 +314,7 @@ function register_block_core_navigation_link() {
if ( $taxonomies ) {
foreach ( $taxonomies as $taxonomy ) {
$variation = build_variation_for_navigation_link( $taxonomy, 'taxonomy' );
if ( 'category' === $variation['name'] || 'tag' === $variation['name'] ) {
if ( 'category' === $variation['name'] || 'tag' === $variation['name'] || 'post_format' === $variation['name'] ) {
$built_ins[] = $variation;
} else {
$variations[] = $variation;
Expand Down

0 comments on commit 0cbab3a

Please sign in to comment.