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

Implement suitable fallback for Nav block on front end of site when no menu selected #36724

Merged
merged 10 commits into from
Nov 22, 2021
6 changes: 5 additions & 1 deletion packages/block-library/src/navigation/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
},
"customOverlayTextColor": {
"type": "string"
},
"maxPages": {
"type": "number"
}
},
"usesContext": [ "navigationArea" ],
Expand All @@ -72,7 +75,8 @@
"showSubmenuIcon": "showSubmenuIcon",
"openSubmenusOnClick": "openSubmenusOnClick",
"style": "style",
"orientation": "orientation"
"orientation": "orientation",
"maxPages": "maxPages"
},
"supports": {
"align": [ "wide", "full" ],
Expand Down
16 changes: 14 additions & 2 deletions packages/block-library/src/navigation/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ function block_core_navigation_render_submenu_icon() {
* @return string Returns the post content with the legacy widget added.
*/
function render_block_core_navigation( $attributes, $content, $block ) {

$is_fallback = false;
/**
* Deprecated:
* The rgbTextColor and rgbBackgroundColor attributes
Expand Down Expand Up @@ -214,8 +216,17 @@ function( $block ) {
$inner_blocks = new WP_Block_List( $compacted_blocks, $attributes );
}

// If there are no inner blocks then fallback to rendering the Page List block.
if ( empty( $inner_blocks ) ) {
return '';
$is_fallback = true; // indicate we are rendering the fallback.
$attributes['maxPages'] = 4; // set value to be passed as context to Page List block.
getdave marked this conversation as resolved.
Show resolved Hide resolved

$page_list_block = array(
'blockName' => 'core/page-list',
'attrs' => array(),
);

$inner_blocks = new WP_Block_List( array( $page_list_block ), $attributes );
}

// Restore legacy classnames for submenu positioning.
Expand All @@ -234,7 +245,8 @@ function( $block ) {
$colors['css_classes'],
$font_sizes['css_classes'],
$is_responsive_menu ? array( 'is-responsive' ) : array(),
$layout_class ? array( $layout_class ) : array()
$layout_class ? array( $layout_class ) : array(),
$is_fallback ? array( 'is-fallback' ) : array()
);

$inner_blocks_html = '';
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/page-list/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"customFontSize",
"showSubmenuIcon",
"style",
"openSubmenusOnClick"
"openSubmenusOnClick",
"maxPages"
],
"supports": {
"reusable": false,
Expand Down
4 changes: 4 additions & 0 deletions packages/block-library/src/page-list/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,10 @@ function render_block_core_page_list( $attributes, $content, $block ) {

$nested_pages = block_core_page_list_nest_pages( $top_level_pages, $pages_with_children );

if ( array_key_exists( 'maxPages', $block->context ) ) {
$nested_pages = array_slice( $nested_pages, 0, $block->context['maxPages'] );
}

$is_navigation_child = array_key_exists( 'showSubmenuIcon', $block->context );

$open_submenus_on_click = array_key_exists( 'openSubmenusOnClick', $block->context ) ? $block->context['openSubmenusOnClick'] : false;
Expand Down