Skip to content

Commit

Permalink
Navigation block: Add an unstable location attribute (#32491)
Browse files Browse the repository at this point in the history
* Navigation block: Add an unstable location attribute

* Change the signature and name of the function that renders a classic menu

* Use the location parameter when rendering the classic menu

* lint fix
  • Loading branch information
scruffian authored Jun 17, 2021
1 parent 271d711 commit 3506d1c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/block-library/src/navigation/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
"isResponsive": {
"type": "boolean",
"default": false
},
"__unstable__location": {
"type": "string"
}
},
"providesContext": {
Expand Down
39 changes: 39 additions & 0 deletions packages/block-library/src/navigation/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,37 @@ function block_core_navigation_build_css_font_sizes( $attributes ) {
return $font_sizes;
}

/**
* Renders a Navigation Block derived from data from the theme_location assigned
* via the block attribute 'location'.
*
* If the theme doesn't explicity support 'block-nav-menus' or no location was provided
* as a block attribute then an empty string is returned.
*
* @param array $location The location of the classic menu to display.
* @param array $attributes Navigation block attributes.
* @return string|false HTML markup of a generated Navigation Block or false if no location is specified.
*/
function render_classic_location_menu( $location, $attributes ) {
if ( empty( $location ) ) {
return false;
}

$block_attributes = $attributes;
unset( $block_attributes['__unstable__location'] );

return wp_nav_menu(
array(
'theme_location' => $location,
'block_attributes' => $block_attributes,
'container' => '',
'items_wrap' => '%3$s',
'fallback_cb' => false,
'echo' => false,
)
);
}

/**
* Returns the top-level submenu SVG chevron icon.
*
Expand Down Expand Up @@ -133,6 +164,14 @@ function render_block_core_navigation( $attributes, $content, $block ) {
}

if ( empty( $block->inner_blocks ) ) {
if ( array_key_exists( '__unstable__location', $attributes ) ) {
$location = $attributes['__unstable__location'];
$maybe_classic_navigation = render_classic_location_menu( $location, $attributes );
if ( $maybe_classic_navigation ) {
return $maybe_classic_navigation;
}
}

return '';
}

Expand Down

0 comments on commit 3506d1c

Please sign in to comment.