Skip to content

Commit

Permalink
Allow filtering of Nav block fallback (#36850)
Browse files Browse the repository at this point in the history
* Create filter and improve output validity test

* Document the filter
  • Loading branch information
getdave committed Nov 26, 2021
1 parent 27d413c commit d7a0066
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions packages/block-library/src/navigation/index.php
Expand Up @@ -214,7 +214,16 @@ function block_core_navigation_get_fallback_blocks() {
$fallback_blocks = ! empty( $maybe_fallback ) ? $maybe_fallback : $fallback_blocks;
}

return $fallback_blocks;
/**
* Filters the fallback experience for the Navigation block.
*
* Returning a falsey value will opt out of the fallback and cause the block not to render.
* To customise the blocks provided return an array of blocks - these should be valid
* children of the `core/navigation` block.
*
* @param array[] default fallback blocks provided by the default block mechanic.
*/
return apply_filters( 'block_core_navigation_render_fallback', $fallback_blocks );
}

/**
Expand Down Expand Up @@ -310,12 +319,13 @@ function render_block_core_navigation( $attributes, $content, $block ) {

$fallback_blocks = block_core_navigation_get_fallback_blocks();

// May be empty if core/navigation or core/page list are not registered.
if ( empty( $fallback_blocks ) ) {
// Fallback my have been filtered so do basic test for validity.
if ( empty( $fallback_blocks ) || ! is_array( $fallback_blocks ) ) {
return '';
}

$inner_blocks = new WP_Block_List( $fallback_blocks, $attributes );

}

// Restore legacy classnames for submenu positioning.
Expand Down Expand Up @@ -424,8 +434,8 @@ function render_block_core_navigation( $attributes, $content, $block ) {
/**
* Register the navigation block.
*
* @throws WP_Error An WP_Error exception parsing the block definition.
* @uses render_block_core_navigation()
* @throws WP_Error An WP_Error exception parsing the block definition.
*/
function register_block_core_navigation() {
register_block_type_from_metadata(
Expand Down

0 comments on commit d7a0066

Please sign in to comment.