Skip to content

Commit

Permalink
Navigation block: Add an unstable location attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
scruffian committed Jun 7, 2021
1 parent 7e54162 commit 566b7c3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
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
32 changes: 31 additions & 1 deletion packages/block-library/src/navigation/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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 );
Expand Down

0 comments on commit 566b7c3

Please sign in to comment.