Skip to content

Commit

Permalink
Implement Page List fallback for Nav block on front end of site when …
Browse files Browse the repository at this point in the history
…no menu selected (#36724)

* Use get_pages as block fallback

* Simplify by using page list block

* Output special class when rendering the fallback

* Remove requirement for parsing the block

* Fix linting

* Limit max number of pages to 4 via context API

* 2nd attempt to fix PHP linting

* Revert debugging

* Make attribute/context prop "unstable"

* More linting
  • Loading branch information
getdave authored Nov 22, 2021
1 parent d3ad805 commit 7839432
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
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"
},
"__unstableMaxPages": {
"type": "number"
}
},
"usesContext": [ "navigationArea" ],
Expand All @@ -72,7 +75,8 @@
"showSubmenuIcon": "showSubmenuIcon",
"openSubmenusOnClick": "openSubmenusOnClick",
"style": "style",
"orientation": "orientation"
"orientation": "orientation",
"__unstableMaxPages": "__unstableMaxPages"
},
"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['__unstableMaxPages'] = 4; // set value to be passed as context to Page List block.

$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",
"__unstableMaxPages"
],
"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( '__unstableMaxPages', $block->context ) ) {
$nested_pages = array_slice( $nested_pages, 0, $block->context['__unstableMaxPages'] );
}

$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

0 comments on commit 7839432

Please sign in to comment.