Skip to content

Commit

Permalink
Navigation Link: populate variation types like post link, page link, …
Browse files Browse the repository at this point in the history
…category link in server from register_block_type_from_metadata instead of hardcoded file.
  • Loading branch information
gwwar committed Feb 26, 2021
1 parent c993a86 commit 2a2b518
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 66 deletions.
3 changes: 3 additions & 0 deletions packages/block-library/src/navigation-link/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
},
"title": {
"type": "string"
},
"kind": {
"type": "string"
}
},
"usesContext": [
Expand Down
15 changes: 13 additions & 2 deletions packages/block-library/src/navigation-link/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,10 @@ const useIsDraggingWithin = ( elementRef ) => {
* /wp/v2/search.
*
* @param {string} type Link block's type attribute.
* @param {string} kind Link block's entity of kind (post-type|taxonomy)
* @return {{ type?: string, subtype?: string }} Search query params.
*/
function getSuggestionsQuery( type ) {
function getSuggestionsQuery( type, kind ) {
switch ( type ) {
case 'post':
case 'page':
Expand All @@ -115,6 +116,12 @@ function getSuggestionsQuery( type ) {
case 'tag':
return { type: 'term', subtype: 'post_tag' };
default:
if ( kind === 'taxonomy' ) {
return { type: 'term', subtype: type };
}
if ( kind === 'post-type' ) {
return { type: 'post', subtype: type };
}
return {};
}
}
Expand All @@ -137,6 +144,7 @@ export default function NavigationLinkEdit( {
description,
rel,
title,
kind,
} = attributes;
const link = {
url,
Expand Down Expand Up @@ -501,7 +509,10 @@ export default function NavigationLinkEdit( {
} }
noDirectEntry={ !! type }
noURLSuggestion={ !! type }
suggestionsQuery={ getSuggestionsQuery( type ) }
suggestionsQuery={ getSuggestionsQuery(
type,
kind
) }
onChange={ ( {
title: newTitle = '',
url: newURL = '',
Expand Down
49 changes: 49 additions & 0 deletions packages/block-library/src/navigation-link/hooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* WordPress dependencies
*/
import { addFilter } from '@wordpress/hooks';
import { category, page, postTitle, tag } from '@wordpress/icons';

const ICON_MAP = {
category,
page,
post: postTitle,
tag,
//TODO: add a generic CPT icon, remember to ask folks for a new icon
};

function enhanceNavigationLinkVariations( settings, name ) {
if ( name !== 'core/navigation-link' ) {
return settings;
}

if ( settings?.variations ) {
const variations = settings.variations.map( ( variation ) => {
return {
...variation,
...( ! variation.icon &&
ICON_MAP[ variation.name ] && {
icon: ICON_MAP[ variation.name ],
} ),
...( ! variation.isActive && {
isActive: ( blockAttributes, variationAttributes ) => {
return (
blockAttributes.type === variationAttributes.type
);
},
} ),
};
} );
return {
...settings,
variations,
};
}
return settings;
}

addFilter(
'blocks.registerBlockType',
'core/navigation-link',
enhanceNavigationLinkVariations
);
15 changes: 11 additions & 4 deletions packages/block-library/src/navigation-link/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import { InnerBlocks } from '@wordpress/block-editor';
import metadata from './block.json';
import edit from './edit';
import save from './save';
import variations from './variations';
import './hooks';

const { name } = metadata;

export { metadata, name };
// If we export metadata, this calls unstable__bootstrapServerSideBlockDefinitions a second time, and overwrites
// what was injected on the page
export { name };

export const settings = {
title: _x( 'Link', 'block title' ),
Expand All @@ -24,8 +26,6 @@ export const settings = {

description: __( 'Add a page, link, or another item to your navigation.' ),

variations,

__experimentalLabel: ( { label } ) => label,

merge( leftAttributes, { label: rightLabel = '' } ) {
Expand All @@ -39,6 +39,13 @@ export const settings = {

save,

example: {
attributes: {
label: _x( 'Example Link', 'navigation link preview example' ),
url: 'https://example.com',
},
},

deprecated: [
{
isEligible( attributes ) {
Expand Down
40 changes: 40 additions & 0 deletions packages/block-library/src/navigation-link/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,17 +226,57 @@ function render_block_core_navigation_link( $attributes, $content, $block ) {
return $html;
}

/**
* Returns a navigation link variation
*
* @param $entity WP_Taxonomy|WP_Post_Type post type or taxonomy entity.
* @param $kind string string of value 'taxonomy' or 'post-type'
* @param $name string entity name eg: post, page, post_tag, category
*
* @return array
*/
function build_variation_for_navigation_link( $entity, $kind ) {
$name = 'post_tag' === $entity->name ? 'tag' : $entity->name;
return array(
'name' => $name,
/* translators: %s: Entity type, eg: Post Link, Page Link, Category Link, Tag Link, Portfolio Link etc */
'title' => sprintf( __( '%s Link' ), $entity->labels->singular_name ),
/* translators: %s: Entity type, eg: A link to a post. A link to a page. */
'description' => sprintf( __( 'A link to a %s.' ), $entity->labels->singular_name ),
'attributes' => array(
'type' => $name,
'kind' => $kind,
),
);
}

/**
* Register the navigation link block.
*
* @uses render_block_core_navigation()
* @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' );
$variations = array();
if ( $post_types ) {
foreach ( $post_types as $post_type ) {
$variations[] = build_variation_for_navigation_link( $post_type, 'post-type' );
}
}
if ( $taxonomies ) {
foreach ( $taxonomies as $taxonomy ) {
$variations[] = build_variation_for_navigation_link( $taxonomy, 'taxonomy' );
}
}

register_block_type_from_metadata(
__DIR__ . '/navigation-link',
array(
'render_callback' => 'render_block_core_navigation_link',
'variations' => $variations,
)
);
}
Expand Down
60 changes: 0 additions & 60 deletions packages/block-library/src/navigation-link/variations.js

This file was deleted.

0 comments on commit 2a2b518

Please sign in to comment.