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 block: Add an unstable location attribute #32491

Merged
merged 4 commits into from
Jun 17, 2021

Conversation

scruffian
Copy link
Contributor

@scruffian scruffian commented Jun 7, 2021

Description

This is a simplified version of #30852.

All it does is add an __unstable__location attribute to the block and then, if that is set, render a classic menu in its place.

This opens some questions about if/how this should be handled in the UI but these can be dealt with in followup PRs if necessary.

How has this been tested?

  • Add a menu to a menu location for your site
  • Add a navigation block with the "__unstable__location" attribute set to the location of the menu
  • Confirm that you can see the classic menu rendering
  • I tested with the Quadrat theme:

Screenshots

Screenshot 2021-06-07 at 17 47 20

Types of changes

New feature (non-breaking change which adds functionality)

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • I've tested my changes with keyboard and screen readers.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR (please manually search all *.native.js files for terms that need renaming or removal).

Copy link
Contributor

@draganescu draganescu left a comment

Choose a reason for hiding this comment

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

This PR is pretty good like this. Left two nitpicks.

* @param array $attributes Navigation block attributes.
* @return string HTML markup of a generated Navigation Block.
*/
function get_classic_navigation_elements( $attributes ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We could rename get_classic_navigation_elements to render_classic_location_menu because that is what the function does, and the getter aspect is not correct. I would also change the signature to include $location as a parameter since it's a bit opaque otherwise => render_classic_navigation( $location, $attributes ) : mixed {}; I'd make it return false for an empty location.

@@ -133,7 +163,7 @@ function render_block_core_navigation( $attributes, $content, $block ) {
}

if ( empty( $block->inner_blocks ) ) {
return '';
return get_classic_navigation_elements( $attributes );
Copy link
Contributor

Choose a reason for hiding this comment

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

This code reads "if the block is empty render a classic menu", which is not correct. It should read "if the block is empty and there is an associated classic menu, render that". So, what about this as an option:

  1. rename get_classic_navigation_elements to render_classic_location_menu
  2. update render_classic_location_menu to return false if no location is set
  3. change this to:
if ( empty( $block->inner_blocks ) ) {
	$maybe_classic_navigation = render_classic_location_menu( $attributes );
	if ( $maybe_classic_navigation ) {
		return $maybe_classic_navigation;
	}
	return '';
}

@scruffian scruffian force-pushed the add/unstable-location-to-navigation branch from 566b7c3 to de22b21 Compare June 15, 2021 13:44
Copy link
Contributor

@draganescu draganescu left a comment

Choose a reason for hiding this comment

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

I believe this is a minimal change and it's worth merging like this. We'll see how the location attribute evolves. Also giving the block the power to render a classic menu is a superpower useful for theme migration. 🚢 it.

@draganescu draganescu merged commit 3506d1c into trunk Jun 17, 2021
@draganescu draganescu deleted the add/unstable-location-to-navigation branch June 17, 2021 06:57
@github-actions github-actions bot added this to the Gutenberg 11.0 milestone Jun 17, 2021
@talldan
Copy link
Contributor

talldan commented Jun 17, 2021

Was the main problem of #30852 addressed (that it relies on add_theme_support( 'block-nav-menus' ))?

@@ -42,6 +42,9 @@
"isResponsive": {
"type": "boolean",
"default": false
},
"__unstable__location": {
Copy link
Contributor

Choose a reason for hiding this comment

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

It should be __unstableLocation. Attributes are camel case.

Copy link
Contributor

Choose a reason for hiding this comment

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

🤦🏻‍♂️ that's my bad. Will update.

Copy link
Contributor

Choose a reason for hiding this comment

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

Added #32783 to fix this 🙏🏻

@draganescu
Copy link
Contributor

draganescu commented Jun 17, 2021

Was the main problem of #30852 addressed

Yes, we're not checking on that anymore, if the location attribute is present we render the menu found in said location using wp_nav_menu.

return wp_nav_menu(
array(
'theme_location' => $location,
'block_attributes' => $block_attributes,
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not needed I think.

@talldan
Copy link
Contributor

talldan commented Jun 21, 2021

Great, thank you for addressing that. 👍

The render_classic_location_menu doc block in this PR does still mention the following:

If the theme doesn't explicity support 'block-nav-menus' or no location was provided
as a block attribute then an empty string is returned.

I don't think that's the case now, so maybe that could be updated too.

* @param array $attributes Navigation block attributes.
* @return string|false HTML markup of a generated Navigation Block or false if no location is specified.
*/
function render_classic_location_menu( $location, $attributes ) {
Copy link
Member

Choose a reason for hiding this comment

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

Can we rename this to render_menu_from_location or something to that effect?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, it should probably be prefixed with gutenberg_ too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done here: #32918

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Navigation Affects the Navigation Block
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants