Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Home link: Allow usage outside the navigation block #60558

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,6 @@ Create a link that always points to the homepage of the site. Usually not necess

- **Name:** core/home-link
- **Category:** design
- **Parent:** core/navigation
- **Supports:** interactivity (clientNavigation), typography (fontSize, lineHeight), ~~html~~, ~~reusable~~
- **Attributes:** label

Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/home-link/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"apiVersion": 3,
"name": "core/home-link",
"category": "design",
"parent": [ "core/navigation" ],
"title": "Home Link",
"description": "Create a link that always points to the homepage of the site. Usually not necessary if there is already a site title link present in the header.",
"textdomain": "default",
Expand All @@ -19,7 +18,8 @@
"customBackgroundColor",
"fontSize",
"customFontSize",
"style"
"style",
"showSubmenuIcon"
],
"supports": {
"reusable": false,
Expand Down
13 changes: 12 additions & 1 deletion packages/block-library/src/home-link/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,20 @@ function render_block_core_home_link( $attributes, $content, $block ) {
$aria_current = ' aria-current="page"';
}

// If the home link is inside a navigation block, it should be wrapped in a list item with additional attributes.
// Use the block context to determine if the home link is inside a navigation block.
$in_navigation = ! empty( $block->context );

if ( $in_navigation ) {
$item_markup = '<li %1$s><a class="wp-block-home-link__content wp-block-navigation-item__content" href="%3$s" rel="home"%4$s>%5$s</a></li>';
} else {
$item_markup = '<a %2$s href="%3$s" rel="home"%4$s>%5$s</a>';
}
Comment on lines +154 to +162
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this may be better covered via the block_core_navigation_listable_blocks filter introduced in 6.5.

Or you can add the block to the default array here:

private static $needs_list_item_wrapper = array(
'core/site-title',
'core/site-logo',
);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. Thanks I will look those up!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though how would I avoid it breaking on 6.3, 6.4?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm mostly thinking out loud, but this block would also need the current-menu-item class on the li.


return sprintf(
'<li %1$s><a class="wp-block-home-link__content wp-block-navigation-item__content" href="%2$s" rel="home"%3$s>%4$s</a></li>',
$item_markup,
block_core_home_link_build_li_wrapper_attributes( $block->context ),
get_block_wrapper_attributes(),
esc_url( home_url() ),
$aria_current,
wp_kses_post( $attributes['label'] )
Expand Down
Loading