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

Query Title: add Search and 404 page titles #33515

Closed
wants to merge 54 commits into from

Conversation

mikachan
Copy link
Member

@mikachan mikachan commented Jul 16, 2021

Description

This PR addresses #33476 and is predominantly based on the work in #27989.

The change re-introduces a search title to the Query Title block from #27989 and adds a new 404 title to the same block. Both use the _x() function to provide translatable titles: "Search results for %search%" is used for the search title and "Nothing found" is used for the 404 title.

I've tried to be as mindful as possible of the previous discussion in #27989 and cherry-picked the search title changes in line with the current archive title, but I'd love to get some ideas on any other approaches or thoughts.

How has this been tested?

In emptytheme and Blockbase by using the editor to add Query Title blocks to the search, 404, archive, and index templates.

Screenshots

image

image

image

Types of changes

Non-breaking change which adds functionality.

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • I've tested my changes with keyboard and screen readers.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR (please manually search all *.native.js files for terms that need renaming or removal).

@github-actions
Copy link

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @mikachan! In case you missed it, we'd love to have you join us in our Slack community, where we hold regularly weekly meetings open to anyone to coordinate with each other.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

@github-actions github-actions bot added the First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository label Jul 16, 2021
@mikachan mikachan changed the title Add/search 404 titles Query Title: add Search and 404 page titles Jul 16, 2021
@mikachan mikachan marked this pull request as ready for review July 19, 2021 11:03
@mikachan mikachan requested a review from ajitbohra as a code owner July 19, 2021 11:03
},
{
name: 'search-title',
title: __( 'Search Title' ),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these need to be variations or could we just infer from context what to display? As a template author, what happens if I only have index.html with archive title added?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're able to infer what to display from the page context that would work better I think. This would be much quicker for creating and editing templates. I'll look into this, thanks!

} else {
titleElement = (
<div { ...blockProps }>
<RichText
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to use RichText here rather than an H* element like we do for the Archive title?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure actually, I pulled this from the previous work on the search title in #27989. I'm guessing the intent was to use the additional functionality you get from RichText, but using the H* / TagName would be easier to read and maintain because there are fewer props. I'll try it out with just the H* tag.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RichText should be used if text is meant to be editable

@ntsekouras
Copy link
Contributor

👋 - thanks for working on this!

Why would we need a 404 title? This should be handled in 404 template where we can add whatever blocks we want.

@scruffian
Copy link
Contributor

Why would we need a 404 title? This should be handled in 404 template where we can add whatever blocks we want.

The main benefit is for i18n. If we just add a title that says "Nothing found" then it won't be translated, whereas a block like this will be.

Copy link
Contributor

@ntsekouras ntsekouras left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this @mikachan !

I've left some comments and in general I'm not sure what we gain by not using block variations. Is it mostly because we want users to insert just one block (Query Title) in archive, search, and 404 ? What are other benefits and how they are compared to block variations benefits/restrictions.

In addition if we go this way, we should add a deprecation for this block (removal of type attribute).

if ( $is_archive ) {
$title = get_the_archive_title();
}
if ( $is_search ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: we could make this elseif.

);

// Use placeholder content if new Query Title block is added
if ( content === '' ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't add the placeholder to content here as the user will not be able to clear the input and write something new.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah I can see this problem when I try to clear the input. If I use a non-empty default placeholder such as 'Query title' rather than checking for an empty string, it looks like I can clear the input safely from the editor.

I'm not sure this is the correct fix though, as this logic was mainly because my use of useEffect was overwriting the custom content when a new query title block was added. I'll try and fix this as part of a useEffect refactor.

case 'archive':
// translators: Title for archive template.
content = _x( 'Archive title', 'archive template title' );
titleElement = <TagName { ...blockProps }>{ content }</TagName>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

content is not used in archive so there is no reason to assign it to content.
It should be <TagName { ...blockProps }>{ _x( 'Archive title', 'archive template title' ) }</TagName>;

case '404':
// translators: Title for 404 template.
content = _x( 'Nothing found', '404 template title' );
titleElement = <TagName { ...blockProps }>{ content }</TagName>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO this should be editable. I don't think every user should want Nothing found as a title without the ability to change it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If they want to edit it can't they just add their own heading block?

}

// Update content based on current template
useEffect( () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems hacky to me 😄 . We use React's implementation details to update the content after the first render and by updating the content attribute, we also create an undo level in history. That's one of the reasons I had block variations in mind back then. The php handling remains the same, the above content attribute handling is done by the variations and we can provide a bit more specific info for the block (BlockCard etc..).

An alternative, if this approach is preferred, is to use __unstableMarkNextChangeAsNotPersistent here and a new variable for the first content (to avoid changing the existing content var).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha I wasn't sure where else to turn so I jumped to useEffect! Thanks so much for the detailed explanation.

As soon as I moved this logic from the variations file to edit.js, I could see the advantage of using variations. However, as @scruffian has just mentioned above, I think the advantage to not using variations is that the user (or a theme) only needs to insert one block, and the block works the content out itself based on context (i.e. the template).

With that in mind, I'm going to attempt your suggested alternative. Thanks for the suggestion and input, it's really helped me understand this more.

Content is not used in archive
This allows the user to delete the block content and write something new
@scruffian
Copy link
Contributor

@ntsekouras

Is it mostly because we want users to insert just one block (Query Title) in archive, search, and 404

The benefit I see is it means a simple theme can just have one index.html and the block will handle all scenarios...

@ntsekouras
Copy link
Contributor

ntsekouras commented Jul 20, 2021

The benefit I see is it means a simple theme can just have one index.html and the block will handle all scenarios...

I can't see this unless I'm missing something.

If you have only an index.html and add the Query Title block, it will not match any of the supported [ 'archive', 'search', '404' ] based on templateSlug. Right now it will show a placeholder of Template is not supported and the content will be the default content from block.json.

So in cases like search where the content attribute is used, it will have the default value.

@mikachan
Copy link
Member Author

If you have only an index.html and add the Query Title block, it will not match any of the supported [ 'archive', 'search', '404' ] based on templateSlug.

Yeah I see what you mean, the way I've currently structured this means that this block still wouldn't work by itself on a single index.html template. Even if index is added to the supported templates, it will currently show the default title which isn't based on any meaningful context. I think it's still cool that we could have one block that can base its content on the templateSlug, but that doesn't address being able to have a simple theme with a single index template.

I'm not sure how (or the best way) to achieve this... I think one of the biggest things I'm struggling with for the single index template approach is that the editor has no context of the page query, as its purpose is a default 'catch-all' template. For example, if we want the search title to be editable, we may need to go back to having variations in order to show the search content, as I don't think there's any other way to provide the 'search' context on the index template. But then, with this approach, would we then expect users to add multiple title blocks for each variation (assuming only one would display on the front end based on context)?

Another approach I've just been trying out is allowing users to set the title content from the Inspector Controls in the right sidebar. With this, I'm thinking the block would show placeholder text (similar to how the archive title currently works), and then the user could customise the title variation contents in the sidebar, with something like this:

image

My latest commits are for this approach, although not fully tested yet, it's more of a proof of concept at the moment.

Any thoughts on this would be appreciated, and apologies for any lack of understanding on my part!

@ntsekouras ntsekouras requested a review from a team July 22, 2021 06:37
@scruffian
Copy link
Contributor

The way I see if, if the block is used on a page on which it doesn't have any default content then it would simply output nothing. That's what I added the early return and changed the default content to an empty string.

I'm not a fan of the idea of putting the content in the sidebar as we want the default experience to be one of direct manipulation.

"type": {
"content": {
"type": "string",
"default": "Query title placeholder"
Copy link
Member

@swissspidy swissspidy Jul 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd avoid non-translatable default values in block.json attributes.

Put the default text in the edit component instead and make it translatable.

}
$tag_name = isset( $attributes['level'] ) ? 'h' . (int) $attributes['level'] : 'h1';
$align_class_name = empty( $attributes['textAlign'] ) ? '' : "has-text-align-{$attributes['textAlign']}";
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $align_class_name ) );
if ( 'Query title placeholder' === $title || empty( $title ) ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per my other comment, this should be translatable

@mikachan
Copy link
Member Author

I'm not a fan of the idea of putting the content in the sidebar as we want the default experience to be one of direct manipulation.

Yeah, I agree. It's a bit of an odd pattern as well as I'm guessing we'd never really want users editing content via the settings controls. I tried this as a way to allow the user to edit multiple types of title context at once, but I think maybe I'm over-complicating things...

I've just pushed some changes that go back to using the editor as usual, with a couple of tweaks:

  • If the block is added to the index template it will show a non-editable 'query title' placeholder in the editor. On the front end, it will switch to the pre-defined search, 404 or archive titles based on the query context.
  • If the block is added to the search or 404 template, it will show the corresponding default title and allow the user to edit it.
  • If the block is added to the archive template, it will show the archive placeholder in the editor and insert an archive title based on get_the_archive_title() on the front end (this is the existing functionality)

My aim here was to capture both allowing the user to edit the search and 404 titles when they're added to specific templates, but if not, still allow for a context-based title if the block is added to the index template. This means the title isn't editable via the editor when used on the index template, but this does allow for quick template/theme creation with context-based, translatable titles.

@scruffian
Copy link
Contributor

I talked to @mtias about this and we decided that this is an unnecessary optimisation that can make things worse for the user.

By providing specific search and 404 templates, we will make it easier for users to understand which template relates to which page. If we want everything to inherit from index that actually makes it harder for users to understand the templating system. However, if the user has search or 404 templates then they can easily make edits to them.

On the other hand, if the user does not have search or 404 templates then nothing will break, the page titles on the 404 and search pages will just be empty.

@scruffian scruffian closed this Jul 14, 2022
@mikachan mikachan deleted the add/search-404-titles branch July 14, 2022 22:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Query Title Affects the Query Title Block First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants