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

Navigation: Add post format variation to navigation link block #30403

Merged
merged 8 commits into from
Apr 1, 2021
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: 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' };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For folks wondering about the kebab case:

if ( ! disablePostFormats && ( ! type || type === 'post-format' ) ) {

FYI on behavior, it also looks like we removed the labels in search results if all results are the same:
#24839

So this is expected behavior:

Custom Link Search No labels with all post formats
custom link search mixed types post format same types

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' ),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can also update the registration here to add the post labels. I didn't realize we wanted to show this one, so I missed it in the last PR.

https://github.com/WordPress/wordpress-develop/blob/f7d5b2bca6083a22eb4ebadb4b68a0e683bc49c4/src/wp-includes/taxonomy.php#L164-L167

And for folks 👀 changes here, the reason why we added new post labels was because if we combine strings from two translated items, it will generate poor translations.

Example from swissspidy:

In German the translation for A link to a %s. changes depending on the post type.
A link to a post -> Ein Link zu einem Beitrag
A link to a page -> Ein Link zu einer Seite
sprintf( __( 'A link to a %s.' ), $entity->labels->singular_name ) doesn't allow for that.

WP 5.8 is planned for July so I think we can safely make it. (Variation changes aren't present in WP 5.7 and below)

It's okay if we miss the date and need a fallback override, but I'd maybe leave a comment on when it can be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I'll push a PR to do that.

The thing I was referring to is how the title and description for tag and category are used as fallbacks by core for any taxonomy that doesn't provide certain labels.

On trunk I'm seeing two Tag Link variations:
Screenshot 2021-04-01 at 4 55 45 pm

The second seems to be post format taxonomy being added as variation but using the title and description of the tag taxonomy (as fallback text).

'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