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

Post terms: Add prefix & suffix #40559

Merged
merged 4 commits into from
Apr 29, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ Post terms. ([Source](https://github.com/WordPress/gutenberg/tree/trunk/packages
- **Name:** core/post-terms
- **Category:** theme
- **Supports:** color (background, gradients, link, text), typography (fontSize, lineHeight), ~~html~~
- **Attributes:** separator, term, textAlign
- **Attributes:** prefix, separator, suffix, term, textAlign

## Post Title

Expand Down
8 changes: 8 additions & 0 deletions packages/block-library/src/post-terms/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
"separator": {
"type": "string",
"default": ", "
},
"prefix": {
"type": "string",
"default": ""
},
"suffix": {
"type": "string",
"default": ""
}
},
"usesContext": [ "postId", "postType" ],
Expand Down
29 changes: 28 additions & 1 deletion packages/block-library/src/post-terms/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
BlockControls,
useBlockProps,
useBlockDisplayInformation,
RichText,
} from '@wordpress/block-editor';
import { Spinner, TextControl } from '@wordpress/components';
import { useSelect } from '@wordpress/data';
Expand All @@ -30,7 +31,7 @@ export default function PostTermsEdit( {
context,
setAttributes,
} ) {
const { term, textAlign, separator } = attributes;
const { term, textAlign, separator, prefix, suffix } = attributes;
const { postId, postType } = context;

const selectedTerm = useSelect(
Expand Down Expand Up @@ -83,6 +84,19 @@ export default function PostTermsEdit( {
</InspectorControls>
<div { ...blockProps }>
{ isLoading && <Spinner /> }
{ ! isLoading && hasPostTerms && (
<RichText
className="wp-block-post-terms__prefix"
multiline={ false }
aria-label={ __( 'Prefix' ) }
placeholder={ __( 'Prefix' ) + ' ' }
value={ prefix }
onChange={ ( value ) =>
setAttributes( { prefix: value } )
}
tagName="span"
/>
) }
{ ! isLoading &&
hasPostTerms &&
postTerms
Expand All @@ -108,6 +122,19 @@ export default function PostTermsEdit( {
! hasPostTerms &&
( selectedTerm?.labels?.no_terms ||
__( 'Term items not found.' ) ) }
{ ! isLoading && hasPostTerms && (
<RichText
className="wp-block-post-terms__suffix"
multiline={ false }
aria-label={ __( 'Suffix' ) }
placeholder={ ' ' + __( 'Suffix' ) }
value={ suffix }
onChange={ ( value ) =>
setAttributes( { suffix: value } )
}
tagName="span"
/>
) }
</div>
</>
);
Expand Down
14 changes: 12 additions & 2 deletions packages/block-library/src/post-terms/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,22 @@ function render_block_core_post_terms( $attributes, $content, $block ) {

$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classes ) );

$prefix = "<div $wrapper_attributes>";
if ( isset( $attributes['prefix'] ) && $attributes['prefix'] ) {
$prefix .= '<span class="wp-block-post-terms__prefix">' . $attributes['prefix'] . '</span>';
dsas marked this conversation as resolved.
Show resolved Hide resolved
}

$suffix = '</div>';
if ( isset( $attributes['suffix'] ) && $attributes['suffix'] ) {
$suffix = '<span class="wp-block-post-terms__suffix">' . $attributes['suffix'] . $suffix;
dsas marked this conversation as resolved.
Show resolved Hide resolved
}

return get_the_term_list(
$block->context['postId'],
$attributes['term'],
"<div $wrapper_attributes>",
$prefix,
'<span class="wp-block-post-terms__separator">' . esc_html( $separator ) . '</span>',
'</div>'
$suffix
);
}

Expand Down
4 changes: 3 additions & 1 deletion test/integration/fixtures/blocks/core__post-terms.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"name": "core/post-terms",
"isValid": true,
"attributes": {
"separator": ", "
"prefix": "",
"separator": ", ",
"suffix": ""
},
"innerBlocks": []
}
Expand Down