From 3506d1c5d7398eac61aa2368bc9e4af12e607adf Mon Sep 17 00:00:00 2001 From: Ben Dwyer Date: Thu, 17 Jun 2021 07:57:15 +0100 Subject: [PATCH] Navigation block: Add an unstable location attribute (#32491) * 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 --- .../block-library/src/navigation/block.json | 3 ++ .../block-library/src/navigation/index.php | 39 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/packages/block-library/src/navigation/block.json b/packages/block-library/src/navigation/block.json index bb9a24dab353c4..8f311d96fea5c6 100644 --- a/packages/block-library/src/navigation/block.json +++ b/packages/block-library/src/navigation/block.json @@ -42,6 +42,9 @@ "isResponsive": { "type": "boolean", "default": false + }, + "__unstable__location": { + "type": "string" } }, "providesContext": { diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index 0dff15eeaa1978..66ec159935cc96 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -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. * @@ -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 ''; }