Skip to content

Conversation

@getdave
Copy link
Contributor

@getdave getdave commented Nov 25, 2021

Description

In #36740 (comment) we introduced a specific fallback mechanic for the Nav block front-of-site rendering for the case where a Menu was not specifically assigned to the block.

This aimed to follow the existing code patterns set by wp_nav_menu.

That PR didn't handle allowing developers to filter the fallback output of the block. This PR enables that via the introduction of a block-specific filter block_core_navigation_render_fallback.

This is important because traditionally Theme developers have had the option to opt-out of the wp_nav_menu's fallback mechanic via the fallback_cb argument that you can pass to that function.

This PR replicates that. Developers can now do this to opt out of rendering a fallback:

add_filter('block_core_navigation_render_fallback', '__return_false');

Alternatively they can return their own list of blocks to act as a fallback with the caveat that these blocks must be valid as children of the core/navigation block.

If folks are in favour of merging this PR then I'll document the filter.

Note this overlaps with #36849. If/when that is merged then we'll consolidate the "empty" checking mechanics.

How has this been tested?

  • Delete all Navigation Menu posts from your site - http://localhost:8888/wp-admin/edit.php?post_type=wp_navigation.
  • Add several Pages to your site.
  • Add a Nav block to your site. Remove all items from the Nav block.
  • Save the Site Editor.
  • Visit front of site. You should see the core/page-list block used to render the default fallback experience.
  • Now add the filter - I did this in a test mu-plugin, but you could simply add to your Theme's functions.php:
add_filter('block_core_navigation_render_fallback', '__return_false');
  • Visit front of site. You should see that no navigation block is rendered at all - you've opted out of the the fallback experience.

Screenshots

Types of changes

Bug fix (non-breaking change which fixes an issue)

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).

@getdave getdave self-assigned this Nov 25, 2021
@getdave getdave added [Block] Navigation Affects the Navigation Block Backport to WP 6.9 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta labels Nov 25, 2021
@getdave getdave marked this pull request as ready for review November 25, 2021 10:36
@getdave
Copy link
Contributor Author

getdave commented Nov 25, 2021

cc @fklein-lu also for review

* @return array the array of blocks to be used as a fallback.
*/
function block_core_navigation_get_fallback_blocks() {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Suggested change

@getdave
Copy link
Contributor Author

getdave commented Nov 25, 2021

Let's leave this open overnight (UTC) to see if there's further feedback. If I don't receive any then I'll merge this based on @adamziel's 👍

@fklein-lu
Copy link
Contributor

Cheers for the quick follow up 👍

@getdave getdave force-pushed the fix/make-nav-block-fallback-filterable branch from b93b623 to 4750be3 Compare November 25, 2021 16:18
@getdave getdave mentioned this pull request Nov 25, 2021
38 tasks
@getdave
Copy link
Contributor Author

getdave commented Nov 26, 2021

Ok as there was no further feedback I will merge this.

@getdave getdave merged commit d7a0066 into trunk Nov 26, 2021
@getdave getdave deleted the fix/make-nav-block-fallback-filterable branch November 26, 2021 09:25
@github-actions github-actions bot added this to the Gutenberg 12.1 milestone Nov 26, 2021
@noisysocks noisysocks removed the Backport to WP 6.9 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta label Nov 29, 2021
noisysocks pushed a commit that referenced this pull request Nov 29, 2021
* Create filter and improve output validity test

* Document the filter
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.

5 participants