From 7dac0e3d5e38b1914d428b4403a562510d4019a4 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Fri, 26 Nov 2021 09:25:41 +0000 Subject: [PATCH] Allow filtering of Nav block fallback (#36850) * Create filter and improve output validity test * Document the filter --- packages/block-library/src/navigation/index.php | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index 118b333b17de..e3e9d2d87f70 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -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 ); } /** @@ -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.