Skip to content

Commit

Permalink
feat: Makes the search title editable.
Browse files Browse the repository at this point in the history
  • Loading branch information
vipul0425 committed May 8, 2024
1 parent 293537d commit 0d2f288
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 11 deletions.
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ Display the query title. ([Source](https://github.com/WordPress/gutenberg/tree/t
- **Name:** core/query-title
- **Category:** theme
- **Supports:** align (full, wide), color (background, gradients, text), interactivity (clientNavigation), spacing (margin, padding), typography (fontSize, lineHeight), ~~html~~
- **Attributes:** level, showPrefix, showSearchTerm, textAlign, type
- **Attributes:** level, searchResultsTerm, searchResultsTermSuffix, showPrefix, showSearchTerm, textAlign, type

## Quote

Expand Down
6 changes: 6 additions & 0 deletions packages/block-library/src/query-title/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
"showSearchTerm": {
"type": "boolean",
"default": true
},
"searchResultsTerm": {
"type": "string"
},
"searchResultsTermSuffix": {
"type": "string"
}
},
"supports": {
Expand Down
50 changes: 44 additions & 6 deletions packages/block-library/src/query-title/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
Warning,
HeadingLevelDropdown,
store as blockEditorStore,
RichText,
} from '@wordpress/block-editor';
import { ToggleControl, PanelBody } from '@wordpress/components';
import { __, sprintf } from '@wordpress/i18n';
Expand All @@ -22,7 +23,15 @@ import { useSelect } from '@wordpress/data';
const SUPPORTED_TYPES = [ 'archive', 'search' ];

export default function QueryTitleEdit( {
attributes: { type, level, textAlign, showPrefix, showSearchTerm },
attributes: {
type,
level,
textAlign,
showPrefix,
showSearchTerm,
searchResultsTerm,
searchResultsTermSuffix,
},
setAttributes,
} ) {
const { archiveTypeTitle, archiveNameLabel } = useSelect( ( select ) => {
Expand Down Expand Up @@ -123,11 +132,40 @@ export default function QueryTitleEdit( {
</PanelBody>
</InspectorControls>

<TagName { ...blockProps }>
{ showSearchTerm
? __( 'Search results for: “search term”' )
: __( 'Search results' ) }
</TagName>
{ showSearchTerm ? (
<div { ...blockProps }>
<RichText
tagName={ `h${ level }` }
value={ searchResultsTerm }
onChange={ ( content ) =>
setAttributes( { searchResultsTerm: content } )
}
placeholder={ __( 'Search Results for:' ) }
/>
<TagName>{ __( 'Search Term' ) }</TagName>
<RichText
tagName={ `h${ level }` }
value={ searchResultsTermSuffix }
onChange={ ( content ) =>
setAttributes( {
searchResultsTermSuffix: content,
} )
}
placeholder={ __( 'Suffix' ) }
/>
</div>
) : (
<RichText
{ ...blockProps }
tagName={ `h${ level }` }
value={ searchResultsTerm }
allowedFormats={ [] }
onChange={ ( content ) =>
setAttributes( { searchResultsTerm: content } )
}
placeholder={ __( 'Search Results' ) }
/>
) }
</>
);
}
Expand Down
18 changes: 14 additions & 4 deletions packages/block-library/src/query-title/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,23 @@ function render_block_core_query_title( $attributes ) {
}
}
if ( $is_search ) {
$title = __( 'Search results' );
$title = isset($attributes['searchResultsTerm']) && !empty($attributes['searchResultsTerm']) ? $attributes['searchResultsTerm'] : __( 'Search results' );

Check failure on line 41 in packages/block-library/src/query-title/index.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected 1 spaces after opening parenthesis; 0 found

Check failure on line 41 in packages/block-library/src/query-title/index.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected 1 spaces before closing parenthesis; 0 found

Check failure on line 41 in packages/block-library/src/query-title/index.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected 1 space after "!"; 0 found

Check failure on line 41 in packages/block-library/src/query-title/index.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected 1 spaces after opening parenthesis; 0 found

Check failure on line 41 in packages/block-library/src/query-title/index.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected 1 spaces before closing parenthesis; 0 found

if ( isset( $attributes['showSearchTerm'] ) && $attributes['showSearchTerm'] ) {

// Get the prefix and suffix.
$prefix = isset($attributes['searchResultsTerm']) ? $attributes['searchResultsTerm'] : __('Search results for: ');

Check failure on line 46 in packages/block-library/src/query-title/index.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected 1 spaces after opening parenthesis; 0 found

Check failure on line 46 in packages/block-library/src/query-title/index.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected 1 spaces before closing parenthesis; 0 found

Check failure on line 46 in packages/block-library/src/query-title/index.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected 1 spaces after opening parenthesis; 0 found

Check failure on line 46 in packages/block-library/src/query-title/index.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected 1 spaces before closing parenthesis; 0 found
$suffix = isset($attributes['searchResultsTermSuffix']) ? $attributes['searchResultsTermSuffix'] : '';

Check failure on line 47 in packages/block-library/src/query-title/index.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected 1 spaces after opening parenthesis; 0 found

// Get the search query.
$search_query = get_search_query();

$title = sprintf(
/* translators: %s is the search term. */
__( 'Search results for: "%s"' ),
get_search_query()
/* translators: 1: Search term prefix, 2: Search term, 3: Search term suffix. */
__('%1$s "%2$s" %3$s'),
$prefix,
$search_query,
$suffix
);
}
}
Expand Down
4 changes: 4 additions & 0 deletions packages/block-library/src/query-title/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
// This block has customizable padding, border-box makes that more predictable.
box-sizing: border-box;
}
div.wp-block-query-title__placeholder {
display: flex;
gap: 10px;
}

0 comments on commit 0d2f288

Please sign in to comment.