Sorry, no results found for "".

Plugin SDK > Sidebars and sidebar panels

Sidebars and sidebar panels

Through plugins it is possible to customize the standard sidebars that DatoCMS offers when editing a record or an asset in the Media Area.

Sidebars vs Sidebar Panels

The SDK offers two ways to customize the sidebar interface. You can either add new collapsible panels to the default sidebar:

Or offer complete alternative sidebars, as in the example below:

Depending on the size of the content you need to display, you can choose one or the other. Or even offer both. You can take a look at a real-world example of both sidebars and sidebar panels in the Web Previews plugin.

Inside sidebars and sidebar panels, the plugin developer can render what they prefer, while also having the possibility to:

  • access a series of information relating to the record that's being edited, the project in which the plugin is installed or the logged-in user;

  • make calls to DatoCMS to produce various effects and interact with the main application (changing form values, navigating to other pages, triggering notifications, opening modals, etc.);

Implementing a Sidebar Panel

Let's say we want to create a sidebar panel that will show a link pointing to the website page related to the record we're editing.

The first step is to implement the itemFormSidebarPanels hook, to declare our intent to add the panel to the sidebar:

import { connect, ItemFormSidebarPanelsCtx } from 'datocms-plugin-sdk';
connect({
itemFormSidebarPanels(model: ItemType, ctx: ItemFormSidebarPanelsCtx) {
return [
{
id: 'goToWebsite',
label: 'Go to website',
startOpen: true,
},
];
},
});

The code above will add a panel to every record in our project... but maybe not every record in DatoCMS has a specific page in the final website, right?

It might be better to add some settings to our plugin to let the final user declare the set of models that have permalinks, and the relative URL structure enforced on the frontend:

itemFormSidebarPanels(model: ItemType, ctx: ItemFormSidebarPanelsCtx) {
const { permalinksByModel } = ctx.plugin.attributes.parameters;
// Assuming we're saving user preferences in this format:
// {
// 'blog_post': '/blog/:slug',
// 'author': '/author/:slug',
// ...
// }
}
if (!permalinksByModel[model.attributes.api_key]) {
// Don't add the panel!
return [];
}
// Add the panel!
}

Rendering the panel

The final step is to actually render the panel itself by implementing the renderItemFormSidebarPanel hook.

Inside of this hook we initialize React and render a custom component called GoToWebsiteItemFormSidebarPanel, passing down as a prop the second ctx argument, which provides a series of information and methods for interacting with the main application:

import React from 'react';
import ReactDOM from 'react-dom';
import { connect, RenderItemFormSidebarPanelCtx } from 'datocms-plugin-sdk';
connect({
renderItemFormSidebarPanel(
sidebarPanelId,
ctx: RenderItemFormSidebarPanelCtx,
) {
ReactDOM.render(
<React.StrictMode>
<GoToWebsiteItemFormSidebarPanel ctx={ctx} />
</React.StrictMode>,
document.getElementById('root'),
);
},
});

A plugin might render different panels, so we can use the sidebarPanelId argument to know which one we are requested to render, and write a specific React component for each of them.

import { Canvas } from 'datocms-react-ui';
type PropTypes = {
ctx: RenderItemFormSidebarPanelCtx;
};
function GoToWebsiteItemFormSidebarPanel({ ctx }: PropTypes) {
return (
<Canvas ctx={ctx}>
Hello from the sidebar!
</Canvas>
);
}
Always use the canvas!

It is important to wrap the content inside the Canvas component, so that the iframe will continuously auto-adjust its size based on the content we're rendering, and to give our app the look and feel of the DatoCMS web app.

All we need to do now is to actually render the link to the website, reading from ctx.formValues the slug value and generating the final frontend URL:

import { ButtonLink } from 'datocms-react-ui';
function GoToWebsiteItemFormSidebarPanel({ ctx }: PropTypes) {
if (ctx.itemStatus === 'new') {
// we're in a record that still has not been persisted
return <div>Please save the record first!</div>;
}
const { permalinksByModel } = ctx.plugin.attributes.parameters;
const permalinkStructure = permalinksByModel[ctx.itemType.attributes.api_key];
const url = permalinkStructure.replace(':slug', ctx.formValues.slug);
return (
<Canvas ctx={ctx}>
<ButtonLink href={url} fullWidth>
View it on the website!
</ButtonLink>
</Canvas>
);
}

Implementing a custom Sidebar

Suppose that instead of presenting a link to a webpage, we want to embed the actual web page alongside the record. To do that we need more space than what a sidebar panel can offer, so creating a completely separate sidebar is more appropriate.

Managing sidebars is very similar to what we just did with sidebar panels. The main difference is in the way you define them. To declare our intent to add the sidebar, implement the itemFormSidebars hook:

import { connect, ItemFormSidebarsCtx } from 'datocms-plugin-sdk';
connect({
itemFormSidebars(model: ItemType, ctx: ItemFormSidebarsCtx) {
return [
{
id: "sideBySidePreview",
label: "Side-by-side preview",
preferredWidth: 900,
},
];
},
});

With the preferredWidth, you can control the ideal width for the sidebar when it opens. Users will then be able to resize it if they want. There is one constraint though: the sidebar width cannot exceed 60% of the screen, taking up too much screen real estate. If the preferredWidth is bigger than this value, it will be capped.

Rendering the sidebar

Now, to render the sidebar itself, we can implement the renderItemFormSidebar hook.

Just like we did with the sidebar panel, we initialize React and render a custom component, passing down as a prop the second ctx argument, which provides a series of information and methods for interacting with the main application:

import React from 'react';
import ReactDOM from 'react-dom';
import { connect, RenderItemFormSidebarCtx } from 'datocms-plugin-sdk';
connect({
renderItemFormSidebar(
sidebarId,
ctx: RenderItemFormSidebarCtx,
) {
ReactDOM.render(
<React.StrictMode>
<SideBySidePreviewSidebar ctx={ctx} />
</React.StrictMode>,
document.getElementById('root'),
);
},
});

A plugin might render different sidebars, so we can use the sidebarId argument to know which one we are requested to render, and write a specific React component for each of them.

In our <SideBySidePreviewSidebar> component, we can simply render an iframe pointing to the webpage, copying most of the logic from our previous sidebar panel:

import { Canvas } from 'datocms-react-ui';
type PropTypes = {
ctx: RenderItemFormSidebarCtx;
};
function SideBySidePreviewSidebar({ ctx }: PropTypes) {
const { permalinksByModel } = ctx.plugin.attributes.parameters;
const permalinkStructure = permalinksByModel[ctx.itemType.attributes.api_key];
const url = permalinkStructure.replace(':slug', ctx.formValues.slug);
return (
<Canvas ctx={ctx}>
<iframe src={url} />
</Canvas>
);
}

Asset Sidebars and Sidebar Panels

Requires Plugin SDK v2+

The asset sidebars and sidebar panels in the following examples requires v2.x.x or higher of the DatoCMS Plugins SDK. If you're still on v1, please upgrade before proceeding.

In addition to being able to customize the sidebars of a record, it is also possible to do the same in the detail view of an asset in the Media Area:

The implementation is absolutely similar to the one just seen. The only thing that changes is the hooks to be used:

Here's an example:

import {
connect,
UploadSidebarPanelsCtx,
RenderUploadSidebarPanelCtx,
UploadSidebarsCtx,
RenderUploadSidebarCtx
} from 'datocms-plugin-sdk';
connect({
uploadSidebars(ctx: UploadSidebarsCtx) {
return [
{
id: "customSidebar",
label: "My Custom Sidebar",
preferredWidth: 900,
},
];
},
renderUploadSidebar(sidebarId: string, ctx: RenderUploadSidebarCtx) {
render(<CustomSidebar ctx={ctx} />);
},
uploadSidebarPanels(ctx: UploadSidebarPanelsCtx) {
return [
{
id: 'customSidebarPanel',
label: 'Custom Sidebar Panel',
startOpen: true,
},
];
},
renderUploadSidebarPanel(sidebarPanelId: string, ctx: RenderUploadSidebarPanelCtx) {
render(<CustomSidebarPanel ctx={ctx} />);
},
});

itemFormSidebars(itemType: ItemType, ctx)

Use this function to declare new sidebar to be shown when the user edits records of a particular model.

Return value

The function must return: ItemFormSidebar[].

Context object

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

Properties and methods available in every hook

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

Authentication properties
Information about the current user using the CMS.
ctx.currentUser: User | SsoUser | Account | Organization The current DatoCMS user. It can either be the owner or one of the collaborators (regular or SSO).

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

View on Github
ctx.currentRole: Role The role for the current DatoCMS user.

The role for the current DatoCMS user.

View on Github
ctx.currentUserAccessToken: string | undefined The access token to perform API calls on behalf of the current user. Only available if currentUserAccessToken additional permission is granted.

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

View on Github
Custom dialog methods
These methods can be used to open custom dialogs/confirmation panels.
ctx.openModal(modal: Modal) => Promise<unknown> Opens a custom modal. Returns a promise resolved with what the modal itself returns calling the resolve() function.

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!');
}
ctx.openConfirm(options: ConfirmOptions) => Promise<unknown> Opens a UI-consistent confirmation dialog. Returns a promise resolved with the value of the choice made by the user.

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!');
}
Entity repos properties
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.
ctx.itemTypes: Partial<Record<string, ItemType>> All the models of the current DatoCMS project, indexed by ID.

All the models of the current DatoCMS project, indexed by ID.

View on Github
ctx.fields: Partial<Record<string, Field>> All the fields currently loaded for the current DatoCMS project, indexed by ID. If some fields you need are not present, use the loadItemTypeFields function to load them.

All the 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
ctx.fieldsets: Partial<Record<string, Fieldset>> All the fieldsets currently loaded for the current DatoCMS project, indexed by ID. If some fields you need are not present, use the loadItemTypeFieldsets function to load them.

All the 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
ctx.users: Partial<Record<string, User>> All the regular users currently loaded for the current DatoCMS project, indexed by ID. It will always contain the current user. If some users you need are not present, use the loadUsers function to load them.

All the 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
ctx.ssoUsers: Partial<Record<string, SsoUser>> 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.

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
Item dialog methods
These methods let you open the standard DatoCMS dialogs needed to interact with records.
ctx.createNewItem(itemTypeId: string) => Promise<Item | null> 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.

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!');
}
ctx.selectItem Opens a dialog for selecting one (or multiple) record(s) from a list of existing records of type itemTypeId. It returns a promise resolved with the selected record(s), or null if the user closes the dialog without choosing any record.

Opens a dialog for 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!');
}
ctx.editItem(itemId: string) => Promise<Item | null> 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.

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!');
}
Load data methods
These methods can be used to asyncronously load additional information your plugin needs to work.
ctx.loadItemTypeFields(itemTypeId: string) => Promise<Field[]> Loads all the fields for a specific model (or block). Fields will be returned and will also be available in the the fields property.

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(', ')}`,
);
ctx.loadItemTypeFieldsets(itemTypeId: string) => Promise<Fieldset[]> Loads all the fieldsets for a specific model (or block). Fieldsets will be returned and will also be available in the the fieldsets property.

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(', ')}`,
);
ctx.loadFieldsUsingPlugin() => Promise<Field[]> Loads all the fields in the project that are currently using the plugin for one of its manual field extensions.

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(', ')}`,
);
ctx.loadUsers() => Promise<User[]> Loads all regular users. Users will be returned and will also be available in the the users property.

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(', ')}`);
ctx.loadSsoUsers() => Promise<SsoUser[]> Loads all SSO users. Users will be returned and will also be available in the the ssoUsers property.

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(', ')}`);
Navigate methods
These methods can be used to take the user to different pages.
ctx.navigateTo(path: string) => Promise<void> Moves the user to another URL internal to the backend.

Moves the user to another URL internal to the backend.

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

The current plugin.

View on Github
Project properties
ctx.site: Site The current DatoCMS project.

The current DatoCMS project.

View on Github
ctx.environment: string The ID of the current environment.

The ID of the current environment.

View on Github
ctx.isEnvironmentPrimary: boolean Whether the current environment is the primary one.

Whether the current environment is the primary one.

View on Github
ctx.owner: Account | Organization The account/organization that is the project owner.

The account/organization that is the project owner.

View on Github
ctx.ui UI preferences of the current user (right now, only the preferred locale is available).

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

View on Github
ctx.theme: Theme An object containing the theme colors for the current DatoCMS project.

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

View on Github
Toast methods
These methods can be used to show UI-consistent toast notifications to the end-user.
ctx.alert(message: string) => Promise<void> Triggers an "error" toast displaying the selected message.

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);
ctx.notice(message: string) => Promise<void> Triggers a "success" toast displaying the selected 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);
ctx.customToast Triggers a custom toast displaying the selected message (and optionally a CTA).

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!`);
}
Update plugin parameters methods
These methods can be used to update both plugin parameters and manual field extensions configuration.
ctx.updatePluginParameters(params: Record<string, unknown>) => Promise<void> 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.

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!');
ctx.updateFieldAppearance(...) 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.

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}`);
}
Upload dialog methods
These methods let you open the standard DatoCMS dialogs needed to interact with Media Area assets.
ctx.selectUpload 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.

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!');
}
ctx.editUpload(...) 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.

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!');
}
ctx.editUploadMetadata(...) 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.

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!');
}

renderItemFormSidebar(sidebarId: string, ctx)

This function will be called when the plugin needs to render a sidebar (see the itemFormSidebars hook).

Context object

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

Hook-specific properties and methods

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

Item form additional methods
These methods can be used to interact with the form that's being shown to the end-user to edit a record.
ctx.toggleField(path: string, show: boolean) => Promise<void> 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.

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);
ctx.disableField(path: string, disable: boolean) => Promise<void> Disables/re-enables a specific field in the form.

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);
ctx.scrollToField(path: string, locale?: string) => Promise<void> 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.

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);
ctx.setFieldValue(path: string, value: unknown) => Promise<void> Changes a specific path of the formValues object.

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');
ctx.formValuesToItem(...) 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.

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);
ctx.itemToFormValues(...) Takes an Item entity, and converts it into the internal form state.

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

View on Github
await ctx.itemToFormValues(ctx.item);
ctx.saveCurrentItem(showToast?: boolean) => Promise<void> Triggers a submit form for current record.

Triggers a submit form for current record.

View on Github
await ctx.saveCurrentItem();
Item form additional properties
These information describe the current state of the form that's being shown to the end-user to edit a record.
ctx.locale: string The currently active locale for the record.

The currently active locale for the record.

View on Github
ctx.item: Item | null If an already persisted record is being edited, returns the full record entity.

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

View on Github
ctx.itemType: ItemType The model for the record being edited.

The model for the record being edited.

View on Github
ctx.formValues: Record<string, unknown> The complete internal form state.

The complete internal form state.

View on Github
ctx.itemStatus: 'new' | 'draft' | 'updated' | 'published' The current status of the record being edited.

The current status of the record being edited.

View on Github
ctx.isSubmitting: boolean Whether the form is currently submitting itself or not.

Whether the form is currently submitting itself or not.

View on Github
ctx.isFormDirty: boolean Whether the form has some non-persisted changes or not.

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

View on Github
ctx.blocksAnalysis: BlocksAnalysis Provides information on how many blocks are currently present in the form.

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

View on Github
Properties and methods
ctx.sidebarId: string The ID of the sidebar that needs to be rendered.

The ID of the sidebar that needs to be rendered.

View on Github
ctx.parameters: Record<string, unknown> The arbitrary parameters of the declared in the itemFormSidebars function.

The arbitrary parameters of the declared in the itemFormSidebars function.

View on Github
Properties and methods available in every hook

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

Authentication properties
Information about the current user using the CMS.
ctx.currentUser: User | SsoUser | Account | Organization The current DatoCMS user. It can either be the owner or one of the collaborators (regular or SSO).

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

View on Github
ctx.currentRole: Role The role for the current DatoCMS user.

The role for the current DatoCMS user.

View on Github
ctx.currentUserAccessToken: string | undefined The access token to perform API calls on behalf of the current user. Only available if currentUserAccessToken additional permission is granted.

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

View on Github
Custom dialog methods
These methods can be used to open custom dialogs/confirmation panels.
ctx.openModal(modal: Modal) => Promise<unknown> Opens a custom modal. Returns a promise resolved with what the modal itself returns calling the resolve() function.

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!');
}
ctx.openConfirm(options: ConfirmOptions) => Promise<unknown> Opens a UI-consistent confirmation dialog. Returns a promise resolved with the value of the choice made by the user.

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!');
}
Entity repos properties
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.
ctx.itemTypes: Partial<Record<string, ItemType>> All the models of the current DatoCMS project, indexed by ID.

All the models of the current DatoCMS project, indexed by ID.

View on Github
ctx.fields: Partial<Record<string, Field>> All the fields currently loaded for the current DatoCMS project, indexed by ID. If some fields you need are not present, use the loadItemTypeFields function to load them.

All the 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
ctx.fieldsets: Partial<Record<string, Fieldset>> All the fieldsets currently loaded for the current DatoCMS project, indexed by ID. If some fields you need are not present, use the loadItemTypeFieldsets function to load them.

All the 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
ctx.users: Partial<Record<string, User>> All the regular users currently loaded for the current DatoCMS project, indexed by ID. It will always contain the current user. If some users you need are not present, use the loadUsers function to load them.

All the 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
ctx.ssoUsers: Partial<Record<string, SsoUser>> 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.

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
Item dialog methods
These methods let you open the standard DatoCMS dialogs needed to interact with records.
ctx.createNewItem(itemTypeId: string) => Promise<Item | null> 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.

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!');
}
ctx.selectItem Opens a dialog for selecting one (or multiple) record(s) from a list of existing records of type itemTypeId. It returns a promise resolved with the selected record(s), or null if the user closes the dialog without choosing any record.

Opens a dialog for 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!');
}
ctx.editItem(itemId: string) => Promise<Item | null> 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.

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!');
}
Load data methods
These methods can be used to asyncronously load additional information your plugin needs to work.
ctx.loadItemTypeFields(itemTypeId: string) => Promise<Field[]> Loads all the fields for a specific model (or block). Fields will be returned and will also be available in the the fields property.

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(', ')}`,
);
ctx.loadItemTypeFieldsets(itemTypeId: string) => Promise<Fieldset[]> Loads all the fieldsets for a specific model (or block). Fieldsets will be returned and will also be available in the the fieldsets property.

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(', ')}`,
);
ctx.loadFieldsUsingPlugin() => Promise<Field[]> Loads all the fields in the project that are currently using the plugin for one of its manual field extensions.

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(', ')}`,
);
ctx.loadUsers() => Promise<User[]> Loads all regular users. Users will be returned and will also be available in the the users property.

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(', ')}`);
ctx.loadSsoUsers() => Promise<SsoUser[]> Loads all SSO users. Users will be returned and will also be available in the the ssoUsers property.

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(', ')}`);
Navigate methods
These methods can be used to take the user to different pages.
ctx.navigateTo(path: string) => Promise<void> Moves the user to another URL internal to the backend.

Moves the user to another URL internal to the backend.

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

The current plugin.

View on Github
Project properties
ctx.site: Site The current DatoCMS project.

The current DatoCMS project.

View on Github
ctx.environment: string The ID of the current environment.

The ID of the current environment.

View on Github
ctx.isEnvironmentPrimary: boolean Whether the current environment is the primary one.

Whether the current environment is the primary one.

View on Github
ctx.owner: Account | Organization The account/organization that is the project owner.

The account/organization that is the project owner.

View on Github
ctx.ui UI preferences of the current user (right now, only the preferred locale is available).

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

View on Github
ctx.theme: Theme An object containing the theme colors for the current DatoCMS project.

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

View on Github
Toast methods
These methods can be used to show UI-consistent toast notifications to the end-user.
ctx.alert(message: string) => Promise<void> Triggers an "error" toast displaying the selected message.

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);
ctx.notice(message: string) => Promise<void> Triggers a "success" toast displaying the selected 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);
ctx.customToast Triggers a custom toast displaying the selected message (and optionally a CTA).

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!`);
}
Update plugin parameters methods
These methods can be used to update both plugin parameters and manual field extensions configuration.
ctx.updatePluginParameters(params: Record<string, unknown>) => Promise<void> 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.

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!');
ctx.updateFieldAppearance(...) 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.

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}`);
}
Upload dialog methods
These methods let you open the standard DatoCMS dialogs needed to interact with Media Area assets.
ctx.selectUpload 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.

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!');
}
ctx.editUpload(...) 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.

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!');
}
ctx.editUploadMetadata(...) 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.

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!');
}

itemFormSidebarPanels(itemType: ItemType, ctx)

Use this function to declare new sidebar panels to be shown when the user edits records of a particular model.

Return value

The function must return: ItemFormSidebarPanel[].

Context object

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

Properties and methods available in every hook

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

Authentication properties
Information about the current user using the CMS.
ctx.currentUser: User | SsoUser | Account | Organization The current DatoCMS user. It can either be the owner or one of the collaborators (regular or SSO).

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

View on Github
ctx.currentRole: Role The role for the current DatoCMS user.

The role for the current DatoCMS user.

View on Github
ctx.currentUserAccessToken: string | undefined The access token to perform API calls on behalf of the current user. Only available if currentUserAccessToken additional permission is granted.

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

View on Github
Custom dialog methods
These methods can be used to open custom dialogs/confirmation panels.
ctx.openModal(modal: Modal) => Promise<unknown> Opens a custom modal. Returns a promise resolved with what the modal itself returns calling the resolve() function.

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!');
}
ctx.openConfirm(options: ConfirmOptions) => Promise<unknown> Opens a UI-consistent confirmation dialog. Returns a promise resolved with the value of the choice made by the user.

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!');
}
Entity repos properties
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.
ctx.itemTypes: Partial<Record<string, ItemType>> All the models of the current DatoCMS project, indexed by ID.

All the models of the current DatoCMS project, indexed by ID.

View on Github
ctx.fields: Partial<Record<string, Field>> All the fields currently loaded for the current DatoCMS project, indexed by ID. If some fields you need are not present, use the loadItemTypeFields function to load them.

All the 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
ctx.fieldsets: Partial<Record<string, Fieldset>> All the fieldsets currently loaded for the current DatoCMS project, indexed by ID. If some fields you need are not present, use the loadItemTypeFieldsets function to load them.

All the 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
ctx.users: Partial<Record<string, User>> All the regular users currently loaded for the current DatoCMS project, indexed by ID. It will always contain the current user. If some users you need are not present, use the loadUsers function to load them.

All the 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
ctx.ssoUsers: Partial<Record<string, SsoUser>> 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.

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
Item dialog methods
These methods let you open the standard DatoCMS dialogs needed to interact with records.
ctx.createNewItem(itemTypeId: string) => Promise<Item | null> 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.

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!');
}
ctx.selectItem Opens a dialog for selecting one (or multiple) record(s) from a list of existing records of type itemTypeId. It returns a promise resolved with the selected record(s), or null if the user closes the dialog without choosing any record.

Opens a dialog for 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!');
}
ctx.editItem(itemId: string) => Promise<Item | null> 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.

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!');
}
Load data methods
These methods can be used to asyncronously load additional information your plugin needs to work.
ctx.loadItemTypeFields(itemTypeId: string) => Promise<Field[]> Loads all the fields for a specific model (or block). Fields will be returned and will also be available in the the fields property.

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(', ')}`,
);
ctx.loadItemTypeFieldsets(itemTypeId: string) => Promise<Fieldset[]> Loads all the fieldsets for a specific model (or block). Fieldsets will be returned and will also be available in the the fieldsets property.

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(', ')}`,
);
ctx.loadFieldsUsingPlugin() => Promise<Field[]> Loads all the fields in the project that are currently using the plugin for one of its manual field extensions.

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(', ')}`,
);
ctx.loadUsers() => Promise<User[]> Loads all regular users. Users will be returned and will also be available in the the users property.

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(', ')}`);
ctx.loadSsoUsers() => Promise<SsoUser[]> Loads all SSO users. Users will be returned and will also be available in the the ssoUsers property.

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(', ')}`);
Navigate methods
These methods can be used to take the user to different pages.
ctx.navigateTo(path: string) => Promise<void> Moves the user to another URL internal to the backend.

Moves the user to another URL internal to the backend.

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

The current plugin.

View on Github
Project properties
ctx.site: Site The current DatoCMS project.

The current DatoCMS project.

View on Github
ctx.environment: string The ID of the current environment.

The ID of the current environment.

View on Github
ctx.isEnvironmentPrimary: boolean Whether the current environment is the primary one.

Whether the current environment is the primary one.

View on Github
ctx.owner: Account | Organization The account/organization that is the project owner.

The account/organization that is the project owner.

View on Github
ctx.ui UI preferences of the current user (right now, only the preferred locale is available).

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

View on Github
ctx.theme: Theme An object containing the theme colors for the current DatoCMS project.

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

View on Github
Toast methods
These methods can be used to show UI-consistent toast notifications to the end-user.
ctx.alert(message: string) => Promise<void> Triggers an "error" toast displaying the selected message.

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);
ctx.notice(message: string) => Promise<void> Triggers a "success" toast displaying the selected 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);
ctx.customToast Triggers a custom toast displaying the selected message (and optionally a CTA).

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!`);
}
Update plugin parameters methods
These methods can be used to update both plugin parameters and manual field extensions configuration.
ctx.updatePluginParameters(params: Record<string, unknown>) => Promise<void> 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.

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!');
ctx.updateFieldAppearance(...) 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.

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}`);
}
Upload dialog methods
These methods let you open the standard DatoCMS dialogs needed to interact with Media Area assets.
ctx.selectUpload 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.

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!');
}
ctx.editUpload(...) 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.

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!');
}
ctx.editUploadMetadata(...) 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.

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!');
}

renderItemFormSidebarPanel(sidebarPaneId: string, ctx)

This function will be called when the plugin needs to render a sidebar panel (see the itemFormSidebarPanels hook).

Context object

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

Hook-specific properties and methods

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

Item form additional methods
These methods can be used to interact with the form that's being shown to the end-user to edit a record.
ctx.toggleField(path: string, show: boolean) => Promise<void> 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.

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);
ctx.disableField(path: string, disable: boolean) => Promise<void> Disables/re-enables a specific field in the form.

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);
ctx.scrollToField(path: string, locale?: string) => Promise<void> 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.

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);
ctx.setFieldValue(path: string, value: unknown) => Promise<void> Changes a specific path of the formValues object.

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');
ctx.formValuesToItem(...) 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.

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);
ctx.itemToFormValues(...) Takes an Item entity, and converts it into the internal form state.

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

View on Github
await ctx.itemToFormValues(ctx.item);
ctx.saveCurrentItem(showToast?: boolean) => Promise<void> Triggers a submit form for current record.

Triggers a submit form for current record.

View on Github
await ctx.saveCurrentItem();
Item form additional properties
These information describe the current state of the form that's being shown to the end-user to edit a record.
ctx.locale: string The currently active locale for the record.

The currently active locale for the record.

View on Github
ctx.item: Item | null If an already persisted record is being edited, returns the full record entity.

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

View on Github
ctx.itemType: ItemType The model for the record being edited.

The model for the record being edited.

View on Github
ctx.formValues: Record<string, unknown> The complete internal form state.

The complete internal form state.

View on Github
ctx.itemStatus: 'new' | 'draft' | 'updated' | 'published' The current status of the record being edited.

The current status of the record being edited.

View on Github
ctx.isSubmitting: boolean Whether the form is currently submitting itself or not.

Whether the form is currently submitting itself or not.

View on Github
ctx.isFormDirty: boolean Whether the form has some non-persisted changes or not.

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

View on Github
ctx.blocksAnalysis: BlocksAnalysis Provides information on how many blocks are currently present in the form.

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

View on Github
Properties and methods
ctx.sidebarPaneId: string The ID of the sidebar panel that needs to be rendered.

The ID of the sidebar panel that needs to be rendered.

View on Github
ctx.parameters: Record<string, unknown> The arbitrary parameters of the panel declared in the itemFormSidebarPanels function.

The arbitrary parameters of the panel declared in the itemFormSidebarPanels function.

View on Github
Properties and methods available in every hook

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

Authentication properties
Information about the current user using the CMS.
ctx.currentUser: User | SsoUser | Account | Organization The current DatoCMS user. It can either be the owner or one of the collaborators (regular or SSO).

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

View on Github
ctx.currentRole: Role The role for the current DatoCMS user.

The role for the current DatoCMS user.

View on Github
ctx.currentUserAccessToken: string | undefined The access token to perform API calls on behalf of the current user. Only available if currentUserAccessToken additional permission is granted.

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

View on Github
Custom dialog methods
These methods can be used to open custom dialogs/confirmation panels.
ctx.openModal(modal: Modal) => Promise<unknown> Opens a custom modal. Returns a promise resolved with what the modal itself returns calling the resolve() function.

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!');
}
ctx.openConfirm(options: ConfirmOptions) => Promise<unknown> Opens a UI-consistent confirmation dialog. Returns a promise resolved with the value of the choice made by the user.

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!');
}
Entity repos properties
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.
ctx.itemTypes: Partial<Record<string, ItemType>> All the models of the current DatoCMS project, indexed by ID.

All the models of the current DatoCMS project, indexed by ID.

View on Github
ctx.fields: Partial<Record<string, Field>> All the fields currently loaded for the current DatoCMS project, indexed by ID. If some fields you need are not present, use the loadItemTypeFields function to load them.

All the 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
ctx.fieldsets: Partial<Record<string, Fieldset>> All the fieldsets currently loaded for the current DatoCMS project, indexed by ID. If some fields you need are not present, use the loadItemTypeFieldsets function to load them.

All the 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
ctx.users: Partial<Record<string, User>> All the regular users currently loaded for the current DatoCMS project, indexed by ID. It will always contain the current user. If some users you need are not present, use the loadUsers function to load them.

All the 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
ctx.ssoUsers: Partial<Record<string, SsoUser>> 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.

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
Item dialog methods
These methods let you open the standard DatoCMS dialogs needed to interact with records.
ctx.createNewItem(itemTypeId: string) => Promise<Item | null> 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.

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!');
}
ctx.selectItem Opens a dialog for selecting one (or multiple) record(s) from a list of existing records of type itemTypeId. It returns a promise resolved with the selected record(s), or null if the user closes the dialog without choosing any record.

Opens a dialog for 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!');
}
ctx.editItem(itemId: string) => Promise<Item | null> 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.

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!');
}
Load data methods
These methods can be used to asyncronously load additional information your plugin needs to work.
ctx.loadItemTypeFields(itemTypeId: string) => Promise<Field[]> Loads all the fields for a specific model (or block). Fields will be returned and will also be available in the the fields property.

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(', ')}`,
);
ctx.loadItemTypeFieldsets(itemTypeId: string) => Promise<Fieldset[]> Loads all the fieldsets for a specific model (or block). Fieldsets will be returned and will also be available in the the fieldsets property.

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(', ')}`,
);
ctx.loadFieldsUsingPlugin() => Promise<Field[]> Loads all the fields in the project that are currently using the plugin for one of its manual field extensions.

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(', ')}`,
);
ctx.loadUsers() => Promise<User[]> Loads all regular users. Users will be returned and will also be available in the the users property.

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(', ')}`);
ctx.loadSsoUsers() => Promise<SsoUser[]> Loads all SSO users. Users will be returned and will also be available in the the ssoUsers property.

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(', ')}`);
Navigate methods
These methods can be used to take the user to different pages.
ctx.navigateTo(path: string) => Promise<void> Moves the user to another URL internal to the backend.

Moves the user to another URL internal to the backend.

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

The current plugin.

View on Github
Project properties
ctx.site: Site The current DatoCMS project.

The current DatoCMS project.

View on Github
ctx.environment: string The ID of the current environment.

The ID of the current environment.

View on Github
ctx.isEnvironmentPrimary: boolean Whether the current environment is the primary one.

Whether the current environment is the primary one.

View on Github
ctx.owner: Account | Organization The account/organization that is the project owner.

The account/organization that is the project owner.

View on Github
ctx.ui UI preferences of the current user (right now, only the preferred locale is available).

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

View on Github
ctx.theme: Theme An object containing the theme colors for the current DatoCMS project.

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

View on Github
Sizing utilities
A number of methods that you can use to control the size of the plugin frame.
ctx.startAutoResizer() => void Listens for DOM changes and automatically calls setHeight when it detects a change. If you're using datocms-react-ui package, the `` component already takes care of calling this method for you.

Listens for DOM changes and automatically calls setHeight when it detects a change. If you're using datocms-react-ui package, the <Canvas /> component already takes care of calling this method for you.

View on Github
ctx.stopAutoResizer() => void Stops resizing the iframe automatically.

Stops resizing the iframe automatically.

View on Github
ctx.updateHeight(newHeight?: number) => void Triggers a change in the size of the iframe. If you don't explicitely pass a newHeight it will be automatically calculated using the iframe content at the moment.

Triggers a change in the size of the iframe. If you don't explicitely pass a newHeight it will be automatically calculated using the iframe content at the moment.

View on Github
Toast methods
These methods can be used to show UI-consistent toast notifications to the end-user.
ctx.alert(message: string) => Promise<void> Triggers an "error" toast displaying the selected message.

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);
ctx.notice(message: string) => Promise<void> Triggers a "success" toast displaying the selected 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);
ctx.customToast Triggers a custom toast displaying the selected message (and optionally a CTA).

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!`);
}
Update plugin parameters methods
These methods can be used to update both plugin parameters and manual field extensions configuration.
ctx.updatePluginParameters(params: Record<string, unknown>) => Promise<void> 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.

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!');
ctx.updateFieldAppearance(...) 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.

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}`);
}
Upload dialog methods
These methods let you open the standard DatoCMS dialogs needed to interact with Media Area assets.
ctx.selectUpload 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.

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!');
}
ctx.editUpload(...) 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.

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!');
}
ctx.editUploadMetadata(...) 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.

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!');
}

uploadSidebars(ctx)

Use this function to declare new sidebar to be shown when the user opens up an asset in the Media Area.

Return value

The function must return: UploadSidebar[].

Context object

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

Properties and methods available in every hook

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

Authentication properties
Information about the current user using the CMS.
ctx.currentUser: User | SsoUser | Account | Organization The current DatoCMS user. It can either be the owner or one of the collaborators (regular or SSO).

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

View on Github
ctx.currentRole: Role The role for the current DatoCMS user.

The role for the current DatoCMS user.

View on Github
ctx.currentUserAccessToken: string | undefined The access token to perform API calls on behalf of the current user. Only available if currentUserAccessToken additional permission is granted.

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

View on Github
Custom dialog methods
These methods can be used to open custom dialogs/confirmation panels.
ctx.openModal(modal: Modal) => Promise<unknown> Opens a custom modal. Returns a promise resolved with what the modal itself returns calling the resolve() function.

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!');
}
ctx.openConfirm(options: ConfirmOptions) => Promise<unknown> Opens a UI-consistent confirmation dialog. Returns a promise resolved with the value of the choice made by the user.

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!');
}
Entity repos properties
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.
ctx.itemTypes: Partial<Record<string, ItemType>> All the models of the current DatoCMS project, indexed by ID.

All the models of the current DatoCMS project, indexed by ID.

View on Github
ctx.fields: Partial<Record<string, Field>> All the fields currently loaded for the current DatoCMS project, indexed by ID. If some fields you need are not present, use the loadItemTypeFields function to load them.

All the 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
ctx.fieldsets: Partial<Record<string, Fieldset>> All the fieldsets currently loaded for the current DatoCMS project, indexed by ID. If some fields you need are not present, use the loadItemTypeFieldsets function to load them.

All the 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
ctx.users: Partial<Record<string, User>> All the regular users currently loaded for the current DatoCMS project, indexed by ID. It will always contain the current user. If some users you need are not present, use the loadUsers function to load them.

All the 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
ctx.ssoUsers: Partial<Record<string, SsoUser>> 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.

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
Item dialog methods
These methods let you open the standard DatoCMS dialogs needed to interact with records.
ctx.createNewItem(itemTypeId: string) => Promise<Item | null> 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.

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!');
}
ctx.selectItem Opens a dialog for selecting one (or multiple) record(s) from a list of existing records of type itemTypeId. It returns a promise resolved with the selected record(s), or null if the user closes the dialog without choosing any record.

Opens a dialog for 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!');
}
ctx.editItem(itemId: string) => Promise<Item | null> 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.

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!');
}
Load data methods
These methods can be used to asyncronously load additional information your plugin needs to work.
ctx.loadItemTypeFields(itemTypeId: string) => Promise<Field[]> Loads all the fields for a specific model (or block). Fields will be returned and will also be available in the the fields property.

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(', ')}`,
);
ctx.loadItemTypeFieldsets(itemTypeId: string) => Promise<Fieldset[]> Loads all the fieldsets for a specific model (or block). Fieldsets will be returned and will also be available in the the fieldsets property.

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(', ')}`,
);
ctx.loadFieldsUsingPlugin() => Promise<Field[]> Loads all the fields in the project that are currently using the plugin for one of its manual field extensions.

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(', ')}`,
);
ctx.loadUsers() => Promise<User[]> Loads all regular users. Users will be returned and will also be available in the the users property.

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(', ')}`);
ctx.loadSsoUsers() => Promise<SsoUser[]> Loads all SSO users. Users will be returned and will also be available in the the ssoUsers property.

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(', ')}`);
Navigate methods
These methods can be used to take the user to different pages.
ctx.navigateTo(path: string) => Promise<void> Moves the user to another URL internal to the backend.

Moves the user to another URL internal to the backend.

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

The current plugin.

View on Github
Project properties
ctx.site: Site The current DatoCMS project.

The current DatoCMS project.

View on Github
ctx.environment: string The ID of the current environment.

The ID of the current environment.

View on Github
ctx.isEnvironmentPrimary: boolean Whether the current environment is the primary one.

Whether the current environment is the primary one.

View on Github
ctx.owner: Account | Organization The account/organization that is the project owner.

The account/organization that is the project owner.

View on Github
ctx.ui UI preferences of the current user (right now, only the preferred locale is available).

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

View on Github
ctx.theme: Theme An object containing the theme colors for the current DatoCMS project.

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

View on Github
Toast methods
These methods can be used to show UI-consistent toast notifications to the end-user.
ctx.alert(message: string) => Promise<void> Triggers an "error" toast displaying the selected message.

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);
ctx.notice(message: string) => Promise<void> Triggers a "success" toast displaying the selected 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);
ctx.customToast Triggers a custom toast displaying the selected message (and optionally a CTA).

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!`);
}
Update plugin parameters methods
These methods can be used to update both plugin parameters and manual field extensions configuration.
ctx.updatePluginParameters(params: Record<string, unknown>) => Promise<void> 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.

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!');
ctx.updateFieldAppearance(...) 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.

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}`);
}
Upload dialog methods
These methods let you open the standard DatoCMS dialogs needed to interact with Media Area assets.
ctx.selectUpload 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.

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!');
}
ctx.editUpload(...) 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.

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!');
}
ctx.editUploadMetadata(...) 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.

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!');
}

renderUploadSidebar(sidebarId: string, ctx)

This function will be called when the plugin needs to render a sidebar (see the uploadSidebars hook).

Context object

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

Hook-specific properties and methods

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

ctx.sidebarId: string The ID of the sidebar that needs to be rendered.

The ID of the sidebar that needs to be rendered.

View on Github
ctx.parameters: Record<string, unknown> The arbitrary parameters of the declared in the uploadSidebars function.

The arbitrary parameters of the declared in the uploadSidebars function.

View on Github
ctx.upload: Upload The active asset.

The active asset.

View on Github
Properties and methods available in every hook

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

Authentication properties
Information about the current user using the CMS.
ctx.currentUser: User | SsoUser | Account | Organization The current DatoCMS user. It can either be the owner or one of the collaborators (regular or SSO).

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

View on Github
ctx.currentRole: Role The role for the current DatoCMS user.

The role for the current DatoCMS user.

View on Github
ctx.currentUserAccessToken: string | undefined The access token to perform API calls on behalf of the current user. Only available if currentUserAccessToken additional permission is granted.

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

View on Github
Custom dialog methods
These methods can be used to open custom dialogs/confirmation panels.
ctx.openModal(modal: Modal) => Promise<unknown> Opens a custom modal. Returns a promise resolved with what the modal itself returns calling the resolve() function.

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!');
}
ctx.openConfirm(options: ConfirmOptions) => Promise<unknown> Opens a UI-consistent confirmation dialog. Returns a promise resolved with the value of the choice made by the user.

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!');
}
Entity repos properties
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.
ctx.itemTypes: Partial<Record<string, ItemType>> All the models of the current DatoCMS project, indexed by ID.

All the models of the current DatoCMS project, indexed by ID.

View on Github
ctx.fields: Partial<Record<string, Field>> All the fields currently loaded for the current DatoCMS project, indexed by ID. If some fields you need are not present, use the loadItemTypeFields function to load them.

All the 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
ctx.fieldsets: Partial<Record<string, Fieldset>> All the fieldsets currently loaded for the current DatoCMS project, indexed by ID. If some fields you need are not present, use the loadItemTypeFieldsets function to load them.

All the 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
ctx.users: Partial<Record<string, User>> All the regular users currently loaded for the current DatoCMS project, indexed by ID. It will always contain the current user. If some users you need are not present, use the loadUsers function to load them.

All the 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
ctx.ssoUsers: Partial<Record<string, SsoUser>> 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.

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
Item dialog methods
These methods let you open the standard DatoCMS dialogs needed to interact with records.
ctx.createNewItem(itemTypeId: string) => Promise<Item | null> 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.

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!');
}
ctx.selectItem Opens a dialog for selecting one (or multiple) record(s) from a list of existing records of type itemTypeId. It returns a promise resolved with the selected record(s), or null if the user closes the dialog without choosing any record.

Opens a dialog for 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!');
}
ctx.editItem(itemId: string) => Promise<Item | null> 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.

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!');
}
Load data methods
These methods can be used to asyncronously load additional information your plugin needs to work.
ctx.loadItemTypeFields(itemTypeId: string) => Promise<Field[]> Loads all the fields for a specific model (or block). Fields will be returned and will also be available in the the fields property.

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(', ')}`,
);
ctx.loadItemTypeFieldsets(itemTypeId: string) => Promise<Fieldset[]> Loads all the fieldsets for a specific model (or block). Fieldsets will be returned and will also be available in the the fieldsets property.

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(', ')}`,
);
ctx.loadFieldsUsingPlugin() => Promise<Field[]> Loads all the fields in the project that are currently using the plugin for one of its manual field extensions.

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(', ')}`,
);
ctx.loadUsers() => Promise<User[]> Loads all regular users. Users will be returned and will also be available in the the users property.

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(', ')}`);
ctx.loadSsoUsers() => Promise<SsoUser[]> Loads all SSO users. Users will be returned and will also be available in the the ssoUsers property.

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(', ')}`);
Navigate methods
These methods can be used to take the user to different pages.
ctx.navigateTo(path: string) => Promise<void> Moves the user to another URL internal to the backend.

Moves the user to another URL internal to the backend.

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

The current plugin.

View on Github
Project properties
ctx.site: Site The current DatoCMS project.

The current DatoCMS project.

View on Github
ctx.environment: string The ID of the current environment.

The ID of the current environment.

View on Github
ctx.isEnvironmentPrimary: boolean Whether the current environment is the primary one.

Whether the current environment is the primary one.

View on Github
ctx.owner: Account | Organization The account/organization that is the project owner.

The account/organization that is the project owner.

View on Github
ctx.ui UI preferences of the current user (right now, only the preferred locale is available).

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

View on Github
ctx.theme: Theme An object containing the theme colors for the current DatoCMS project.

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

View on Github
Toast methods
These methods can be used to show UI-consistent toast notifications to the end-user.
ctx.alert(message: string) => Promise<void> Triggers an "error" toast displaying the selected message.

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);
ctx.notice(message: string) => Promise<void> Triggers a "success" toast displaying the selected 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);
ctx.customToast Triggers a custom toast displaying the selected message (and optionally a CTA).

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!`);
}
Update plugin parameters methods
These methods can be used to update both plugin parameters and manual field extensions configuration.
ctx.updatePluginParameters(params: Record<string, unknown>) => Promise<void> 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.

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!');
ctx.updateFieldAppearance(...) 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.

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}`);
}
Upload dialog methods
These methods let you open the standard DatoCMS dialogs needed to interact with Media Area assets.
ctx.selectUpload 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.

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!');
}
ctx.editUpload(...) 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.

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!');
}
ctx.editUploadMetadata(...) 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.

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!');
}

uploadSidebarPanels(ctx)

Use this function to declare new sidebar panels to be shown when the user opens up an asset in the Media Area.

Return value

The function must return: UploadSidebarPanel[].

Context object

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

Properties and methods available in every hook

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

Authentication properties
Information about the current user using the CMS.
ctx.currentUser: User | SsoUser | Account | Organization The current DatoCMS user. It can either be the owner or one of the collaborators (regular or SSO).

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

View on Github
ctx.currentRole: Role The role for the current DatoCMS user.

The role for the current DatoCMS user.

View on Github
ctx.currentUserAccessToken: string | undefined The access token to perform API calls on behalf of the current user. Only available if currentUserAccessToken additional permission is granted.

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

View on Github
Custom dialog methods
These methods can be used to open custom dialogs/confirmation panels.
ctx.openModal(modal: Modal) => Promise<unknown> Opens a custom modal. Returns a promise resolved with what the modal itself returns calling the resolve() function.

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!');
}
ctx.openConfirm(options: ConfirmOptions) => Promise<unknown> Opens a UI-consistent confirmation dialog. Returns a promise resolved with the value of the choice made by the user.

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!');
}
Entity repos properties
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.
ctx.itemTypes: Partial<Record<string, ItemType>> All the models of the current DatoCMS project, indexed by ID.

All the models of the current DatoCMS project, indexed by ID.

View on Github
ctx.fields: Partial<Record<string, Field>> All the fields currently loaded for the current DatoCMS project, indexed by ID. If some fields you need are not present, use the loadItemTypeFields function to load them.

All the 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
ctx.fieldsets: Partial<Record<string, Fieldset>> All the fieldsets currently loaded for the current DatoCMS project, indexed by ID. If some fields you need are not present, use the loadItemTypeFieldsets function to load them.

All the 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
ctx.users: Partial<Record<string, User>> All the regular users currently loaded for the current DatoCMS project, indexed by ID. It will always contain the current user. If some users you need are not present, use the loadUsers function to load them.

All the 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
ctx.ssoUsers: Partial<Record<string, SsoUser>> 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.

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
Item dialog methods
These methods let you open the standard DatoCMS dialogs needed to interact with records.
ctx.createNewItem(itemTypeId: string) => Promise<Item | null> 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.

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!');
}
ctx.selectItem Opens a dialog for selecting one (or multiple) record(s) from a list of existing records of type itemTypeId. It returns a promise resolved with the selected record(s), or null if the user closes the dialog without choosing any record.

Opens a dialog for 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!');
}
ctx.editItem(itemId: string) => Promise<Item | null> 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.

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!');
}
Load data methods
These methods can be used to asyncronously load additional information your plugin needs to work.
ctx.loadItemTypeFields(itemTypeId: string) => Promise<Field[]> Loads all the fields for a specific model (or block). Fields will be returned and will also be available in the the fields property.

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(', ')}`,
);
ctx.loadItemTypeFieldsets(itemTypeId: string) => Promise<Fieldset[]> Loads all the fieldsets for a specific model (or block). Fieldsets will be returned and will also be available in the the fieldsets property.

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(', ')}`,
);
ctx.loadFieldsUsingPlugin() => Promise<Field[]> Loads all the fields in the project that are currently using the plugin for one of its manual field extensions.

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(', ')}`,
);
ctx.loadUsers() => Promise<User[]> Loads all regular users. Users will be returned and will also be available in the the users property.

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(', ')}`);
ctx.loadSsoUsers() => Promise<SsoUser[]> Loads all SSO users. Users will be returned and will also be available in the the ssoUsers property.

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(', ')}`);
Navigate methods
These methods can be used to take the user to different pages.
ctx.navigateTo(path: string) => Promise<void> Moves the user to another URL internal to the backend.

Moves the user to another URL internal to the backend.

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

The current plugin.

View on Github
Project properties
ctx.site: Site The current DatoCMS project.

The current DatoCMS project.

View on Github
ctx.environment: string The ID of the current environment.

The ID of the current environment.

View on Github
ctx.isEnvironmentPrimary: boolean Whether the current environment is the primary one.

Whether the current environment is the primary one.

View on Github
ctx.owner: Account | Organization The account/organization that is the project owner.

The account/organization that is the project owner.

View on Github
ctx.ui UI preferences of the current user (right now, only the preferred locale is available).

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

View on Github
ctx.theme: Theme An object containing the theme colors for the current DatoCMS project.

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

View on Github
Toast methods
These methods can be used to show UI-consistent toast notifications to the end-user.
ctx.alert(message: string) => Promise<void> Triggers an "error" toast displaying the selected message.

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);
ctx.notice(message: string) => Promise<void> Triggers a "success" toast displaying the selected 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);
ctx.customToast Triggers a custom toast displaying the selected message (and optionally a CTA).

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!`);
}
Update plugin parameters methods
These methods can be used to update both plugin parameters and manual field extensions configuration.
ctx.updatePluginParameters(params: Record<string, unknown>) => Promise<void> 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.

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!');
ctx.updateFieldAppearance(...) 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.

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}`);
}
Upload dialog methods
These methods let you open the standard DatoCMS dialogs needed to interact with Media Area assets.
ctx.selectUpload 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.

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!');
}
ctx.editUpload(...) 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.

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!');
}
ctx.editUploadMetadata(...) 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.

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!');
}

renderUploadSidebarPanel(sidebarPaneId: string, ctx)

This function will be called when the plugin needs to render a sidebar panel (see the uploadSidebarPanels hook).

Context object

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

Hook-specific properties and methods

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

ctx.sidebarPaneId: string The ID of the sidebar panel that needs to be rendered.

The ID of the sidebar panel that needs to be rendered.

View on Github
ctx.parameters: Record<string, unknown> The arbitrary parameters of the panel declared in the uploadSidebarPanels function.

The arbitrary parameters of the panel declared in the uploadSidebarPanels function.

View on Github
ctx.upload: Upload The active asset.

The active asset.

View on Github
Properties and methods available in every hook

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

Authentication properties
Information about the current user using the CMS.
ctx.currentUser: User | SsoUser | Account | Organization The current DatoCMS user. It can either be the owner or one of the collaborators (regular or SSO).

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

View on Github
ctx.currentRole: Role The role for the current DatoCMS user.

The role for the current DatoCMS user.

View on Github
ctx.currentUserAccessToken: string | undefined The access token to perform API calls on behalf of the current user. Only available if currentUserAccessToken additional permission is granted.

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

View on Github
Custom dialog methods
These methods can be used to open custom dialogs/confirmation panels.
ctx.openModal(modal: Modal) => Promise<unknown> Opens a custom modal. Returns a promise resolved with what the modal itself returns calling the resolve() function.

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!');
}
ctx.openConfirm(options: ConfirmOptions) => Promise<unknown> Opens a UI-consistent confirmation dialog. Returns a promise resolved with the value of the choice made by the user.

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!');
}
Entity repos properties
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.
ctx.itemTypes: Partial<Record<string, ItemType>> All the models of the current DatoCMS project, indexed by ID.

All the models of the current DatoCMS project, indexed by ID.

View on Github
ctx.fields: Partial<Record<string, Field>> All the fields currently loaded for the current DatoCMS project, indexed by ID. If some fields you need are not present, use the loadItemTypeFields function to load them.

All the 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
ctx.fieldsets: Partial<Record<string, Fieldset>> All the fieldsets currently loaded for the current DatoCMS project, indexed by ID. If some fields you need are not present, use the loadItemTypeFieldsets function to load them.

All the 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
ctx.users: Partial<Record<string, User>> All the regular users currently loaded for the current DatoCMS project, indexed by ID. It will always contain the current user. If some users you need are not present, use the loadUsers function to load them.

All the 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
ctx.ssoUsers: Partial<Record<string, SsoUser>> 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.

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
Item dialog methods
These methods let you open the standard DatoCMS dialogs needed to interact with records.
ctx.createNewItem(itemTypeId: string) => Promise<Item | null> 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.

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!');
}
ctx.selectItem Opens a dialog for selecting one (or multiple) record(s) from a list of existing records of type itemTypeId. It returns a promise resolved with the selected record(s), or null if the user closes the dialog without choosing any record.

Opens a dialog for 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!');
}
ctx.editItem(itemId: string) => Promise<Item | null> 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.

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!');
}
Load data methods
These methods can be used to asyncronously load additional information your plugin needs to work.
ctx.loadItemTypeFields(itemTypeId: string) => Promise<Field[]> Loads all the fields for a specific model (or block). Fields will be returned and will also be available in the the fields property.

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(', ')}`,
);
ctx.loadItemTypeFieldsets(itemTypeId: string) => Promise<Fieldset[]> Loads all the fieldsets for a specific model (or block). Fieldsets will be returned and will also be available in the the fieldsets property.

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(', ')}`,
);
ctx.loadFieldsUsingPlugin() => Promise<Field[]> Loads all the fields in the project that are currently using the plugin for one of its manual field extensions.

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(', ')}`,
);
ctx.loadUsers() => Promise<User[]> Loads all regular users. Users will be returned and will also be available in the the users property.

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(', ')}`);
ctx.loadSsoUsers() => Promise<SsoUser[]> Loads all SSO users. Users will be returned and will also be available in the the ssoUsers property.

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(', ')}`);
Navigate methods
These methods can be used to take the user to different pages.
ctx.navigateTo(path: string) => Promise<void> Moves the user to another URL internal to the backend.

Moves the user to another URL internal to the backend.

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

The current plugin.

View on Github
Project properties
ctx.site: Site The current DatoCMS project.

The current DatoCMS project.

View on Github
ctx.environment: string The ID of the current environment.

The ID of the current environment.

View on Github
ctx.isEnvironmentPrimary: boolean Whether the current environment is the primary one.

Whether the current environment is the primary one.

View on Github
ctx.owner: Account | Organization The account/organization that is the project owner.

The account/organization that is the project owner.

View on Github
ctx.ui UI preferences of the current user (right now, only the preferred locale is available).

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

View on Github
ctx.theme: Theme An object containing the theme colors for the current DatoCMS project.

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

View on Github
Sizing utilities
A number of methods that you can use to control the size of the plugin frame.
ctx.startAutoResizer() => void Listens for DOM changes and automatically calls setHeight when it detects a change. If you're using datocms-react-ui package, the `` component already takes care of calling this method for you.

Listens for DOM changes and automatically calls setHeight when it detects a change. If you're using datocms-react-ui package, the <Canvas /> component already takes care of calling this method for you.

View on Github
ctx.stopAutoResizer() => void Stops resizing the iframe automatically.

Stops resizing the iframe automatically.

View on Github
ctx.updateHeight(newHeight?: number) => void Triggers a change in the size of the iframe. If you don't explicitely pass a newHeight it will be automatically calculated using the iframe content at the moment.

Triggers a change in the size of the iframe. If you don't explicitely pass a newHeight it will be automatically calculated using the iframe content at the moment.

View on Github
Toast methods
These methods can be used to show UI-consistent toast notifications to the end-user.
ctx.alert(message: string) => Promise<void> Triggers an "error" toast displaying the selected message.

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);
ctx.notice(message: string) => Promise<void> Triggers a "success" toast displaying the selected 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);
ctx.customToast Triggers a custom toast displaying the selected message (and optionally a CTA).

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!`);
}
Update plugin parameters methods
These methods can be used to update both plugin parameters and manual field extensions configuration.
ctx.updatePluginParameters(params: Record<string, unknown>) => Promise<void> 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.

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!');
ctx.updateFieldAppearance(...) 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.

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}`);
}
Upload dialog methods
These methods let you open the standard DatoCMS dialogs needed to interact with Media Area assets.
ctx.selectUpload 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.

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!');
}
ctx.editUpload(...) 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.

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!');
}
ctx.editUploadMetadata(...) 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.

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!');
}