Quite often, a plugin needs to offer a set of configuration options to the user who installs it.
DatoCMS offers every plugin a configuration screen and a read-write object that can be used to store such settings. It is a free-form object, with no restrictions in the format. Plugins can store what they want in it, and retrieve its value anytime they need in any hook.
As the configuration parameters are completely arbitrary, it is up to the plugin itself to show the user a form through which they can be changed.
The hook provided for this purpose is called renderConfigScreen, and it will be called by DatoCMS when the user visits the details page of the installed plugin:
Implementing a simple configuration form
To give a very simple example, let's say our plugin wants to provide the end user with a simple boolean flag called debugMode. If this flag is enabled, then the plugin will display a series of debug messages in the console as it works.
The first step is to implement the renderConfigScreen hook, which will simply initialize React by rendering a custom component called ConfigScreen:
The hook, in its ctx argument, provides a series of information and methods for interacting with the main application, and for now we'll just pass the whole object to the component, in the form of a React prop:
1
import{ Canvas }from'datocms-react-ui';
2
3
type PropTypes ={
4
ctx: RenderConfigScreenCtx;
5
};
6
7
functionConfigScreen({ctx}: PropTypes){
8
return (
9
<Canvasctx={ctx}>
10
Hello from the config screen!
11
</Canvas>
12
);
13
}
Always use the canvas!
It is important to wrap the content inside the Canvas component, so that the iframe will continuously auto-adjust its size based on the content we're rendering, and to give our app the look and feel of the DatoCMS web app.
we can access the currently saved configuration object through ctx.plugin.attributes.parameters
we can call ctx.updatePluginParameters() to save a new configuration object.
Once saved, settings are always available as ctx.plugin.attributes.parameters in any of the other hooks, so that your plugin can have different behaviours based on them.
Using a form management library
If you have more complex settings, feel free to use one of the many form management libraries available for React to avoid code repetition.
We recommend react-final-form, as it works well and is quite lightweight (~8kb). Here's a more complete example using it:
'Lorem Ipsum is simply dummy text of the printing and typesetting industry',
5
choices: [
6
{
7
label:'Positive',
8
value:'positive',
9
intent:'positive',
10
},
11
{
12
label:'Negative',
13
value:'negative',
14
intent:'negative',
15
},
16
],
17
cancel:{
18
label:'Cancel',
19
value:false,
20
},
21
});
22
23
if (result) {
24
ctx.notice(`Success! ${result}`);
25
}else{
26
ctx.alert('Cancelled!');
27
}
These properties provide access to "entity repos", that is, the collection of
resources of a particular type that have been loaded by the CMS up to this
moment. The entity repos are objects, indexed by the ID of the entity itself.
All the models of the current DatoCMS project, indexed by ID.
All the fields currently loaded for the current DatoCMS project, indexed by
ID. If some fields you need are not present, use the loadItemTypeFields
function to load them.
All the fieldsets currently loaded for the current DatoCMS project, indexed
by ID. If some fields you need are not present, use the
loadItemTypeFieldsets function to load them.
All the regular users currently loaded for the current DatoCMS project,
indexed by ID. It will always contain the current user. If some users you
need are not present, use the loadUsers function to load them.
All the SSO users currently loaded for the current DatoCMS project, indexed
by ID. It will always contain the current user. If some users you need are
not present, use the loadSsoUsers function to load them.
These methods let you open the standard DatoCMS dialogs needed to interact
with records.
Opens a dialog for creating a new record. It returns a promise resolved
with the newly created record or null if the user closes the dialog
without creating anything.
const itemTypeId =prompt('Please insert a model ID:');
2
3
const item =await ctx.createNewItem(itemTypeId);
4
5
if (item) {
6
ctx.notice(`Success! ${item.id}`);
7
}else{
8
ctx.alert('Closed!');
9
}
Opens a dialog for selecting one (or multiple) record(s) from a list of
existing records of type itemTypeId. It returns a promise resolved with
the selected record(s), or null if the user closes the dialog without
choosing any record.
Opens a dialog for editing an existing record. It returns a promise
resolved with the edited record, or null if the user closes the dialog
without persisting any change.
A number of methods that you can use to control the size of the plugin frame.
Listens for DOM changes and automatically calls setHeight when it detects
a change. If you're using datocms-react-ui package, the <Canvas />
component already takes care of calling this method for you.
Triggers a change in the size of the iframe. If you don't explicitely pass
a newHeight it will be automatically calculated using the iframe content
at the moment.
These methods can be used to update both plugin parameters and manual field
extensions configuration.
Updates the plugin parameters.
Always check ctx.currentRole.meta.final_permissions.can_edit_schema
before calling this, as the user might not have the permission to perform
the operation.
Performs changes in the appearance of a field. You can install/remove a
manual field extension, or tweak their parameters. If multiple changes are
passed, they will be applied sequencially.
Always check ctx.currentRole.meta.final_permissions.can_edit_schema
before calling this, as the user might not have the permission to perform
the operation.
ctx.notice(`Successfully edited field ${field.attributes.api_key}`);
36
}
These methods let you open the standard DatoCMS dialogs needed to interact
with Media Area assets.
Opens a dialog for selecting one (or multiple) existing asset(s). It
returns a promise resolved with the selected asset(s), or null if the
user closes the dialog without selecting any upload.
const uploadId =prompt('Please insert an asset ID:');
2
3
const item =await ctx.editUpload(uploadId);
4
5
if (item) {
6
ctx.notice(`Success! ${item.id}`);
7
}else{
8
ctx.alert('Closed!');
9
}
Opens a dialog for editing a "single asset" field structure. It returns a
promise resolved with the updated structure, or null if the user closes
the dialog without persisting any change.