-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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: Surface menu name
in the List View
next to the Navigation block
#68446
base: trunk
Are you sure you want to change the base?
Navigation: Surface menu name
in the List View
next to the Navigation block
#68446
Conversation
menu name
in the List View
next to the Navigation block
.menu name
in the List View
next to the Navigation block
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Hi @getdave, can you please review this PR when you get a moment? Thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for working on this :) The ens result you have is great and really useful. However the way that it is currently implemented is not ideal.
Instead of storing the title of the menu in an attribute we should instead use the actual title
of the post connected to the navigation block via the ref
. That way if someone renames the navigation the title stays accurate and the value cannot get out of sync.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow! That was quick :) Thanks for the update. Left 2 more small notes :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the review @fabiankaegy. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR! I think this is a fantastic improvement.
My suggestion is to simply display only the name of the navigation menu. In other words, {navigationName}
instead of Navigation ({navigationName})
. What do you think?
This is because in the initial installation of WordPress it is displayed as follows:
Also, the Template Part block only displays the name of the template part.
Additionally, we need to decide whether the new name should apply only to the List View. In this PR, the new name is also applied to the block toolbar:
If we want the new name to apply only to the List View, we need to check the context
prop (Example).
@@ -52,6 +55,26 @@ export const settings = { | |||
}, | |||
edit, | |||
save, | |||
__experimentalLabel: ( { ref } ) => { | |||
if ( ! ref ) { | |||
return __( 'Navigation' ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The block title may have changed. Example code:
function change_navigation_block_name( $args, $block_type ) {
if ( 'core/navigation' !== $block_type ) {
return $args;
}
$args['title'] = __( 'My Navigation!' );
return $args;
}
add_filter( "register_block_type_args", "change_navigation_block_name", 10, 2 );
Should this possibility be considered?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can return undefined
here. This will signal the block editor to display the block's default title.
See the Template Part block for a similar example:
gutenberg/packages/block-library/src/template-part/index.js
Lines 31 to 33 in e746a95
if ( ! slug ) { | |
return; | |
} |
Another suggestion on this:
|
return navigation?.title | ||
? sprintf( | ||
/* translators: %1$s: block title, %2$s: navigation menu title */ | ||
__( '%1$s (%2$s)' ), | ||
metadata.title, | ||
decodeEntities( navigation.title ) | ||
) | ||
: __( 'Navigation' ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here. Let's return undefined
when Navigation has no title. You can use early return to make things more readable, IMO.
ref | ||
); | ||
|
||
if ( ! navigation?.title || navigation.title === metadata.title ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The navigation.title === metadata.title
condition will never be true
. The Navigation block has its renaming system and disabled renaming
support.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the block title and navigation name are the same, display Navigation.
I was attempting to achieve this. It works fine when both the texts are Navigation
but if the label is changed using the following snippet, it fails:
function change_navigation_block_name( $args, $block_type ) {
if ( 'core/navigation' !== $block_type ) {
return $args;
}
$args['title'] = __( 'My Navigation!' );
return $args;
}
add_filter( "register_block_type_args", "change_navigation_block_name", 10, 2 );
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Mamaduka I think this is for the case when the Navigation menu is named Navigation
(which is the default name).
Because we don't want the label to read Navigation (Navigation)
So that is what this comparison is for
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel we can use the runtime title for navigation block:
const runtimeBlockTitle =
select( blocksStore ).getBlockType( 'core/navigation' )?.title;
This way, even if the filter updates the title, we can accommodate for the changes.
Like, if the title becomes My Navigation, then My Navigation (My Navigation)
=> My Navigation
case will be covered. However, the above condition works just fine if we want to check Navigation
itself, as the name is relatively static. I'm not sure if we should accommodate for the title changes on the fly (via filter) for this one. If so, this is the way to go.
What are your thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should just drop the block title from here and only display the navigation title. No other block (Template Part, Synced Pattern) does this. Let's keep the behavior consistent.
@t-hamano, also suggested the same: #68446 (comment).
Let's just use this logic. What do you think?
return decodeEntities( navigation.title );
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good @Mamaduka. The icon can convey the meaning of the block IMO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The icon can convey the meaning of the block IMO.
That's the current convention in the List View, and the Block Card in the right sidebar displays a badge with the original block title.
Thanks for the follow ups, @yogeshbhutkar! I’ll try to do another round of testing when I get some time and merge the update. |
This PR mostly works, but there are visual changes in the list view. Add the following code to function change_navigation_block_name( $args, $block_type ) {
if ( 'core/navigation' !== $block_type ) {
return $args;
}
$args['title'] = __( 'My PHP Navigation!' );
return $args;
}
add_filter( "register_block_type_args", "change_navigation_block_name", 10, 2 ); trunk: All block titles will be updated. This PR: Only list view block titles will not be updated. Is this an acceptable edge case? |
Let’s see how Template Part and Sync Pattern handle this. Navigation block should do the same. I think that means removing context limitation. P.S. I think renaming blocks via PHP definition is super rare case anyways. |
Template parts, Sync pattern, the entity title are displayed consistently in all the following places.
So it might be a good idea to remove the context in the Navigation block as well. |
Also with the change in #65641 it makes sense to remove the block name from the custom label when we display a custom label :) |
Fixes Point 2 of #68177
What, Why and How?
This PR enhances the
Navigation
block by displaying themenu name
alongside theNavigation
text. It achieves this by introducing a newmenuTitle
attribute, initialized during block rendering. The label is displayed using the__experimentalLabel
attribute, passed to theinitBlock
method for rendering.Testing Instructions
Navigation Block
.List View
(left side) next to theNavigation
text.Screencast
Screenshots