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

Update/post term add toggle for no terms message #55680

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/block-library/src/post-terms/block.json
Expand Up @@ -24,6 +24,10 @@
"suffix": {
"type": "string",
"default": ""
},
"showTermNoResults": {
"type": "boolean",
"default": true
}
},
"usesContext": [ "postId", "postType" ],
Expand Down
21 changes: 17 additions & 4 deletions packages/block-library/src/post-terms/edit.js
Expand Up @@ -15,7 +15,7 @@ import {
RichText,
} from '@wordpress/block-editor';
import { createBlock, getDefaultBlockName } from '@wordpress/blocks';
import { Spinner, TextControl } from '@wordpress/components';
import { Spinner, TextControl, ToggleControl } from '@wordpress/components';
import { useSelect } from '@wordpress/data';
import { decodeEntities } from '@wordpress/html-entities';
import { __ } from '@wordpress/i18n';
Expand Down Expand Up @@ -44,7 +44,7 @@ export default function PostTermsEdit( {
setAttributes,
insertBlocksAfter,
} ) {
const { term, textAlign, separator, prefix, suffix } = attributes;
const { term, textAlign, separator, prefix, suffix, showTermNoResults } = attributes;
const { postId, postType } = context;

const selectedTerm = useSelect(
Expand Down Expand Up @@ -90,6 +90,14 @@ export default function PostTermsEdit( {
} }
help={ __( 'Enter character(s) used to separate terms.' ) }
/>
<ToggleControl
label={ __( 'Show "No tags"' ) }
help={ __( 'Show/Hide "No tags" message. Note: We show always a message in the editor for easier block access' ) }
checked={ showTermNoResults }
onChange={() =>
setAttributes( { showTermNoResults: !!! showTermNoResults } )
}
/>
</InspectorControls>
<div { ...blockProps }>
{ isLoading && hasPost && <Spinner /> }
Expand Down Expand Up @@ -135,8 +143,13 @@ export default function PostTermsEdit( {
{ hasPost &&
! isLoading &&
! hasPostTerms &&
( selectedTerm?.labels?.no_terms ||
__( 'Term items not found.' ) ) }
(
showTermNoResults
? selectedTerm?.labels?.no_terms || __( 'Term items not found.' )
: __( 'Term items not found.' )

)
}
{ ! isLoading && hasPostTerms && ( isSelected || suffix ) && (
<RichText
allowedFormats={ ALLOWED_FORMATS }
Expand Down
7 changes: 6 additions & 1 deletion packages/block-library/src/post-terms/index.php
Expand Up @@ -24,7 +24,12 @@

$post_terms = get_the_terms( $block->context['postId'], $attributes['term'] );
if ( is_wp_error( $post_terms ) || empty( $post_terms ) ) {
return '';
if ( ! $attributes['showTermNoResults'] ) {
return '';
}
$tax = get_taxonomy( $attributes['term'] );

Check warning on line 30 in packages/block-library/src/post-terms/index.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space
$labels = get_taxonomy_labels( $tax );
return '<div class="taxonomy-' . $attributes['term'] . ' wp-block-post-terms">' . esc_html( $labels->no_terms ) . '</div>';
}

$classes = array( 'taxonomy-' . $attributes['term'] );
Expand Down