From 566b7c36ca4b2f289bc7b5eabbb9e35ed7d35640 Mon Sep 17 00:00:00 2001 From: Ben Dwyer Date: Mon, 7 Jun 2021 17:43:52 +0100 Subject: [PATCH] Navigation block: Add an unstable location attribute --- .../block-library/src/navigation/block.json | 3 ++ .../block-library/src/navigation/index.php | 32 ++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) 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..7f1fe4cdcffc53 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -85,6 +85,36 @@ 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 $attributes Navigation block attributes. + * @return string HTML markup of a generated Navigation Block. + */ +function get_classic_navigation_elements( $attributes ) { + if ( ! array_key_exists( '__unstable__location', $attributes ) ) { + return ''; + } + + $block_attributes = $attributes; + unset( $block_attributes['__unstable__location'] ); + + return wp_nav_menu( + array( + 'theme_location' => $attributes['__unstable__location'], + 'block_attributes' => $block_attributes, + 'container' => '', + 'items_wrap' => '%3$s', + 'fallback_cb' => false, + 'echo' => false, + ) + ); +} + /** * Returns the top-level submenu SVG chevron icon. * @@ -133,7 +163,7 @@ function render_block_core_navigation( $attributes, $content, $block ) { } if ( empty( $block->inner_blocks ) ) { - return ''; + return get_classic_navigation_elements( $attributes ); } $colors = block_core_navigation_build_css_colors( $attributes );