Sorry, no results found for "".

Plugin SDK > Dropdown actions

Dropdown actions

Plugins can greatly improve DatoCMS's overall functionality by defining custom actions that will appear as dropdown menu items or context menus throughout the entire interface, enhancing a more personalized user experience.

Custom dropdown actions can be defined in various unique contexts:

Record-Editing actions

Actions of this specific type will be prominently displayed next to the title of the record, enabling users to quickly access tools custom to streamline their editing process:

The hooks required for their implementation are:

Field-Specific Record Actions

Actions will be conveniently placed in a dropdown next to the field label, allowing users to easily interact with the data contained in a specific field of a record:

The hooks required for their implementation are:

Global Record Actions

Actions of this type will be presented in two areas of the interface: firstly, in the batch actions available within the record collection view:

And secondly, in the detailed view of the record itself (together with any available Record-Editing actions), providing users with additional options for manipulation:

The hooks required for their implementation are:

Asset Management Actions

Actions of this type will be displayed in two sections of the interface: in batch actions within the Media Area:

and in the detailed view of the asset itself:

The hooks required for their implementation are:

How to implement a dropdown action

This is a brief example of how you can implement your actions:

import {
connect,
type FieldDropdownActionsCtx,
type ExecuteFieldDropdownActionCtx,
} from "datocms-plugin-sdk";
import "datocms-react-ui/styles.css";
connect({
fieldDropdownActions(field, ctx: FieldDropdownActionsCtx) {
if (
ctx.itemType.attributes.api_key !== "blog_post" ||
field.attributes.api_key !== "title"
) {
// Don't add any action!
return [];
}
return [
// A single action
{
id: "actionA",
label: "Custom action A",
icon: "music",
},
// A group of actions
{
label: "Group of custom actions",
icon: "mug-hot",
actions: [
// These actions will be shown in a submenu
{
id: "actionB",
label: "Custom action B",
icon: "rocket-launch",
},
{
id: "actionC",
label: "Custom action C",
icon: "sparkles",
},
],
},
];
},
async executeFieldDropdownAction(
actionId: string,
ctx: ExecuteFieldDropdownActionCtx,
) {
if (actionId === "actionA") {
// Do something using ctx
ctx.notice('Selected action A');
} else if (actionId === "actionB") {
// Do something else
ctx.notice('Selected action B');
} else if (actionId === "actionC") {
// Do something else
ctx.notice('Selected action C');
}
},
});

The types of operations you can perform within your execute hooks are dependent on the methods available in the ctx argument, which in turn is influenced by the specific type of action. As an example, Record-Editing and Field-Specific Record actions offer methods in ctx to change the state of the record form, while other actions do not. Consult the specific documentation for each hook listed below to understand the available options.

executeFieldDropdownAction(actionId: string, ctx)

Use this function to execute a particular dropdown action defined via the fieldDropdownActions() hook.

Return value

The function must return: Promise<void>.

Context object

The following properties and methods are available in the ctx argument:

This hook exposes additional information and operations specific to the context in which it operates.

These information describe the current state of the field where this plugin is applied to.

Whether the field is currently disabled or not.

View on Github

The path in the formValues object where to find the current value for the field.

View on Github

The field where the field extension is installed to.

View on Github

If the field extension is installed in a field of a block, returns the top level Modular Content/Structured Text field containing the block itself.

View on Github

If the field extension is installed in a field of a block, returns the ID of the block — or undefined if the block is still not persisted — and the block model.

View on Github
These methods can be used to interact with the form that's being shown to the end-user to edit a record.

Hides/shows a specific field in the form. Please be aware that when a field is hidden, the field editor for that field will be removed from the DOM itself, including any associated plugins. When it is shown again, its plugins will be reinitialized.

View on Github
const fieldPath = prompt(
'Please insert the path of a field in the form',
ctx.fieldPath,
);
await ctx.toggleField(fieldPath, true);

Disables/re-enables a specific field in the form.

View on Github
const fieldPath = prompt(
'Please insert the path of a field in the form',
ctx.fieldPath,
);
await ctx.disableField(fieldPath, true);

Smoothly navigates to a specific field in the form. If the field is localized it will switch language tab and then navigate to the chosen field.

View on Github
const fieldPath = prompt(
'Please insert the path of a field in the form',
ctx.fieldPath,
);
await ctx.scrollToField(fieldPath);

Changes a specific path of the formValues object.

View on Github
const fieldPath = prompt(
'Please insert the path of a field in the form',
ctx.fieldPath,
);
await ctx.setFieldValue(fieldPath, 'new value');

Takes the internal form state, and transforms it into an Item entity compatible with DatoCMS API.

When skipUnchangedFields, only the fields that changed value will be serialized.

If the required nested blocks are still not loaded, this method will return undefined.

View on Github
await ctx.formValuesToItem(ctx.formValues, false);

Takes an Item entity, and converts it into the internal form state.

View on Github
await ctx.itemToFormValues(ctx.item);

Triggers a submit form for current record.

View on Github
await ctx.saveCurrentItem();
These information describe the current state of the form that's being shown to the end-user to edit a record.

The currently active locale for the record.

View on Github

If an already persisted record is being edited, returns the full record entity.

View on Github

The model for the record being edited.

View on Github

The complete internal form state.

View on Github

The current status of the record being edited.

View on Github

Whether the form is currently submitting itself or not.

View on Github

Whether the form has some non-persisted changes or not.

View on Github

Provides information on how many blocks are currently present in the form.

View on Github

Every hook available in the Plugin SDK shares the same minumum set of properties and methods.

Information about the current user using the CMS.

The current DatoCMS user. It can either be the owner or one of the collaborators (regular or SSO).

View on Github

The role for the current DatoCMS user.

View on Github

The access token to perform API calls on behalf of the current user. Only available if currentUserAccessToken additional permission is granted.

View on Github
These methods can be used to open custom dialogs/confirmation panels.

Opens a custom modal. Returns a promise resolved with what the modal itself returns calling the resolve() function.

View on Github
const result = await ctx.openModal({
id: 'regular',
title: 'Custom title!',
width: 'l',
parameters: { foo: 'bar' },
});
if (result) {
ctx.notice(`Success! ${JSON.stringify(result)}`);
} else {
ctx.alert('Closed!');
}

Opens a UI-consistent confirmation dialog. Returns a promise resolved with the value of the choice made by the user.

View on Github
const result = await ctx.openConfirm({
title: 'Custom title',
content:
'Lorem Ipsum is simply dummy text of the printing and typesetting industry',
choices: [
{
label: 'Positive',
value: 'positive',
intent: 'positive',
},
{
label: 'Negative',
value: 'negative',
intent: 'negative',
},
],
cancel: {
label: 'Cancel',
value: false,
},
});
if (result) {
ctx.notice(`Success! ${result}`);
} else {
ctx.alert('Cancelled!');
}
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.

View on Github

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.

View on Github

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.

View on Github

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.

View on Github

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.

View on Github
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.

View on Github
const itemTypeId = prompt('Please insert a model ID:');
const item = await ctx.createNewItem(itemTypeId);
if (item) {
ctx.notice(`Success! ${item.id}`);
} else {
ctx.alert('Closed!');
}

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.

View on Github
const itemTypeId = prompt('Please insert a model ID:');
const items = await ctx.selectItem(itemTypeId, { multiple: true });
if (items) {
ctx.notice(`Success! ${items.map((i) => i.id).join(', ')}`);
} else {
ctx.alert('Closed!');
}

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.

View on Github
const itemId = prompt('Please insert a record ID:');
const item = await ctx.editItem(itemId);
if (item) {
ctx.notice(`Success! ${item.id}`);
} else {
ctx.alert('Closed!');
}
These methods can be used to asyncronously load additional information your plugin needs to work.

Loads all the fields for a specific model (or block). Fields will be returned and will also be available in the the fields property.

View on Github
const itemTypeId = prompt('Please insert a model ID:');
const fields = await ctx.loadItemTypeFields(itemTypeId);
ctx.notice(
`Success! ${fields
.map((field) => field.attributes.api_key)
.join(', ')}`,
);

Loads all the fieldsets for a specific model (or block). Fieldsets will be returned and will also be available in the the fieldsets property.

View on Github
const itemTypeId = prompt('Please insert a model ID:');
const fieldsets = await ctx.loadItemTypeFieldsets(itemTypeId);
ctx.notice(
`Success! ${fieldsets
.map((fieldset) => fieldset.attributes.title)
.join(', ')}`,
);

Loads all the fields in the project that are currently using the plugin for one of its manual field extensions.

View on Github
const fields = await ctx.loadFieldsUsingPlugin();
ctx.notice(
`Success! ${fields
.map((field) => field.attributes.api_key)
.join(', ')}`,
);

Loads all regular users. Users will be returned and will also be available in the the users property.

View on Github
const users = await ctx.loadUsers();
ctx.notice(`Success! ${users.map((user) => user.id).join(', ')}`);

Loads all SSO users. Users will be returned and will also be available in the the ssoUsers property.

View on Github
const users = await ctx.loadSsoUsers();
ctx.notice(`Success! ${users.map((user) => user.id).join(', ')}`);
These methods can be used to take the user to different pages.

Moves the user to another URL internal to the backend.

View on Github
await ctx.navigateTo('/');
Information about the current plugin. Useful to access the plugin's global configuration object.

The current plugin.

View on Github

The current DatoCMS project.

View on Github

The ID of the current environment.

View on Github

The account/organization that is the project owner.

View on Github

UI preferences of the current user (right now, only the preferred locale is available).

View on Github

An object containing the theme colors for the current DatoCMS project.

View on Github
These methods can be used to show UI-consistent toast notifications to the end-user.

Triggers an "error" toast displaying the selected message.

View on Github
const message = prompt(
'Please insert a message:',
'This is an alert message!',
);
await ctx.alert(message);

Triggers a "success" toast displaying the selected message.

View on Github
const message = prompt(
'Please insert a message:',
'This is a notice message!',
);
await ctx.notice(message);

Triggers a custom toast displaying the selected message (and optionally a CTA).

View on Github
const result = await ctx.customToast({
type: 'warning',
message: 'Just a sample warning notification!',
dismissOnPageChange: true,
dismissAfterTimeout: 5000,
cta: {
label: 'Execute call-to-action',
value: 'cta',
},
});
if (result === 'cta') {
ctx.notice(`Clicked CTA!`);
}
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.

View on Github
await ctx.updatePluginParameters({ debugMode: true });
await ctx.notice('Plugin parameters successfully updated!');

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.

View on Github
const fields = await ctx.loadFieldsUsingPlugin();
if (fields.length === 0) {
ctx.alert('No field is using this plugin as a manual extension!');
return;
}
for (const field of fields) {
const { appearance } = field.attributes;
const operations = [];
if (appearance.editor === ctx.plugin.id) {
operations.push({
operation: 'updateEditor',
newParameters: {
...appearance.parameters,
foo: 'bar',
},
});
}
appearance.addons.forEach((addon, i) => {
if (addon.id !== ctx.plugin.id) {
return;
}
operations.push({
operation: 'updateAddon',
index: i,
newParameters: { ...addon.parameters, foo: 'bar' },
});
});
await ctx.updateFieldAppearance(field.id, operations);
ctx.notice(`Successfully edited field ${field.attributes.api_key}`);
}
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.

View on Github
const item = await ctx.selectUpload({ multiple: false });
if (item) {
ctx.notice(`Success! ${item.id}`);
} else {
ctx.alert('Closed!');
}

Opens a dialog for editing a Media Area asset. It returns a promise resolved with:

  • The updated asset, if the user persists some changes to the asset itself
  • null, if the user closes the dialog without persisting any change
  • An asset structure with an additional deleted property set to true, if the user deletes the asset.
View on Github
const uploadId = prompt('Please insert an asset ID:');
const item = await ctx.editUpload(uploadId);
if (item) {
ctx.notice(`Success! ${item.id}`);
} else {
ctx.alert('Closed!');
}

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.

View on Github
const uploadId = prompt('Please insert an asset ID:');
const result = await ctx.editUploadMetadata({
upload_id: uploadId,
alt: null,
title: null,
custom_data: {},
focal_point: null,
});
if (result) {
ctx.notice(`Success! ${JSON.stringify(result)}`);
} else {
ctx.alert('Closed!');
}

executeItemFormDropdownAction(actionId: string, ctx)

Use this function to execute a particular dropdown action defined via the itemFormDropdownActions() hook.

Return value

The function must return: Promise<void>.

Context object

The following properties and methods are available in the ctx argument:

This hook exposes additional information and operations specific to the context in which it operates.

These methods can be used to interact with the form that's being shown to the end-user to edit a record.

Hides/shows a specific field in the form. Please be aware that when a field is hidden, the field editor for that field will be removed from the DOM itself, including any associated plugins. When it is shown again, its plugins will be reinitialized.

View on Github
const fieldPath = prompt(
'Please insert the path of a field in the form',
ctx.fieldPath,
);
await ctx.toggleField(fieldPath, true);

Disables/re-enables a specific field in the form.

View on Github
const fieldPath = prompt(
'Please insert the path of a field in the form',
ctx.fieldPath,
);
await ctx.disableField(fieldPath, true);

Smoothly navigates to a specific field in the form. If the field is localized it will switch language tab and then navigate to the chosen field.

View on Github
const fieldPath = prompt(
'Please insert the path of a field in the form',
ctx.fieldPath,
);
await ctx.scrollToField(fieldPath);

Changes a specific path of the formValues object.

View on Github
const fieldPath = prompt(
'Please insert the path of a field in the form',
ctx.fieldPath,
);
await ctx.setFieldValue(fieldPath, 'new value');

Takes the internal form state, and transforms it into an Item entity compatible with DatoCMS API.

When skipUnchangedFields, only the fields that changed value will be serialized.

If the required nested blocks are still not loaded, this method will return undefined.

View on Github
await ctx.formValuesToItem(ctx.formValues, false);

Takes an Item entity, and converts it into the internal form state.

View on Github
await ctx.itemToFormValues(ctx.item);

Triggers a submit form for current record.

View on Github
await ctx.saveCurrentItem();
These information describe the current state of the form that's being shown to the end-user to edit a record.

The currently active locale for the record.

View on Github

If an already persisted record is being edited, returns the full record entity.

View on Github

The model for the record being edited.

View on Github

The complete internal form state.

View on Github

The current status of the record being edited.

View on Github

Whether the form is currently submitting itself or not.

View on Github

Whether the form has some non-persisted changes or not.

View on Github

Provides information on how many blocks are currently present in the form.

View on Github

Every hook available in the Plugin SDK shares the same minumum set of properties and methods.

Information about the current user using the CMS.

The current DatoCMS user. It can either be the owner or one of the collaborators (regular or SSO).

View on Github

The role for the current DatoCMS user.

View on Github

The access token to perform API calls on behalf of the current user. Only available if currentUserAccessToken additional permission is granted.

View on Github
These methods can be used to open custom dialogs/confirmation panels.

Opens a custom modal. Returns a promise resolved with what the modal itself returns calling the resolve() function.

View on Github
const result = await ctx.openModal({
id: 'regular',
title: 'Custom title!',
width: 'l',
parameters: { foo: 'bar' },
});
if (result) {
ctx.notice(`Success! ${JSON.stringify(result)}`);
} else {
ctx.alert('Closed!');
}

Opens a UI-consistent confirmation dialog. Returns a promise resolved with the value of the choice made by the user.

View on Github
const result = await ctx.openConfirm({
title: 'Custom title',
content:
'Lorem Ipsum is simply dummy text of the printing and typesetting industry',
choices: [
{
label: 'Positive',
value: 'positive',
intent: 'positive',
},
{
label: 'Negative',
value: 'negative',
intent: 'negative',
},
],
cancel: {
label: 'Cancel',
value: false,
},
});
if (result) {
ctx.notice(`Success! ${result}`);
} else {
ctx.alert('Cancelled!');
}
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.

View on Github

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.

View on Github

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.

View on Github

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.

View on Github

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.

View on Github
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.

View on Github
const itemTypeId = prompt('Please insert a model ID:');
const item = await ctx.createNewItem(itemTypeId);
if (item) {
ctx.notice(`Success! ${item.id}`);
} else {
ctx.alert('Closed!');
}

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.

View on Github
const itemTypeId = prompt('Please insert a model ID:');
const items = await ctx.selectItem(itemTypeId, { multiple: true });
if (items) {
ctx.notice(`Success! ${items.map((i) => i.id).join(', ')}`);
} else {
ctx.alert('Closed!');
}

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.

View on Github
const itemId = prompt('Please insert a record ID:');
const item = await ctx.editItem(itemId);
if (item) {
ctx.notice(`Success! ${item.id}`);
} else {
ctx.alert('Closed!');
}
These methods can be used to asyncronously load additional information your plugin needs to work.

Loads all the fields for a specific model (or block). Fields will be returned and will also be available in the the fields property.

View on Github
const itemTypeId = prompt('Please insert a model ID:');
const fields = await ctx.loadItemTypeFields(itemTypeId);
ctx.notice(
`Success! ${fields
.map((field) => field.attributes.api_key)
.join(', ')}`,
);

Loads all the fieldsets for a specific model (or block). Fieldsets will be returned and will also be available in the the fieldsets property.

View on Github
const itemTypeId = prompt('Please insert a model ID:');
const fieldsets = await ctx.loadItemTypeFieldsets(itemTypeId);
ctx.notice(
`Success! ${fieldsets
.map((fieldset) => fieldset.attributes.title)
.join(', ')}`,
);

Loads all the fields in the project that are currently using the plugin for one of its manual field extensions.

View on Github
const fields = await ctx.loadFieldsUsingPlugin();
ctx.notice(
`Success! ${fields
.map((field) => field.attributes.api_key)
.join(', ')}`,
);

Loads all regular users. Users will be returned and will also be available in the the users property.

View on Github
const users = await ctx.loadUsers();
ctx.notice(`Success! ${users.map((user) => user.id).join(', ')}`);

Loads all SSO users. Users will be returned and will also be available in the the ssoUsers property.

View on Github
const users = await ctx.loadSsoUsers();
ctx.notice(`Success! ${users.map((user) => user.id).join(', ')}`);
These methods can be used to take the user to different pages.

Moves the user to another URL internal to the backend.

View on Github
await ctx.navigateTo('/');
Information about the current plugin. Useful to access the plugin's global configuration object.

The current plugin.

View on Github

The current DatoCMS project.

View on Github

The ID of the current environment.

View on Github

The account/organization that is the project owner.

View on Github

UI preferences of the current user (right now, only the preferred locale is available).

View on Github

An object containing the theme colors for the current DatoCMS project.

View on Github
These methods can be used to show UI-consistent toast notifications to the end-user.

Triggers an "error" toast displaying the selected message.

View on Github
const message = prompt(
'Please insert a message:',
'This is an alert message!',
);
await ctx.alert(message);

Triggers a "success" toast displaying the selected message.

View on Github
const message = prompt(
'Please insert a message:',
'This is a notice message!',
);
await ctx.notice(message);

Triggers a custom toast displaying the selected message (and optionally a CTA).

View on Github
const result = await ctx.customToast({
type: 'warning',
message: 'Just a sample warning notification!',
dismissOnPageChange: true,
dismissAfterTimeout: 5000,
cta: {
label: 'Execute call-to-action',
value: 'cta',
},
});
if (result === 'cta') {
ctx.notice(`Clicked CTA!`);
}
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.

View on Github
await ctx.updatePluginParameters({ debugMode: true });
await ctx.notice('Plugin parameters successfully updated!');

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.

View on Github
const fields = await ctx.loadFieldsUsingPlugin();
if (fields.length === 0) {
ctx.alert('No field is using this plugin as a manual extension!');
return;
}
for (const field of fields) {
const { appearance } = field.attributes;
const operations = [];
if (appearance.editor === ctx.plugin.id) {
operations.push({
operation: 'updateEditor',
newParameters: {
...appearance.parameters,
foo: 'bar',
},
});
}
appearance.addons.forEach((addon, i) => {
if (addon.id !== ctx.plugin.id) {
return;
}
operations.push({
operation: 'updateAddon',
index: i,
newParameters: { ...addon.parameters, foo: 'bar' },
});
});
await ctx.updateFieldAppearance(field.id, operations);
ctx.notice(`Successfully edited field ${field.attributes.api_key}`);
}
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.

View on Github
const item = await ctx.selectUpload({ multiple: false });
if (item) {
ctx.notice(`Success! ${item.id}`);
} else {
ctx.alert('Closed!');
}

Opens a dialog for editing a Media Area asset. It returns a promise resolved with:

  • The updated asset, if the user persists some changes to the asset itself
  • null, if the user closes the dialog without persisting any change
  • An asset structure with an additional deleted property set to true, if the user deletes the asset.
View on Github
const uploadId = prompt('Please insert an asset ID:');
const item = await ctx.editUpload(uploadId);
if (item) {
ctx.notice(`Success! ${item.id}`);
} else {
ctx.alert('Closed!');
}

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.

View on Github
const uploadId = prompt('Please insert an asset ID:');
const result = await ctx.editUploadMetadata({
upload_id: uploadId,
alt: null,
title: null,
custom_data: {},
focal_point: null,
});
if (result) {
ctx.notice(`Success! ${JSON.stringify(result)}`);
} else {
ctx.alert('Closed!');
}

executeItemsDropdownAction(actionId: string, items: Item[], ctx)

Use this function to execute a particular dropdown action defined via the itemsDropdownActions() hook.

Return value

The function must return: Promise<void>.

Context object

The following properties and methods are available in the ctx argument:

This hook exposes additional information and operations specific to the context in which it operates.

Every hook available in the Plugin SDK shares the same minumum set of properties and methods.

Information about the current user using the CMS.

The current DatoCMS user. It can either be the owner or one of the collaborators (regular or SSO).

View on Github

The role for the current DatoCMS user.

View on Github

The access token to perform API calls on behalf of the current user. Only available if currentUserAccessToken additional permission is granted.

View on Github
These methods can be used to open custom dialogs/confirmation panels.

Opens a custom modal. Returns a promise resolved with what the modal itself returns calling the resolve() function.

View on Github
const result = await ctx.openModal({
id: 'regular',
title: 'Custom title!',
width: 'l',
parameters: { foo: 'bar' },
});
if (result) {
ctx.notice(`Success! ${JSON.stringify(result)}`);
} else {
ctx.alert('Closed!');
}

Opens a UI-consistent confirmation dialog. Returns a promise resolved with the value of the choice made by the user.

View on Github
const result = await ctx.openConfirm({
title: 'Custom title',
content:
'Lorem Ipsum is simply dummy text of the printing and typesetting industry',
choices: [
{
label: 'Positive',
value: 'positive',
intent: 'positive',
},
{
label: 'Negative',
value: 'negative',
intent: 'negative',
},
],
cancel: {
label: 'Cancel',
value: false,
},
});
if (result) {
ctx.notice(`Success! ${result}`);
} else {
ctx.alert('Cancelled!');
}
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.

View on Github

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.

View on Github

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.

View on Github

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.

View on Github

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.

View on Github
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.

View on Github
const itemTypeId = prompt('Please insert a model ID:');
const item = await ctx.createNewItem(itemTypeId);
if (item) {
ctx.notice(`Success! ${item.id}`);
} else {
ctx.alert('Closed!');
}

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.

View on Github
const itemTypeId = prompt('Please insert a model ID:');
const items = await ctx.selectItem(itemTypeId, { multiple: true });
if (items) {
ctx.notice(`Success! ${items.map((i) => i.id).join(', ')}`);
} else {
ctx.alert('Closed!');
}

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.

View on Github
const itemId = prompt('Please insert a record ID:');
const item = await ctx.editItem(itemId);
if (item) {
ctx.notice(`Success! ${item.id}`);
} else {
ctx.alert('Closed!');
}
These methods can be used to asyncronously load additional information your plugin needs to work.

Loads all the fields for a specific model (or block). Fields will be returned and will also be available in the the fields property.

View on Github
const itemTypeId = prompt('Please insert a model ID:');
const fields = await ctx.loadItemTypeFields(itemTypeId);
ctx.notice(
`Success! ${fields
.map((field) => field.attributes.api_key)
.join(', ')}`,
);

Loads all the fieldsets for a specific model (or block). Fieldsets will be returned and will also be available in the the fieldsets property.

View on Github
const itemTypeId = prompt('Please insert a model ID:');
const fieldsets = await ctx.loadItemTypeFieldsets(itemTypeId);
ctx.notice(
`Success! ${fieldsets
.map((fieldset) => fieldset.attributes.title)
.join(', ')}`,
);

Loads all the fields in the project that are currently using the plugin for one of its manual field extensions.

View on Github
const fields = await ctx.loadFieldsUsingPlugin();
ctx.notice(
`Success! ${fields
.map((field) => field.attributes.api_key)
.join(', ')}`,
);

Loads all regular users. Users will be returned and will also be available in the the users property.

View on Github
const users = await ctx.loadUsers();
ctx.notice(`Success! ${users.map((user) => user.id).join(', ')}`);

Loads all SSO users. Users will be returned and will also be available in the the ssoUsers property.

View on Github
const users = await ctx.loadSsoUsers();
ctx.notice(`Success! ${users.map((user) => user.id).join(', ')}`);
These methods can be used to take the user to different pages.

Moves the user to another URL internal to the backend.

View on Github
await ctx.navigateTo('/');
Information about the current plugin. Useful to access the plugin's global configuration object.

The current plugin.

View on Github

The current DatoCMS project.

View on Github

The ID of the current environment.

View on Github

The account/organization that is the project owner.

View on Github

UI preferences of the current user (right now, only the preferred locale is available).

View on Github

An object containing the theme colors for the current DatoCMS project.

View on Github
These methods can be used to show UI-consistent toast notifications to the end-user.

Triggers an "error" toast displaying the selected message.

View on Github
const message = prompt(
'Please insert a message:',
'This is an alert message!',
);
await ctx.alert(message);

Triggers a "success" toast displaying the selected message.

View on Github
const message = prompt(
'Please insert a message:',
'This is a notice message!',
);
await ctx.notice(message);

Triggers a custom toast displaying the selected message (and optionally a CTA).

View on Github
const result = await ctx.customToast({
type: 'warning',
message: 'Just a sample warning notification!',
dismissOnPageChange: true,
dismissAfterTimeout: 5000,
cta: {
label: 'Execute call-to-action',
value: 'cta',
},
});
if (result === 'cta') {
ctx.notice(`Clicked CTA!`);
}
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.

View on Github
await ctx.updatePluginParameters({ debugMode: true });
await ctx.notice('Plugin parameters successfully updated!');

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.

View on Github
const fields = await ctx.loadFieldsUsingPlugin();
if (fields.length === 0) {
ctx.alert('No field is using this plugin as a manual extension!');
return;
}
for (const field of fields) {
const { appearance } = field.attributes;
const operations = [];
if (appearance.editor === ctx.plugin.id) {
operations.push({
operation: 'updateEditor',
newParameters: {
...appearance.parameters,
foo: 'bar',
},
});
}
appearance.addons.forEach((addon, i) => {
if (addon.id !== ctx.plugin.id) {
return;
}
operations.push({
operation: 'updateAddon',
index: i,
newParameters: { ...addon.parameters, foo: 'bar' },
});
});
await ctx.updateFieldAppearance(field.id, operations);
ctx.notice(`Successfully edited field ${field.attributes.api_key}`);
}
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.

View on Github
const item = await ctx.selectUpload({ multiple: false });
if (item) {
ctx.notice(`Success! ${item.id}`);
} else {
ctx.alert('Closed!');
}

Opens a dialog for editing a Media Area asset. It returns a promise resolved with:

  • The updated asset, if the user persists some changes to the asset itself
  • null, if the user closes the dialog without persisting any change
  • An asset structure with an additional deleted property set to true, if the user deletes the asset.
View on Github
const uploadId = prompt('Please insert an asset ID:');
const item = await ctx.editUpload(uploadId);
if (item) {
ctx.notice(`Success! ${item.id}`);
} else {
ctx.alert('Closed!');
}

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.

View on Github
const uploadId = prompt('Please insert an asset ID:');
const result = await ctx.editUploadMetadata({
upload_id: uploadId,
alt: null,
title: null,
custom_data: {},
focal_point: null,
});
if (result) {
ctx.notice(`Success! ${JSON.stringify(result)}`);
} else {
ctx.alert('Closed!');
}

executeUploadsDropdownAction(actionId: string, uploads: Upload[], ctx)

Use this function to execute a particular dropdown action defined via the uploadsDropdownActions() hook.

Return value

The function must return: Promise<void>.

Context object

The following properties and methods are available in the ctx argument:

This hook exposes additional information and operations specific to the context in which it operates.

Every hook available in the Plugin SDK shares the same minumum set of properties and methods.

Information about the current user using the CMS.

The current DatoCMS user. It can either be the owner or one of the collaborators (regular or SSO).

View on Github

The role for the current DatoCMS user.

View on Github

The access token to perform API calls on behalf of the current user. Only available if currentUserAccessToken additional permission is granted.

View on Github
These methods can be used to open custom dialogs/confirmation panels.

Opens a custom modal. Returns a promise resolved with what the modal itself returns calling the resolve() function.

View on Github
const result = await ctx.openModal({
id: 'regular',
title: 'Custom title!',
width: 'l',
parameters: { foo: 'bar' },
});
if (result) {
ctx.notice(`Success! ${JSON.stringify(result)}`);
} else {
ctx.alert('Closed!');
}

Opens a UI-consistent confirmation dialog. Returns a promise resolved with the value of the choice made by the user.

View on Github
const result = await ctx.openConfirm({
title: 'Custom title',
content:
'Lorem Ipsum is simply dummy text of the printing and typesetting industry',
choices: [
{
label: 'Positive',
value: 'positive',
intent: 'positive',
},
{
label: 'Negative',
value: 'negative',
intent: 'negative',
},
],
cancel: {
label: 'Cancel',
value: false,
},
});
if (result) {
ctx.notice(`Success! ${result}`);
} else {
ctx.alert('Cancelled!');
}
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.

View on Github

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.

View on Github

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.

View on Github

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.

View on Github

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.

View on Github
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.

View on Github
const itemTypeId = prompt('Please insert a model ID:');
const item = await ctx.createNewItem(itemTypeId);
if (item) {
ctx.notice(`Success! ${item.id}`);
} else {
ctx.alert('Closed!');
}

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.

View on Github
const itemTypeId = prompt('Please insert a model ID:');
const items = await ctx.selectItem(itemTypeId, { multiple: true });
if (items) {
ctx.notice(`Success! ${items.map((i) => i.id).join(', ')}`);
} else {
ctx.alert('Closed!');
}

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.

View on Github
const itemId = prompt('Please insert a record ID:');
const item = await ctx.editItem(itemId);
if (item) {
ctx.notice(`Success! ${item.id}`);
} else {
ctx.alert('Closed!');
}
These methods can be used to asyncronously load additional information your plugin needs to work.

Loads all the fields for a specific model (or block). Fields will be returned and will also be available in the the fields property.

View on Github
const itemTypeId = prompt('Please insert a model ID:');
const fields = await ctx.loadItemTypeFields(itemTypeId);
ctx.notice(
`Success! ${fields
.map((field) => field.attributes.api_key)
.join(', ')}`,
);

Loads all the fieldsets for a specific model (or block). Fieldsets will be returned and will also be available in the the fieldsets property.

View on Github
const itemTypeId = prompt('Please insert a model ID:');
const fieldsets = await ctx.loadItemTypeFieldsets(itemTypeId);
ctx.notice(
`Success! ${fieldsets
.map((fieldset) => fieldset.attributes.title)
.join(', ')}`,
);

Loads all the fields in the project that are currently using the plugin for one of its manual field extensions.

View on Github
const fields = await ctx.loadFieldsUsingPlugin();
ctx.notice(
`Success! ${fields
.map((field) => field.attributes.api_key)
.join(', ')}`,
);

Loads all regular users. Users will be returned and will also be available in the the users property.

View on Github
const users = await ctx.loadUsers();
ctx.notice(`Success! ${users.map((user) => user.id).join(', ')}`);

Loads all SSO users. Users will be returned and will also be available in the the ssoUsers property.

View on Github
const users = await ctx.loadSsoUsers();
ctx.notice(`Success! ${users.map((user) => user.id).join(', ')}`);
These methods can be used to take the user to different pages.

Moves the user to another URL internal to the backend.

View on Github
await ctx.navigateTo('/');
Information about the current plugin. Useful to access the plugin's global configuration object.

The current plugin.

View on Github

The current DatoCMS project.

View on Github

The ID of the current environment.

View on Github

The account/organization that is the project owner.

View on Github

UI preferences of the current user (right now, only the preferred locale is available).

View on Github

An object containing the theme colors for the current DatoCMS project.

View on Github
These methods can be used to show UI-consistent toast notifications to the end-user.

Triggers an "error" toast displaying the selected message.

View on Github
const message = prompt(
'Please insert a message:',
'This is an alert message!',
);
await ctx.alert(message);

Triggers a "success" toast displaying the selected message.

View on Github
const message = prompt(
'Please insert a message:',
'This is a notice message!',
);
await ctx.notice(message);

Triggers a custom toast displaying the selected message (and optionally a CTA).

View on Github
const result = await ctx.customToast({
type: 'warning',
message: 'Just a sample warning notification!',
dismissOnPageChange: true,
dismissAfterTimeout: 5000,
cta: {
label: 'Execute call-to-action',
value: 'cta',
},
});
if (result === 'cta') {
ctx.notice(`Clicked CTA!`);
}
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.

View on Github
await ctx.updatePluginParameters({ debugMode: true });
await ctx.notice('Plugin parameters successfully updated!');

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.

View on Github
const fields = await ctx.loadFieldsUsingPlugin();
if (fields.length === 0) {
ctx.alert('No field is using this plugin as a manual extension!');
return;
}
for (const field of fields) {
const { appearance } = field.attributes;
const operations = [];
if (appearance.editor === ctx.plugin.id) {
operations.push({
operation: 'updateEditor',
newParameters: {
...appearance.parameters,
foo: 'bar',
},
});
}
appearance.addons.forEach((addon, i) => {
if (addon.id !== ctx.plugin.id) {
return;
}
operations.push({
operation: 'updateAddon',
index: i,
newParameters: { ...addon.parameters, foo: 'bar' },
});
});
await ctx.updateFieldAppearance(field.id, operations);
ctx.notice(`Successfully edited field ${field.attributes.api_key}`);
}
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.

View on Github
const item = await ctx.selectUpload({ multiple: false });
if (item) {
ctx.notice(`Success! ${item.id}`);
} else {
ctx.alert('Closed!');
}

Opens a dialog for editing a Media Area asset. It returns a promise resolved with:

  • The updated asset, if the user persists some changes to the asset itself
  • null, if the user closes the dialog without persisting any change
  • An asset structure with an additional deleted property set to true, if the user deletes the asset.
View on Github
const uploadId = prompt('Please insert an asset ID:');
const item = await ctx.editUpload(uploadId);
if (item) {
ctx.notice(`Success! ${item.id}`);
} else {
ctx.alert('Closed!');
}

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.

View on Github
const uploadId = prompt('Please insert an asset ID:');
const result = await ctx.editUploadMetadata({
upload_id: uploadId,
alt: null,
title: null,
custom_data: {},
focal_point: null,
});
if (result) {
ctx.notice(`Success! ${JSON.stringify(result)}`);
} else {
ctx.alert('Closed!');
}

fieldDropdownActions(field: Field, ctx)

Use this function to define custom actions (or groups of actions) to be displayed at the individual field level in the record editing form.

The executeFieldDropdownAction() hook will be triggered once the user clicks on one of the defined actions.

Return value

The function must return: Array<DropdownAction | DropdownActionGroup>.

Context object

The following properties and methods are available in the ctx argument:

This hook exposes additional information and operations specific to the context in which it operates.

These information describe the current state of the field where this plugin is applied to.

Whether the field is currently disabled or not.

View on Github

The path in the formValues object where to find the current value for the field.

View on Github

The field where the field extension is installed to.

View on Github

If the field extension is installed in a field of a block, returns the top level Modular Content/Structured Text field containing the block itself.

View on Github

If the field extension is installed in a field of a block, returns the ID of the block — or undefined if the block is still not persisted — and the block model.

View on Github
These information describe the current state of the form that's being shown to the end-user to edit a record.

The currently active locale for the record.

View on Github

If an already persisted record is being edited, returns the full record entity.

View on Github

The model for the record being edited.

View on Github

The complete internal form state.

View on Github

The current status of the record being edited.

View on Github

Whether the form is currently submitting itself or not.

View on Github

Whether the form has some non-persisted changes or not.

View on Github

Provides information on how many blocks are currently present in the form.

View on Github

Every hook available in the Plugin SDK shares the same minumum set of properties and methods.

Information about the current user using the CMS.

The current DatoCMS user. It can either be the owner or one of the collaborators (regular or SSO).

View on Github

The role for the current DatoCMS user.

View on Github

The access token to perform API calls on behalf of the current user. Only available if currentUserAccessToken additional permission is granted.

View on Github
These methods can be used to open custom dialogs/confirmation panels.

Opens a custom modal. Returns a promise resolved with what the modal itself returns calling the resolve() function.

View on Github
const result = await ctx.openModal({
id: 'regular',
title: 'Custom title!',
width: 'l',
parameters: { foo: 'bar' },
});
if (result) {
ctx.notice(`Success! ${JSON.stringify(result)}`);
} else {
ctx.alert('Closed!');
}

Opens a UI-consistent confirmation dialog. Returns a promise resolved with the value of the choice made by the user.

View on Github
const result = await ctx.openConfirm({
title: 'Custom title',
content:
'Lorem Ipsum is simply dummy text of the printing and typesetting industry',
choices: [
{
label: 'Positive',
value: 'positive',
intent: 'positive',
},
{
label: 'Negative',
value: 'negative',
intent: 'negative',
},
],
cancel: {
label: 'Cancel',
value: false,
},
});
if (result) {
ctx.notice(`Success! ${result}`);
} else {
ctx.alert('Cancelled!');
}
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.

View on Github

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.

View on Github

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.

View on Github

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.

View on Github

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.

View on Github
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.

View on Github
const itemTypeId = prompt('Please insert a model ID:');
const item = await ctx.createNewItem(itemTypeId);
if (item) {
ctx.notice(`Success! ${item.id}`);
} else {
ctx.alert('Closed!');
}

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.

View on Github
const itemTypeId = prompt('Please insert a model ID:');
const items = await ctx.selectItem(itemTypeId, { multiple: true });
if (items) {
ctx.notice(`Success! ${items.map((i) => i.id).join(', ')}`);
} else {
ctx.alert('Closed!');
}

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.

View on Github
const itemId = prompt('Please insert a record ID:');
const item = await ctx.editItem(itemId);
if (item) {
ctx.notice(`Success! ${item.id}`);
} else {
ctx.alert('Closed!');
}
These methods can be used to asyncronously load additional information your plugin needs to work.

Loads all the fields for a specific model (or block). Fields will be returned and will also be available in the the fields property.

View on Github
const itemTypeId = prompt('Please insert a model ID:');
const fields = await ctx.loadItemTypeFields(itemTypeId);
ctx.notice(
`Success! ${fields
.map((field) => field.attributes.api_key)
.join(', ')}`,
);

Loads all the fieldsets for a specific model (or block). Fieldsets will be returned and will also be available in the the fieldsets property.

View on Github
const itemTypeId = prompt('Please insert a model ID:');
const fieldsets = await ctx.loadItemTypeFieldsets(itemTypeId);
ctx.notice(
`Success! ${fieldsets
.map((fieldset) => fieldset.attributes.title)
.join(', ')}`,
);

Loads all the fields in the project that are currently using the plugin for one of its manual field extensions.

View on Github
const fields = await ctx.loadFieldsUsingPlugin();
ctx.notice(
`Success! ${fields
.map((field) => field.attributes.api_key)
.join(', ')}`,
);

Loads all regular users. Users will be returned and will also be available in the the users property.

View on Github
const users = await ctx.loadUsers();
ctx.notice(`Success! ${users.map((user) => user.id).join(', ')}`);

Loads all SSO users. Users will be returned and will also be available in the the ssoUsers property.

View on Github
const users = await ctx.loadSsoUsers();
ctx.notice(`Success! ${users.map((user) => user.id).join(', ')}`);
These methods can be used to take the user to different pages.

Moves the user to another URL internal to the backend.

View on Github
await ctx.navigateTo('/');
Information about the current plugin. Useful to access the plugin's global configuration object.

The current plugin.

View on Github

The current DatoCMS project.

View on Github

The ID of the current environment.

View on Github

The account/organization that is the project owner.

View on Github

UI preferences of the current user (right now, only the preferred locale is available).

View on Github

An object containing the theme colors for the current DatoCMS project.

View on Github
These methods can be used to show UI-consistent toast notifications to the end-user.

Triggers an "error" toast displaying the selected message.

View on Github
const message = prompt(
'Please insert a message:',
'This is an alert message!',
);
await ctx.alert(message);

Triggers a "success" toast displaying the selected message.

View on Github
const message = prompt(
'Please insert a message:',
'This is a notice message!',
);
await ctx.notice(message);

Triggers a custom toast displaying the selected message (and optionally a CTA).

View on Github
const result = await ctx.customToast({
type: 'warning',
message: 'Just a sample warning notification!',
dismissOnPageChange: true,
dismissAfterTimeout: 5000,
cta: {
label: 'Execute call-to-action',
value: 'cta',
},
});
if (result === 'cta') {
ctx.notice(`Clicked CTA!`);
}
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.

View on Github
await ctx.updatePluginParameters({ debugMode: true });
await ctx.notice('Plugin parameters successfully updated!');

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.

View on Github
const fields = await ctx.loadFieldsUsingPlugin();
if (fields.length === 0) {
ctx.alert('No field is using this plugin as a manual extension!');
return;
}
for (const field of fields) {
const { appearance } = field.attributes;
const operations = [];
if (appearance.editor === ctx.plugin.id) {
operations.push({
operation: 'updateEditor',
newParameters: {
...appearance.parameters,
foo: 'bar',
},
});
}
appearance.addons.forEach((addon, i) => {
if (addon.id !== ctx.plugin.id) {
return;
}
operations.push({
operation: 'updateAddon',
index: i,
newParameters: { ...addon.parameters, foo: 'bar' },
});
});
await ctx.updateFieldAppearance(field.id, operations);
ctx.notice(`Successfully edited field ${field.attributes.api_key}`);
}
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.

View on Github
const item = await ctx.selectUpload({ multiple: false });
if (item) {
ctx.notice(`Success! ${item.id}`);
} else {
ctx.alert('Closed!');
}

Opens a dialog for editing a Media Area asset. It returns a promise resolved with:

  • The updated asset, if the user persists some changes to the asset itself
  • null, if the user closes the dialog without persisting any change
  • An asset structure with an additional deleted property set to true, if the user deletes the asset.
View on Github
const uploadId = prompt('Please insert an asset ID:');
const item = await ctx.editUpload(uploadId);
if (item) {
ctx.notice(`Success! ${item.id}`);
} else {
ctx.alert('Closed!');
}

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.

View on Github
const uploadId = prompt('Please insert an asset ID:');
const result = await ctx.editUploadMetadata({
upload_id: uploadId,
alt: null,
title: null,
custom_data: {},
focal_point: null,
});
if (result) {
ctx.notice(`Success! ${JSON.stringify(result)}`);
} else {
ctx.alert('Closed!');
}

itemFormDropdownActions(itemType: ItemType, ctx)

Use this function to define custom actions (or groups of actions) to be displayed at when editing a particular record.

The executeItemFormDropdownAction() hook will be triggered once the user clicks on one of the defined actions.

Return value

The function must return: Array<DropdownAction | DropdownActionGroup>.

Context object

The following properties and methods are available in the ctx argument:

This hook exposes additional information and operations specific to the context in which it operates.

These information describe the current state of the form that's being shown to the end-user to edit a record.

The currently active locale for the record.

View on Github

If an already persisted record is being edited, returns the full record entity.

View on Github

The model for the record being edited.

View on Github

The complete internal form state.

View on Github

The current status of the record being edited.

View on Github

Whether the form is currently submitting itself or not.

View on Github

Whether the form has some non-persisted changes or not.

View on Github

Provides information on how many blocks are currently present in the form.

View on Github

Every hook available in the Plugin SDK shares the same minumum set of properties and methods.

Information about the current user using the CMS.

The current DatoCMS user. It can either be the owner or one of the collaborators (regular or SSO).

View on Github

The role for the current DatoCMS user.

View on Github

The access token to perform API calls on behalf of the current user. Only available if currentUserAccessToken additional permission is granted.

View on Github
These methods can be used to open custom dialogs/confirmation panels.

Opens a custom modal. Returns a promise resolved with what the modal itself returns calling the resolve() function.

View on Github
const result = await ctx.openModal({
id: 'regular',
title: 'Custom title!',
width: 'l',
parameters: { foo: 'bar' },
});
if (result) {
ctx.notice(`Success! ${JSON.stringify(result)}`);
} else {
ctx.alert('Closed!');
}

Opens a UI-consistent confirmation dialog. Returns a promise resolved with the value of the choice made by the user.

View on Github
const result = await ctx.openConfirm({
title: 'Custom title',
content:
'Lorem Ipsum is simply dummy text of the printing and typesetting industry',
choices: [
{
label: 'Positive',
value: 'positive',
intent: 'positive',
},
{
label: 'Negative',
value: 'negative',
intent: 'negative',
},
],
cancel: {
label: 'Cancel',
value: false,
},
});
if (result) {
ctx.notice(`Success! ${result}`);
} else {
ctx.alert('Cancelled!');
}
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.

View on Github

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.

View on Github

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.

View on Github

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.

View on Github

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.

View on Github
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.

View on Github
const itemTypeId = prompt('Please insert a model ID:');
const item = await ctx.createNewItem(itemTypeId);
if (item) {
ctx.notice(`Success! ${item.id}`);
} else {
ctx.alert('Closed!');
}

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.

View on Github
const itemTypeId = prompt('Please insert a model ID:');
const items = await ctx.selectItem(itemTypeId, { multiple: true });
if (items) {
ctx.notice(`Success! ${items.map((i) => i.id).join(', ')}`);
} else {
ctx.alert('Closed!');
}

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.

View on Github
const itemId = prompt('Please insert a record ID:');
const item = await ctx.editItem(itemId);
if (item) {
ctx.notice(`Success! ${item.id}`);
} else {
ctx.alert('Closed!');
}
These methods can be used to asyncronously load additional information your plugin needs to work.

Loads all the fields for a specific model (or block). Fields will be returned and will also be available in the the fields property.

View on Github
const itemTypeId = prompt('Please insert a model ID:');
const fields = await ctx.loadItemTypeFields(itemTypeId);
ctx.notice(
`Success! ${fields
.map((field) => field.attributes.api_key)
.join(', ')}`,
);

Loads all the fieldsets for a specific model (or block). Fieldsets will be returned and will also be available in the the fieldsets property.

View on Github
const itemTypeId = prompt('Please insert a model ID:');
const fieldsets = await ctx.loadItemTypeFieldsets(itemTypeId);
ctx.notice(
`Success! ${fieldsets
.map((fieldset) => fieldset.attributes.title)
.join(', ')}`,
);

Loads all the fields in the project that are currently using the plugin for one of its manual field extensions.

View on Github
const fields = await ctx.loadFieldsUsingPlugin();
ctx.notice(
`Success! ${fields
.map((field) => field.attributes.api_key)
.join(', ')}`,
);

Loads all regular users. Users will be returned and will also be available in the the users property.

View on Github
const users = await ctx.loadUsers();
ctx.notice(`Success! ${users.map((user) => user.id).join(', ')}`);

Loads all SSO users. Users will be returned and will also be available in the the ssoUsers property.

View on Github
const users = await ctx.loadSsoUsers();
ctx.notice(`Success! ${users.map((user) => user.id).join(', ')}`);
These methods can be used to take the user to different pages.

Moves the user to another URL internal to the backend.

View on Github
await ctx.navigateTo('/');
Information about the current plugin. Useful to access the plugin's global configuration object.

The current plugin.

View on Github

The current DatoCMS project.

View on Github

The ID of the current environment.

View on Github

The account/organization that is the project owner.

View on Github

UI preferences of the current user (right now, only the preferred locale is available).

View on Github

An object containing the theme colors for the current DatoCMS project.

View on Github
These methods can be used to show UI-consistent toast notifications to the end-user.

Triggers an "error" toast displaying the selected message.

View on Github
const message = prompt(
'Please insert a message:',
'This is an alert message!',
);
await ctx.alert(message);

Triggers a "success" toast displaying the selected message.

View on Github
const message = prompt(
'Please insert a message:',
'This is a notice message!',
);
await ctx.notice(message);

Triggers a custom toast displaying the selected message (and optionally a CTA).

View on Github
const result = await ctx.customToast({
type: 'warning',
message: 'Just a sample warning notification!',
dismissOnPageChange: true,
dismissAfterTimeout: 5000,
cta: {
label: 'Execute call-to-action',
value: 'cta',
},
});
if (result === 'cta') {
ctx.notice(`Clicked CTA!`);
}
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.

View on Github
await ctx.updatePluginParameters({ debugMode: true });
await ctx.notice('Plugin parameters successfully updated!');

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.

View on Github
const fields = await ctx.loadFieldsUsingPlugin();
if (fields.length === 0) {
ctx.alert('No field is using this plugin as a manual extension!');
return;
}
for (const field of fields) {
const { appearance } = field.attributes;
const operations = [];
if (appearance.editor === ctx.plugin.id) {
operations.push({
operation: 'updateEditor',
newParameters: {
...appearance.parameters,
foo: 'bar',
},
});
}
appearance.addons.forEach((addon, i) => {
if (addon.id !== ctx.plugin.id) {
return;
}
operations.push({
operation: 'updateAddon',
index: i,
newParameters: { ...addon.parameters, foo: 'bar' },
});
});
await ctx.updateFieldAppearance(field.id, operations);
ctx.notice(`Successfully edited field ${field.attributes.api_key}`);
}
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.

View on Github
const item = await ctx.selectUpload({ multiple: false });
if (item) {
ctx.notice(`Success! ${item.id}`);
} else {
ctx.alert('Closed!');
}

Opens a dialog for editing a Media Area asset. It returns a promise resolved with:

  • The updated asset, if the user persists some changes to the asset itself
  • null, if the user closes the dialog without persisting any change
  • An asset structure with an additional deleted property set to true, if the user deletes the asset.
View on Github
const uploadId = prompt('Please insert an asset ID:');
const item = await ctx.editUpload(uploadId);
if (item) {
ctx.notice(`Success! ${item.id}`);
} else {
ctx.alert('Closed!');
}

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.

View on Github
const uploadId = prompt('Please insert an asset ID:');
const result = await ctx.editUploadMetadata({
upload_id: uploadId,
alt: null,
title: null,
custom_data: {},
focal_point: null,
});
if (result) {
ctx.notice(`Success! ${JSON.stringify(result)}`);
} else {
ctx.alert('Closed!');
}

itemsDropdownActions(itemType: ItemType, ctx)

This function lets you set up custom actions (or groups of actions) that show up when the user:

  • selects multiple records in the collection view for batch operations, or
  • starts editing a specific record.

The executeItemsDropdownAction() hook will be triggered once the user clicks on one of the defined actions.

Return value

The function must return: Array<DropdownAction | DropdownActionGroup>.

Context object

The following properties and methods are available in the ctx argument:

This hook exposes additional information and operations specific to the context in which it operates.

Every hook available in the Plugin SDK shares the same minumum set of properties and methods.

Information about the current user using the CMS.

The current DatoCMS user. It can either be the owner or one of the collaborators (regular or SSO).

View on Github

The role for the current DatoCMS user.

View on Github

The access token to perform API calls on behalf of the current user. Only available if currentUserAccessToken additional permission is granted.

View on Github
These methods can be used to open custom dialogs/confirmation panels.

Opens a custom modal. Returns a promise resolved with what the modal itself returns calling the resolve() function.

View on Github
const result = await ctx.openModal({
id: 'regular',
title: 'Custom title!',
width: 'l',
parameters: { foo: 'bar' },
});
if (result) {
ctx.notice(`Success! ${JSON.stringify(result)}`);
} else {
ctx.alert('Closed!');
}

Opens a UI-consistent confirmation dialog. Returns a promise resolved with the value of the choice made by the user.

View on Github
const result = await ctx.openConfirm({
title: 'Custom title',
content:
'Lorem Ipsum is simply dummy text of the printing and typesetting industry',
choices: [
{
label: 'Positive',
value: 'positive',
intent: 'positive',
},
{
label: 'Negative',
value: 'negative',
intent: 'negative',
},
],
cancel: {
label: 'Cancel',
value: false,
},
});
if (result) {
ctx.notice(`Success! ${result}`);
} else {
ctx.alert('Cancelled!');
}
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.

View on Github

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.

View on Github

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.

View on Github

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.

View on Github

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.

View on Github
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.

View on Github
const itemTypeId = prompt('Please insert a model ID:');
const item = await ctx.createNewItem(itemTypeId);
if (item) {
ctx.notice(`Success! ${item.id}`);
} else {
ctx.alert('Closed!');
}

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.

View on Github
const itemTypeId = prompt('Please insert a model ID:');
const items = await ctx.selectItem(itemTypeId, { multiple: true });
if (items) {
ctx.notice(`Success! ${items.map((i) => i.id).join(', ')}`);
} else {
ctx.alert('Closed!');
}

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.

View on Github
const itemId = prompt('Please insert a record ID:');
const item = await ctx.editItem(itemId);
if (item) {
ctx.notice(`Success! ${item.id}`);
} else {
ctx.alert('Closed!');
}
These methods can be used to asyncronously load additional information your plugin needs to work.

Loads all the fields for a specific model (or block). Fields will be returned and will also be available in the the fields property.

View on Github
const itemTypeId = prompt('Please insert a model ID:');
const fields = await ctx.loadItemTypeFields(itemTypeId);
ctx.notice(
`Success! ${fields
.map((field) => field.attributes.api_key)
.join(', ')}`,
);

Loads all the fieldsets for a specific model (or block). Fieldsets will be returned and will also be available in the the fieldsets property.

View on Github
const itemTypeId = prompt('Please insert a model ID:');
const fieldsets = await ctx.loadItemTypeFieldsets(itemTypeId);
ctx.notice(
`Success! ${fieldsets
.map((fieldset) => fieldset.attributes.title)
.join(', ')}`,
);

Loads all the fields in the project that are currently using the plugin for one of its manual field extensions.

View on Github
const fields = await ctx.loadFieldsUsingPlugin();
ctx.notice(
`Success! ${fields
.map((field) => field.attributes.api_key)
.join(', ')}`,
);

Loads all regular users. Users will be returned and will also be available in the the users property.

View on Github
const users = await ctx.loadUsers();
ctx.notice(`Success! ${users.map((user) => user.id).join(', ')}`);

Loads all SSO users. Users will be returned and will also be available in the the ssoUsers property.

View on Github
const users = await ctx.loadSsoUsers();
ctx.notice(`Success! ${users.map((user) => user.id).join(', ')}`);
These methods can be used to take the user to different pages.

Moves the user to another URL internal to the backend.

View on Github
await ctx.navigateTo('/');
Information about the current plugin. Useful to access the plugin's global configuration object.

The current plugin.

View on Github

The current DatoCMS project.

View on Github

The ID of the current environment.

View on Github

The account/organization that is the project owner.

View on Github

UI preferences of the current user (right now, only the preferred locale is available).

View on Github

An object containing the theme colors for the current DatoCMS project.

View on Github
These methods can be used to show UI-consistent toast notifications to the end-user.

Triggers an "error" toast displaying the selected message.

View on Github
const message = prompt(
'Please insert a message:',
'This is an alert message!',
);
await ctx.alert(message);

Triggers a "success" toast displaying the selected message.

View on Github
const message = prompt(
'Please insert a message:',
'This is a notice message!',
);
await ctx.notice(message);

Triggers a custom toast displaying the selected message (and optionally a CTA).

View on Github
const result = await ctx.customToast({
type: 'warning',
message: 'Just a sample warning notification!',
dismissOnPageChange: true,
dismissAfterTimeout: 5000,
cta: {
label: 'Execute call-to-action',
value: 'cta',
},
});
if (result === 'cta') {
ctx.notice(`Clicked CTA!`);
}
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.

View on Github
await ctx.updatePluginParameters({ debugMode: true });
await ctx.notice('Plugin parameters successfully updated!');

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.

View on Github
const fields = await ctx.loadFieldsUsingPlugin();
if (fields.length === 0) {
ctx.alert('No field is using this plugin as a manual extension!');
return;
}
for (const field of fields) {
const { appearance } = field.attributes;
const operations = [];
if (appearance.editor === ctx.plugin.id) {
operations.push({
operation: 'updateEditor',
newParameters: {
...appearance.parameters,
foo: 'bar',
},
});
}
appearance.addons.forEach((addon, i) => {
if (addon.id !== ctx.plugin.id) {
return;
}
operations.push({
operation: 'updateAddon',
index: i,
newParameters: { ...addon.parameters, foo: 'bar' },
});
});
await ctx.updateFieldAppearance(field.id, operations);
ctx.notice(`Successfully edited field ${field.attributes.api_key}`);
}
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.

View on Github
const item = await ctx.selectUpload({ multiple: false });
if (item) {
ctx.notice(`Success! ${item.id}`);
} else {
ctx.alert('Closed!');
}

Opens a dialog for editing a Media Area asset. It returns a promise resolved with:

  • The updated asset, if the user persists some changes to the asset itself
  • null, if the user closes the dialog without persisting any change
  • An asset structure with an additional deleted property set to true, if the user deletes the asset.
View on Github
const uploadId = prompt('Please insert an asset ID:');
const item = await ctx.editUpload(uploadId);
if (item) {
ctx.notice(`Success! ${item.id}`);
} else {
ctx.alert('Closed!');
}

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.

View on Github
const uploadId = prompt('Please insert an asset ID:');
const result = await ctx.editUploadMetadata({
upload_id: uploadId,
alt: null,
title: null,
custom_data: {},
focal_point: null,
});
if (result) {
ctx.notice(`Success! ${JSON.stringify(result)}`);
} else {
ctx.alert('Closed!');
}

uploadsDropdownActions(ctx)

This function lets you set up custom actions (or groups of actions) that show up when the user:

  • selects multiple assets in the Media Area for batch operations, or
  • opens up a specific asset from the Media Area.

The executeUploadsDropdownAction() hook will be triggered once the user clicks on one of the defined actions.

Return value

The function must return: Array<DropdownAction | DropdownActionGroup>.

Context object

The following properties and methods are available in the ctx argument:

Every hook available in the Plugin SDK shares the same minumum set of properties and methods.

Information about the current user using the CMS.

The current DatoCMS user. It can either be the owner or one of the collaborators (regular or SSO).

View on Github

The role for the current DatoCMS user.

View on Github

The access token to perform API calls on behalf of the current user. Only available if currentUserAccessToken additional permission is granted.

View on Github
These methods can be used to open custom dialogs/confirmation panels.

Opens a custom modal. Returns a promise resolved with what the modal itself returns calling the resolve() function.

View on Github
const result = await ctx.openModal({
id: 'regular',
title: 'Custom title!',
width: 'l',
parameters: { foo: 'bar' },
});
if (result) {
ctx.notice(`Success! ${JSON.stringify(result)}`);
} else {
ctx.alert('Closed!');
}

Opens a UI-consistent confirmation dialog. Returns a promise resolved with the value of the choice made by the user.

View on Github
const result = await ctx.openConfirm({
title: 'Custom title',
content:
'Lorem Ipsum is simply dummy text of the printing and typesetting industry',
choices: [
{
label: 'Positive',
value: 'positive',
intent: 'positive',
},
{
label: 'Negative',
value: 'negative',
intent: 'negative',
},
],
cancel: {
label: 'Cancel',
value: false,
},
});
if (result) {
ctx.notice(`Success! ${result}`);
} else {
ctx.alert('Cancelled!');
}
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.

View on Github

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.

View on Github

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.

View on Github

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.

View on Github

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.

View on Github
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.

View on Github
const itemTypeId = prompt('Please insert a model ID:');
const item = await ctx.createNewItem(itemTypeId);
if (item) {
ctx.notice(`Success! ${item.id}`);
} else {
ctx.alert('Closed!');
}

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.

View on Github
const itemTypeId = prompt('Please insert a model ID:');
const items = await ctx.selectItem(itemTypeId, { multiple: true });
if (items) {
ctx.notice(`Success! ${items.map((i) => i.id).join(', ')}`);
} else {
ctx.alert('Closed!');
}

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.

View on Github
const itemId = prompt('Please insert a record ID:');
const item = await ctx.editItem(itemId);
if (item) {
ctx.notice(`Success! ${item.id}`);
} else {
ctx.alert('Closed!');
}
These methods can be used to asyncronously load additional information your plugin needs to work.

Loads all the fields for a specific model (or block). Fields will be returned and will also be available in the the fields property.

View on Github
const itemTypeId = prompt('Please insert a model ID:');
const fields = await ctx.loadItemTypeFields(itemTypeId);
ctx.notice(
`Success! ${fields
.map((field) => field.attributes.api_key)
.join(', ')}`,
);

Loads all the fieldsets for a specific model (or block). Fieldsets will be returned and will also be available in the the fieldsets property.

View on Github
const itemTypeId = prompt('Please insert a model ID:');
const fieldsets = await ctx.loadItemTypeFieldsets(itemTypeId);
ctx.notice(
`Success! ${fieldsets
.map((fieldset) => fieldset.attributes.title)
.join(', ')}`,
);

Loads all the fields in the project that are currently using the plugin for one of its manual field extensions.

View on Github
const fields = await ctx.loadFieldsUsingPlugin();
ctx.notice(
`Success! ${fields
.map((field) => field.attributes.api_key)
.join(', ')}`,
);

Loads all regular users. Users will be returned and will also be available in the the users property.

View on Github
const users = await ctx.loadUsers();
ctx.notice(`Success! ${users.map((user) => user.id).join(', ')}`);

Loads all SSO users. Users will be returned and will also be available in the the ssoUsers property.

View on Github
const users = await ctx.loadSsoUsers();
ctx.notice(`Success! ${users.map((user) => user.id).join(', ')}`);
These methods can be used to take the user to different pages.

Moves the user to another URL internal to the backend.

View on Github
await ctx.navigateTo('/');
Information about the current plugin. Useful to access the plugin's global configuration object.

The current plugin.

View on Github

The current DatoCMS project.

View on Github

The ID of the current environment.

View on Github

The account/organization that is the project owner.

View on Github

UI preferences of the current user (right now, only the preferred locale is available).

View on Github

An object containing the theme colors for the current DatoCMS project.

View on Github
These methods can be used to show UI-consistent toast notifications to the end-user.

Triggers an "error" toast displaying the selected message.

View on Github
const message = prompt(
'Please insert a message:',
'This is an alert message!',
);
await ctx.alert(message);

Triggers a "success" toast displaying the selected message.

View on Github
const message = prompt(
'Please insert a message:',
'This is a notice message!',
);
await ctx.notice(message);

Triggers a custom toast displaying the selected message (and optionally a CTA).

View on Github
const result = await ctx.customToast({
type: 'warning',
message: 'Just a sample warning notification!',
dismissOnPageChange: true,
dismissAfterTimeout: 5000,
cta: {
label: 'Execute call-to-action',
value: 'cta',
},
});
if (result === 'cta') {
ctx.notice(`Clicked CTA!`);
}
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.

View on Github
await ctx.updatePluginParameters({ debugMode: true });
await ctx.notice('Plugin parameters successfully updated!');

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.

View on Github
const fields = await ctx.loadFieldsUsingPlugin();
if (fields.length === 0) {
ctx.alert('No field is using this plugin as a manual extension!');
return;
}
for (const field of fields) {
const { appearance } = field.attributes;
const operations = [];
if (appearance.editor === ctx.plugin.id) {
operations.push({
operation: 'updateEditor',
newParameters: {
...appearance.parameters,
foo: 'bar',
},
});
}
appearance.addons.forEach((addon, i) => {
if (addon.id !== ctx.plugin.id) {
return;
}
operations.push({
operation: 'updateAddon',
index: i,
newParameters: { ...addon.parameters, foo: 'bar' },
});
});
await ctx.updateFieldAppearance(field.id, operations);
ctx.notice(`Successfully edited field ${field.attributes.api_key}`);
}
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.

View on Github
const item = await ctx.selectUpload({ multiple: false });
if (item) {
ctx.notice(`Success! ${item.id}`);
} else {
ctx.alert('Closed!');
}

Opens a dialog for editing a Media Area asset. It returns a promise resolved with:

  • The updated asset, if the user persists some changes to the asset itself
  • null, if the user closes the dialog without persisting any change
  • An asset structure with an additional deleted property set to true, if the user deletes the asset.
View on Github
const uploadId = prompt('Please insert an asset ID:');
const item = await ctx.editUpload(uploadId);
if (item) {
ctx.notice(`Success! ${item.id}`);
} else {
ctx.alert('Closed!');
}

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.

View on Github
const uploadId = prompt('Please insert an asset ID:');
const result = await ctx.editUploadMetadata({
upload_id: uploadId,
alt: null,
title: null,
custom_data: {},
focal_point: null,
});
if (result) {
ctx.notice(`Success! ${JSON.stringify(result)}`);
} else {
ctx.alert('Closed!');
}