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

Render empty Nav block if no fallback block can be utilised #36849

Merged
merged 1 commit into from Nov 25, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 13 additions & 2 deletions packages/block-library/src/navigation/index.php
Expand Up @@ -187,14 +187,20 @@ function( $block ) {
* @return array the array of blocks to be used as a fallback.
*/
function block_core_navigation_get_fallback_blocks() {
// Default to a list of Pages.
$fallback_blocks = array(
$page_list_fallback = array(
array(
'blockName' => 'core/page-list',
'attrs' => array(),
),
);

$registry = WP_Block_Type_Registry::get_instance();

// If `core/page-list` is not registered then return empty blocks.
$fallback_blocks = $registry->is_registered( 'core/page-list' ) ? $page_list_fallback : array();

// Default to a list of Pages.

$navigation_post = block_core_navigation_get_first_non_empty_navigation();

// Prefer using the first non-empty Navigation as fallback if available.
Expand Down Expand Up @@ -298,6 +304,11 @@ 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 ) ) {
return '';
}

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

Expand Down