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 authored and noisysocks committed Nov 29, 2021
1 parent 8b95760 commit 7dac0e3
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions packages/block-library/src/navigation/index.php
Expand Up @@ -212,7 +212,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 @@ -304,12 +313,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

0 comments on commit 7dac0e3

Please sign in to comment.