While the DatoCMS interface does offer basic controls for specifying which fields of a model should be used as the record title and "image preview" within the interface, you may encounter scenarios where you need more advanced control over the presentation of your content.
To address this, you can use the buildItemPresentationInfo hook, which enables you to customize the presentation of your records according to your specific needs. 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 imageUrl attributes, or undefined, if the plugin does not want to interfere with the default presentation:
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).
A basic example
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 easily make a fetch call to the Shopify Storefront API and use this information:
To perform the actual API call to Shopify, we need an API token and the Shopify store domain. Both can be specified by the final user by adding some settings to the plugin.
'Lorem Ipsum is simply dummy text of the printing and typesetting industry',
5
choices: [
6
{
7
label:'Positive',
8
value:'positive',
9
intent:'positive',
10
},
11
{
12
label:'Negative',
13
value:'negative',
14
intent:'negative',
15
},
16
],
17
cancel:{
18
label:'Cancel',
19
value:false,
20
},
21
});
22
23
if (result) {
24
ctx.notice(`Success! ${result}`);
25
}else{
26
ctx.alert('Cancelled!');
27
}
These properties provide access to "entity repos", that is, the collection of
resources of a particular type that have been loaded by the CMS up to this
moment. The entity repos are objects, indexed by the ID of the entity itself.
All the models of the current DatoCMS project, indexed by ID.
All the fields currently loaded for the current DatoCMS project, indexed by
ID. If some fields you need are not present, use the loadItemTypeFields
function to load them.
All the fieldsets currently loaded for the current DatoCMS project, indexed
by ID. If some fields you need are not present, use the
loadItemTypeFieldsets function to load them.
All the regular users currently loaded for the current DatoCMS project,
indexed by ID. It will always contain the current user. If some users you
need are not present, use the loadUsers function to load them.
All the SSO users currently loaded for the current DatoCMS project, indexed
by ID. It will always contain the current user. If some users you need are
not present, use the loadSsoUsers function to load them.
These methods let you open the standard DatoCMS dialogs needed to interact
with records.
Opens a dialog for creating a new record. It returns a promise resolved
with the newly created record or null if the user closes the dialog
without creating anything.
const itemTypeId =prompt('Please insert a model ID:');
2
3
const item =await ctx.createNewItem(itemTypeId);
4
5
if (item) {
6
ctx.notice(`Success! ${item.id}`);
7
}else{
8
ctx.alert('Closed!');
9
}
Opens a dialog for selecting one (or multiple) record(s) from a list of
existing records of type itemTypeId. It returns a promise resolved with
the selected record(s), or null if the user closes the dialog without
choosing any record.
Opens a dialog for editing an existing record. It returns a promise
resolved with the edited record, or null if the user closes the dialog
without persisting any change.
These methods can be used to update both plugin parameters and manual field
extensions configuration.
Updates the plugin parameters.
Always check ctx.currentRole.meta.final_permissions.can_edit_schema
before calling this, as the user might not have the permission to perform
the operation.
Performs changes in the appearance of a field. You can install/remove a
manual field extension, or tweak their parameters. If multiple changes are
passed, they will be applied sequencially.
Always check ctx.currentRole.meta.final_permissions.can_edit_schema
before calling this, as the user might not have the permission to perform
the operation.
ctx.notice(`Successfully edited field ${field.attributes.api_key}`);
36
}
These methods let you open the standard DatoCMS dialogs needed to interact
with Media Area assets.
Opens a dialog for selecting one (or multiple) existing asset(s). It
returns a promise resolved with the selected asset(s), or null if the
user closes the dialog without selecting any upload.
const uploadId =prompt('Please insert an asset ID:');
2
3
const item =await ctx.editUpload(uploadId);
4
5
if (item) {
6
ctx.notice(`Success! ${item.id}`);
7
}else{
8
ctx.alert('Closed!');
9
}
Opens a dialog for editing a "single asset" field structure. It returns a
promise resolved with the updated structure, or null if the user closes
the dialog without persisting any change.