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

Saving post in block editor always prompts for "untitled" when saving a custom entity. #68771

Closed
3 of 6 tasks
gschaub opened this issue Jan 19, 2025 · 8 comments
Closed
3 of 6 tasks
Labels
Needs Technical Feedback Needs testing from a developer perspective. [Package] Core data /packages/core-data [Type] Bug An existing feature does not function as intended

Comments

@gschaub
Copy link

gschaub commented Jan 19, 2025

Description

I am developing a custom block that includes a custom entity called "table". Upon save, the prompt shows asks if I want to save an entity record of entity type "table". The entity itself is "Untitled". I have not discovered a way to make this either avoid the prompt or provide a title for the entity record. My desire is to avoid the prompt altogether. However, if one would want a prompt, you should be able to provide a title for the entity record.

The Editor component save-publish-panes controls this prompt by selecting "hasNonPostEntityChanges" from the editor store. This essentially looks for dirty entity records where the entity's "kind" !== postType and "name" !== wp_template. In my case the custom entity has kind of "dynamic-tables/v1" (I understand from research that this must be the path to the custom plugin) and a name of "table". I have also populated the entity attribute for "title" which should be the entity record name, but it defaults back to "untitled".

Finally, the documentation for creating custom entities limited to non-existent. However, my understanding the entity must have:

  • kind = a type of entity,
  • name = name of entity,
  • id = numeric identifier associated with the entity record.

This is the definition for my entity

dispatch('core').addEntities([
{
name: 'table',
kind: 'dynamic-tables',
baseURL: '/dynamic-tables/v1/tables',
baseURLParams: { context: 'edit' },
plural: 'tables',
label: __('Table')
}
]);

Step-by-step reproduction instructions

1 - Create a custom entity that is not a post type
2 - Add the custom entity to a custom block which can be edited through the block editor
3 - Create or modity a post, add the custom block to the post and save the post.
4 - The save message asks if you want to save for the custom entity with an "untitled" name

Screenshots, screen recording, code snippet

Image

Environment info

  • Wordpress 6.7.1, Gutenberg 19.9.0 running on Local by Flywheel
  • Windows 11 desktop with Chrome 131.0.6778.266

Please confirm that you have searched existing issues in the repo.

  • Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

  • Yes

Please confirm which theme type you used for testing.

  • Block
  • Classic
  • Hybrid (e.g. classic with theme.json)
  • Not sure
@gschaub gschaub added the [Type] Bug An existing feature does not function as intended label Jan 19, 2025
@t-hamano t-hamano added Needs Testing Needs further testing to be confirmed. [Package] Core data /packages/core-data labels Jan 21, 2025
@fabiankaegy
Copy link
Member

Hey @gschaub 👋

We discussed this ticket in a Weekly Bug Scrub in the WordPress Slack and would love to get some sample code that helps reproduce the issue.

Custom Entities that aren't Post Types are quire rare & custom so getting a specific example of how you are using it would help determine the root of the issue.

Thanks in advance! :)

@gschaub
Copy link
Author

gschaub commented Jan 22, 2025

Hey @fabiankaegy, This is a rather large project and I wasn't certain I could just send code snippets that would be fully relevant. Therefore, I have invited you to my repository. Note that it is still in heavy development, the practical implication of which is that the code is not even close to as tidy as it will be when I get closer to publishing it.

A word on what you'll find: The entity is created in edit.js. However, most of the block itself uses another store called "dynamic-tables/table". The custom entity is exclusively managed through this store which lives in the data directory. You will also see that I have tortured the code in workarounds because I cannot alter the underlying Gutenberg functionality.

Finally, I am happy to share the repository with others on this team as needed. Please just supply their github user informaiton.

@Mamaduka
Copy link
Member

@gschaub, have you tried using the getTitle property in entity configuration? The getTitle callback will receive an entity record as an argument. You can try to derive the title from there.

See gthe lobal styles example:

label: __( 'Global Styles' ),
name: 'globalStyles',
kind: 'root',
baseURL: '/wp/v2/global-styles',
baseURLParams: { context: 'edit' },
plural: 'globalStylesVariations', // Should be different from name.
getTitle: ( record ) => record?.title?.rendered || record?.title,
getRevisionsUrl: ( parentId, revisionId ) =>
`/wp/v2/global-styles/${ parentId }/revisions${
revisionId ? '/' + revisionId : ''
}`,
supportsPagination: true,
},

@gschaub
Copy link
Author

gschaub commented Jan 22, 2025

@Mamaduka , I may be misinterpreting your question, but I know what the title of my entity is because I set it. My challenge is that I don't know how to pass a title attibute that the editor will recognize as such upon saving. Additionally, this particular method appears oriented toward the root entity 'globalStyles'.

Finally, my ultimate goal is to avoid being prompted to save for my entity at all because it is simply part of the block. If I save a post with a paragraph block, the editor does not ask me if I want to save the paragraph block. It should work the same way for my block.

@Mamaduka
Copy link
Member

@gschaub, the getTitle is an entity config property; it can be added to any editing. I just shared global styles as an example. If your entity record contains any field that can be used as the human-readable title instead of the default fallback.

Finally, my ultimate goal is to avoid being prompted to save for my entity at all because it is simply part of the block. If I save a post with a paragraph block, the editor does not ask me if I want to save the paragraph block. It should work the same way for my block.

This is intentional because the user is editing an entity other than the current post/page. UI allows final review before persisting changes to the DB.

Example:

dispatch('core').addEntities([
	{
		name: 'table',
		kind: 'dynamic-tables',
		baseURL: '/dynamic-tables/v1/tables',
		baseURLParams: { context: 'edit' },
		plural: 'tables',
		label: __('Table'),
		getTitle: ( record ) => record?.title || __( 'Table name' ),
	}
]);

@gschaub
Copy link
Author

gschaub commented Jan 22, 2025

Hey @Mamaduka - That worked! Thank you. I still consider this a workaround because it shouldn't be there at all. While I understand the "why" of associated with the prompt, I don't believe it considers the potential use cases for the custom entity. In this particular case, I am storing post data (i.e., associated with the block) to custom tables; it is a conscious architectural decision.

I am using the entity rather that fetch because of the way entities integrate within the save/post process (i.e., it fires the the PUT, POST, DELETE method based on interaction with the Editor UI that lives outside of my block). From a user experience, having this prompt appear would be no less awkward to the user than if they were prompted for each native block (e.g., paragraph, list, quote, codes, etc.) associated with a post.

My request is not to remove the prompt, but to make it optional at design time. Perhaps it could be hidden based on new entity property. Can this be linked to an enhancement request?

@Mamaduka
Copy link
Member

@gschaub, here's a discussion regarding double confirmation - #38714.

Do you mind closing this issue and continuing the discussion in #38714?

@gschaub
Copy link
Author

gschaub commented Jan 22, 2025

@Mamaduka - Happy to close this issue and track it through issue #38714

@gschaub gschaub closed this as completed Jan 22, 2025
@Mamaduka Mamaduka added Needs Technical Feedback Needs testing from a developer perspective. and removed Needs Testing Needs further testing to be confirmed. labels Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Technical Feedback Needs testing from a developer perspective. [Package] Core data /packages/core-data [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

No branches or pull requests

4 participants