From 1c0fa94c7bfc0fe1bf77d429a4ea376ea9e4519e Mon Sep 17 00:00:00 2001 From: Vincent Bocquel Date: Thu, 30 Apr 2026 12:14:34 +0200 Subject: [PATCH] feat: add model type helper --- README.md | 10 ++++++++-- src/lib/executeComputedCode.ts | 11 +++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e157c31..6ee9699 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ * Return specified values * Use all javascript functionality -* Get a model and an upload with an ID +* Get a model, a model schema, and an upload with an ID * Have access to all fields on the page ## Configuration @@ -59,7 +59,7 @@ This will hide the field, but will not hide the title. To hide the title you cou ### Plugin helper functions -`getModel(modelId)`, `getUpload(uploadId)` and `getFieldValue(formValues, fieldPath)` are functions to use in the plugin. When you have added the DatoCMS readonly token in the general settings of the plugin, you can use these two functions. +`getModel(modelId)`, `getModelType(itemTypeId)`, `getUpload(uploadId)` and `getFieldValue(formValues, fieldPath)` are functions to use in the plugin. When you have added the DatoCMS readonly token in the general settings of the plugin, you can use these functions. For example: When there is an `uploadId` you can use this function to get all data for this upload. ```js @@ -73,6 +73,12 @@ const model = await getModel(modelId) return model.title ``` +For example: When there is an `itemTypeId` you can use this function to get the model schema (item type). +```js +const itemType = await getModelType(itemTypeId) +return itemType.name +``` + For example: To get the value of a field you can use the datoCmsPlugin variable. ```js const fieldValue = getFieldValue(datoCmsPlugin.formValues, datoCmsPlugin.fieldPath) diff --git a/src/lib/executeComputedCode.ts b/src/lib/executeComputedCode.ts index 2a4c080..c8a763d 100644 --- a/src/lib/executeComputedCode.ts +++ b/src/lib/executeComputedCode.ts @@ -5,6 +5,7 @@ import { buildClient } from '@datocms/cma-client-browser' interface Variables { getUpload: (uploadId: string) => any getModel: (modelId: string) => any + getModelType: (itemTypeId: string) => any getFieldValue: (object: object, fieldName: string) => any changedField: string | undefined locale: string @@ -44,6 +45,15 @@ export default async function executeComputedCode( return datoClient.items.find(modelId) } + function getModelType(itemTypeId: string) { + if (!datoClient) { + console.error(accessTokenError) + return accessTokenError + } + + return datoClient.itemTypes.find(itemTypeId) + } + let thisBlock = null const fieldPath = ctx.fieldPath const indexOfDot = fieldPath.lastIndexOf('.') @@ -55,6 +65,7 @@ export default async function executeComputedCode( const variables: Variables = { getUpload: getUpload, getModel: getModel, + getModelType: getModelType, getFieldValue: getFieldValue, changedField: changedField, locale: ctx.locale,