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

[WIP] dynamic titles and URLs for Nav block items #46891

Draft
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ function LinkControl( {
createSuggestionButtonText
}
hideLabelFromVision={ ! showTextControl }
isDisabled={ value?.id }
/>
</div>
{ errorMessage && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const LinkControlSearchInput = forwardRef(
withURLSuggestion = true,
createSuggestionButtonText,
hideLabelFromVision = false,
isDisabled = false,
},
ref
) => {
Expand Down Expand Up @@ -156,6 +157,7 @@ const LinkControlSearchInput = forwardRef(
}
} }
ref={ ref }
isDisabled={ isDisabled }
/>
{ children }
</div>
Expand Down
2 changes: 2 additions & 0 deletions packages/block-editor/src/components/url-input/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ class URLInput extends Component {
__experimentalRenderControl: renderControl,
value = '',
hideLabelFromVision = false,
isDisabled,
} = this.props;

const {
Expand Down Expand Up @@ -476,6 +477,7 @@ class URLInput extends Component {
? `${ suggestionOptionIdPrefix }-${ selectedSuggestion }`
: undefined,
ref: this.inputRef,
disabled: isDisabled,
};

if ( renderControl ) {
Expand Down
22 changes: 21 additions & 1 deletion packages/block-library/src/navigation-link/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
} from '@wordpress/dom';
import { decodeEntities } from '@wordpress/html-entities';
import { link as linkIcon, addSubmenu } from '@wordpress/icons';
import { store as coreStore } from '@wordpress/core-data';
import { store as coreStore, useEntityRecord } from '@wordpress/core-data';
import { useMergeRefs } from '@wordpress/compose';

/**
Expand Down Expand Up @@ -180,6 +180,8 @@ export default function NavigationLinkEdit( {
const itemLabelPlaceholder = __( 'Add label…' );
const ref = useRef();

const { record: navPostRecord } = useEntityRecord( 'postType', type, id );

// Change the label using inspector causes rich text to change focus on firefox.
// This is a workaround to keep the focus on the label field when label filed is focused we don't render the rich text.
const [ isLabelFieldFocused, setIsLabelFieldFocused ] = useState( false );
Expand Down Expand Up @@ -282,6 +284,24 @@ export default function NavigationLinkEdit( {
}
}, [ url ] );

useEffect( () => {
// Only updates attributes if:
// - there is an ID (and it has changed).
// - there is a navPostRecord.
if ( id && navPostRecord ) {
// Conditionall update attributes to avoid
// unnecessary re-renders.
setAttributes( {
...( label !== navPostRecord?.title.rendered && {
label: navPostRecord?.title.rendered,
} ),
...( url !== navPostRecord?.link && {
url: navPostRecord?.link,
} ),
} );
}
}, [ id, navPostRecord ] );

/**
* Focus the Link label text and select it.
*/
Expand Down
22 changes: 12 additions & 10 deletions packages/block-library/src/navigation-link/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,18 @@ function render_block_core_navigation_link( $attributes, $content, $block ) {
$is_post_type = isset( $attributes['kind'] ) && 'post-type' === $attributes['kind'];
$is_post_type = $is_post_type || isset( $attributes['type'] ) && ( 'post' === $attributes['type'] || 'page' === $attributes['type'] );

$post = $is_post_type && $navigation_link_has_id ? get_post( $attributes['id'] ) : null;

// Don't render the block's subtree if it is a draft or if the ID does not exist.
if ( $is_post_type && $navigation_link_has_id ) {
$post = get_post( $attributes['id'] );
if ( ! $post || 'publish' !== $post->post_status ) {
return '';
}
if ( ! $post || 'publish' !== $post->post_status ) {
return '';
}

$dynamic_url = $post ? get_permalink( $post ) : $attributes['url'];
$dynamic_label = $post ? $post->post_title : $attributes['label'];

// Don't render the block's subtree if it has no label.
if ( empty( $attributes['label'] ) ) {
if ( empty( $dynamic_label ) ) {
return '';
}

Expand All @@ -195,8 +197,8 @@ function render_block_core_navigation_link( $attributes, $content, $block ) {
'<a class="wp-block-navigation-item__content" ';

// Start appending HTML attributes to anchor tag.
if ( isset( $attributes['url'] ) ) {
$html .= ' href="' . esc_url( block_core_navigation_link_maybe_urldecode( $attributes['url'] ) ) . '"';
if ( isset( $dynamic_url ) ) {
$html .= ' href="' . esc_url( block_core_navigation_link_maybe_urldecode( $dynamic_url ) ) . '"';
}

if ( $is_active ) {
Expand Down Expand Up @@ -224,8 +226,8 @@ function render_block_core_navigation_link( $attributes, $content, $block ) {
// Wrap title with span to isolate it from submenu icon.
'<span class="wp-block-navigation-item__label">';

if ( isset( $attributes['label'] ) ) {
$html .= wp_kses_post( $attributes['label'] );
if ( isset( $dynamic_label ) ) {
$html .= wp_kses_post( $dynamic_label );
}

$html .= '</span>';
Expand Down
5 changes: 3 additions & 2 deletions packages/block-library/src/navigation-link/link-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export function LinkUI( props ) {
};
}

const { label, url, opensInNewTab, type, kind } = props.link;
const { label, url, opensInNewTab, type, kind, id } = props.link;

let userCanCreate = false;
if ( ! type || type === 'page' ) {
Expand All @@ -174,8 +174,9 @@ export function LinkUI( props ) {
url,
opensInNewTab,
title: label && stripHTML( label ),
id,
} ),
[ label, opensInNewTab, url ]
[ label, opensInNewTab, url, id ]
);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,13 @@ export const updateAttributes = (
( ! newKind && ! isBuiltInType ) || newKind === 'custom';
const kind = isCustomLink ? 'custom' : newKind;

const hasId = id && Number.isInteger( id );

setAttributes( {
// Passed `url` may already be encoded. To prevent double encoding, decodeURI is executed to revert to the original string.
...( newUrl && { url: encodeURI( safeDecodeURI( newUrl ) ) } ),
...( label && { label } ),
...( undefined !== opensInNewTab && { opensInNewTab } ),
...( id && Number.isInteger( id ) && { id } ),
...( hasId && { id } ),
...( kind && { kind } ),
...( type && type !== 'URL' && { type } ),
} );
Expand Down
Loading