From 1ae6872073edd716e802c78974de8bb1bafb13e9 Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Mon, 29 Jan 2024 15:01:51 +0000 Subject: [PATCH 1/3] Render Home text if there is no attribute label present --- packages/block-library/src/home-link/edit.js | 20 +++---------------- .../block-library/src/home-link/index.php | 2 +- 2 files changed, 4 insertions(+), 18 deletions(-) diff --git a/packages/block-library/src/home-link/edit.js b/packages/block-library/src/home-link/edit.js index 4d13dd1939e4c..125dfbb1d76b1 100644 --- a/packages/block-library/src/home-link/edit.js +++ b/packages/block-library/src/home-link/edit.js @@ -6,15 +6,10 @@ import classnames from 'classnames'; /** * WordPress dependencies */ -import { - RichText, - useBlockProps, - store as blockEditorStore, -} from '@wordpress/block-editor'; +import { RichText, useBlockProps } from '@wordpress/block-editor'; import { __ } from '@wordpress/i18n'; -import { useSelect, useDispatch } from '@wordpress/data'; +import { useSelect } from '@wordpress/data'; import { store as coreStore } from '@wordpress/core-data'; -import { useEffect } from '@wordpress/element'; const preventDefault = ( event ) => event.preventDefault(); @@ -27,8 +22,6 @@ export default function HomeEdit( { attributes, setAttributes, context } ) { homeUrl: getUnstableBase()?.home, }; }, [] ); - const { __unstableMarkNextChangeAsNotPersistent } = - useDispatch( blockEditorStore ); const { textColor, backgroundColor, style } = context; const blockProps = useBlockProps( { @@ -46,13 +39,6 @@ export default function HomeEdit( { attributes, setAttributes, context } ) { const { label } = attributes; - useEffect( () => { - if ( label === undefined ) { - __unstableMarkNextChangeAsNotPersistent(); - setAttributes( { label: __( 'Home' ) } ); - } - }, [ label ] ); - return ( <>
@@ -64,7 +50,7 @@ export default function HomeEdit( { attributes, setAttributes, context } ) { { setAttributes( { label: labelValue } ); } } diff --git a/packages/block-library/src/home-link/index.php b/packages/block-library/src/home-link/index.php index 8fb5ed109019d..d3ddde2bf85f9 100644 --- a/packages/block-library/src/home-link/index.php +++ b/packages/block-library/src/home-link/index.php @@ -129,7 +129,7 @@ function block_core_home_link_build_li_wrapper_attributes( $context ) { */ function render_block_core_home_link( $attributes, $content, $block ) { if ( empty( $attributes['label'] ) ) { - return ''; + $attributes['label'] = __( 'Home' ); } $aria_current = ''; From 3d9faff826f150c2dcc23a245492ec1c55e4ef36 Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Tue, 30 Jan 2024 12:08:28 +0000 Subject: [PATCH 2/3] Revert edit.js updates --- packages/block-library/src/home-link/edit.js | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/packages/block-library/src/home-link/edit.js b/packages/block-library/src/home-link/edit.js index 125dfbb1d76b1..4d13dd1939e4c 100644 --- a/packages/block-library/src/home-link/edit.js +++ b/packages/block-library/src/home-link/edit.js @@ -6,10 +6,15 @@ import classnames from 'classnames'; /** * WordPress dependencies */ -import { RichText, useBlockProps } from '@wordpress/block-editor'; +import { + RichText, + useBlockProps, + store as blockEditorStore, +} from '@wordpress/block-editor'; import { __ } from '@wordpress/i18n'; -import { useSelect } from '@wordpress/data'; +import { useSelect, useDispatch } from '@wordpress/data'; import { store as coreStore } from '@wordpress/core-data'; +import { useEffect } from '@wordpress/element'; const preventDefault = ( event ) => event.preventDefault(); @@ -22,6 +27,8 @@ export default function HomeEdit( { attributes, setAttributes, context } ) { homeUrl: getUnstableBase()?.home, }; }, [] ); + const { __unstableMarkNextChangeAsNotPersistent } = + useDispatch( blockEditorStore ); const { textColor, backgroundColor, style } = context; const blockProps = useBlockProps( { @@ -39,6 +46,13 @@ export default function HomeEdit( { attributes, setAttributes, context } ) { const { label } = attributes; + useEffect( () => { + if ( label === undefined ) { + __unstableMarkNextChangeAsNotPersistent(); + setAttributes( { label: __( 'Home' ) } ); + } + }, [ label ] ); + return ( <>
@@ -50,7 +64,7 @@ export default function HomeEdit( { attributes, setAttributes, context } ) { { setAttributes( { label: labelValue } ); } } From 3ca2508a25bbafe92ae7c29ac83635c873021a0d Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Tue, 30 Jan 2024 14:18:07 +0000 Subject: [PATCH 3/3] Update packages/block-library/src/home-link/index.php Co-authored-by: Bernie Reiter <96308+ockham@users.noreply.github.com> --- packages/block-library/src/home-link/index.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/block-library/src/home-link/index.php b/packages/block-library/src/home-link/index.php index d3ddde2bf85f9..9ec0c185872c0 100644 --- a/packages/block-library/src/home-link/index.php +++ b/packages/block-library/src/home-link/index.php @@ -129,6 +129,9 @@ function block_core_home_link_build_li_wrapper_attributes( $context ) { */ function render_block_core_home_link( $attributes, $content, $block ) { if ( empty( $attributes['label'] ) ) { + // Using a fallback for the label attribute allows rendering the block even if no attributes have been set, + // e.g. when using the block as a hooked block. + // Note that the fallback value needs to be kept in sync with the one set in `edit.js` (upon first loading the block in the editor). $attributes['label'] = __( 'Home' ); } $aria_current = '';