Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions src/lib/executeComputedCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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('.')
Expand All @@ -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,
Expand Down