Using custom Entities to retrieve external data in the Block Editor

Question

I have a theoretic question regarding entities in the Block Editor, since I can find virtually no documentation on entities.

I am building a plugin for the Block Editor that relies on plugin settings in the options table. There are many ways to retrieve these settings inside of the Block Editor. The easiest of which is to add 'show_in_rest' when registering the settings and then use useEntityProp( 'root', 'site', 'my-custom-setting' ). This works great so long as a user is an admin. Since none admins do not have access to settings in WordPress, a fetch error occurs when using useEntityProp and the user is not an admin.

I thought of many alternatives: create global variable, create a custom REST endpoint and fetch directly, etc. All were either bad coding practices (IMO) or had significant performance implications.

Then I stumbled upon entities, and the addEntities action. Combining a custom REST endpoint with a new entity, added via addEntities, I am able to retrieve virtually any data in the Block Editor with extremely minimal impacts to performance. You would get the data with getEntityRecord.

So my question is, is this an appropriate usage of addEntities and getEntityRecord or am I doing something uncouth? Has anyone else used this method before? Because this approach really opens up a ton of doors to efficiently retrieving external data in the Block Editor.

0
ndiego 2 months 0 Answers 14 views 0

Leave an answer