Sorry, no results found for "".

Plugin SDK > Customize record presentation

Customize record presentation

When viewing a collection of items, the records will normally show their title and possibly an image preview (as defined in the model's presentation settings). In this example, the record previews come from the record's Name field:

A product listing in Compact view, using the Name field as the title

But sometimes you may want more advanced control over the presentation of your collections. For example, you might want to make the title dynamically change based on another field in the record or an external API query.

Basic Example: Data from another field

Maybe you want to show an emoji next to the product name based on its product type:

The same product listing, now with an emoji next to each title

This change is purely cosmetic & superficial, affecting only what your editors see in the collection. It does NOT change the actual data in the record, only its presentation inside the DatoCMS UI.

How does it work? We used the buildItemPresentationInfo hook:

import {type BuildItemPresentationInfoCtx, connect, Item} from "datocms-plugin-sdk";
// A schema for our basic example
type ProductRecord = Item & {
attributes: {
name: 'string'
product_type?: 'apple' | 'orange'
}
}
// This checks to make sure an item is a product based on its API key, and if it is, assert that it is a ProductRecord
function isProductRecord(item: Item, ctx: BuildItemPresentationInfoCtx): item is ProductRecord {
return ctx.itemTypes[item.relationships.item_type.data.id]?.attributes.api_key === 'product';
}
connect({
async buildItemPresentationInfo(item: Item, ctx: BuildItemPresentationInfoCtx) {
// We only want to override records in the `product` model
if (!isProductRecord(item, ctx)) {
return undefined; // Return undefined to let the record use its default values
}
// Get the record fields
const {attributes: {product_type, name}} = item
const fruitEmoji = {
'apple': '🍎',
'orange': '🍊',
'unknown': '❓'
}
return {
title: `${product_type ? fruitEmoji[product_type] : fruitEmoji['unknown']} ${name}`,
}
},
});

This level of flexibility empowers you to create a unique and tailored user experience that aligns with your goals.

The buildItemPresentationInfo hook can be used in numerous ways. For example, you can:

  • Combine multiple fields to present a record

  • Generate a preview image on the fly

  • Perform asynchronous API requests to third parties to compose the presentation

These are just a few examples of what you can do with the buildItemPresentationInfo hook. The possibilities are limitless, and you can use this hook to create the exact presentation you need.

The buildItemPresentationInfo hook is called every time a record needs to be presented, and it can return an object with title and/or imageUrl attributes, or undefined, if the plugin does not want to interfere with the default presentation at all.

imageUrl can also be a Data URL

While the imageUrl attribute normally is a normal URL starting with https://, you can also pass a Data URL. Data URLs can be useful to generate an image on-the-fly in JavaScript (for example, using canvases).

Advanced Example: Data from an async fetch

Suppose that one of the models in a DatoCMS project is used to represent products in a ecommerce frontend, and that each product record in DatoCMS is linked to a particular Shopify product via its handle.

Shopify holds information like inventory availability, prices and variant images. We don't want to replicate the same information in DatoCMS, but it would be nice to show them inside the DatoCMS interface.

Since the buildItemPresentationInfo hook can be an async function, we can make a fetch call to the Shopify Storefront API (or any other API) and use its response in our collection display.

We'll modify our previous example to show use the result of this fetch instead, based on a new field shopify_product_handle (which holds an external ID) and a fake function fetchShopifyProduct() (simulating an external fetch):

Mockup of third-party data
// Updated schema
type ProductRecord = Item & {
attributes: {
name: 'string'
shopify_product_handle: string // A new required field
// product_type?: 'apple' | 'orange' // No longer needed in the modified example
}
}
// Updated hook
connect({
async buildItemPresentationInfo(item: Item, ctx: BuildItemPresentationInfoCtx) {
// Same function as before
if (!isProductRecord(item, ctx)) {
return undefined;
}
// Get the new field
const {attributes: {name, shopify_product_handle}} = item
// Just an example. In a real use case this would be an awaited fetch.
const shopifyData = await fetchShopifyProduct(shopify_product_handle);
const { imageUrl, availableForSale } = shopifyData;
return {
title: `${name} (${availableForSale ? '🛍️' : '🚫'})`,
imageUrl,
}
},
})

The above is a simplified example using a fake fetch function. In a real project, to perform the actual API call to Shopify, we would need to implement a real fetch function using a real API token and the Shopify store domain. Both can be specified by the final user by adding some settings to the plugin.

A more realistic fetchShopifyProduct function might be something like this:

import { Plugin } from "datocms-plugin-sdk";
type PluginParameters = {
shopifyDomain: string;
shopifyAccessToken: string;
}
async function fetchShopifyProduct(handle: string, plugin: Plugin) {
const parameters = plugin.attributes.parameters as PluginParameters;
const res = await fetch(
`https://${parameters.shopifyDomain}.myshopify.com/api/graphql`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Shopify-Storefront-Access-Token': `${parameters.shopifyAccessToken}`,
},
body: JSON.stringify({
query: `query getProduct($handle: String!) {
product: productByHandle(handle: $handle) {
title
availableForSale
images(first: 1) {
edges {
node {
src: transformedSrc(crop: CENTER, maxWidth: 200, maxHeight: 200)
}
}
}
}
}`,
variables: { handle },
}),
},
);
const body = await res.json();
return {
title: body.data.product.title,
availableForSale: body.data.product.availableForSale,
imageUrl: body.data.product.images.edges[0].node.src,
};
}

buildItemPresentationInfo(item: Item, ctx)

Use this function to customize the presentation of a record in records collections and "Single link" or "Multiple links" field.

Return value

The function must return: MaybePromise<ItemPresentationInfo | undefined>.

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