Use parameters
After you install the Prismic integration, the following parameter types are available. You can use these to incorporate content from Prismic into your components and compositions.
Parameter type | Description |
---|---|
Prismic Document | Select a single document from Prismic. |
Prismic Document
This parameter type allows a Uniform user to select a single Prismic document.
Add parameter to component
In order to allow a user to select a document from Prismic, you must add a parameter to a component. The parameter is used to store the identifier to the selected document when the user selects a document.
In Uniform, navigate to your component.
Add a new parameter using the parameter type Prismic Document.
The following values can be specified:
Name Description Select Linked Source Specifies which Prismic project is the source for the document that can be selected. Allowed document types Specifies which document types will be available to select from when the parameter value is edited. You can select multiple document types in order to allow the user to select from a larger set of documents. Required Indicates whether the value is required when the component is used. Prismic Document parameter with a single document type selected.
Edit parameter value
When you use a component with a Prismic parameter, by default no document will be selected. You are prompted to select a document.
Click Select.
Click the document you want to select.
About this stepA couple of filters are available. The dropdown allows you to filter by document type. The text box allows you to filter by document content.
Click Accept to save your selection.
You will see details about the document you selected, including the title and some metadata.
About this stepAfter your selection is saved, you are presented with the following options:
- If you want to edit the selected document in Prismic, click Edit.
- If you want to unselect the document, click Unlink.
Configure an enhancer
When a document is selected, Uniform only stores the identifier for the document. Your front-end application must retrieve the details for the document. Uniform provides an enhancer to simplify this process.
Using the enhancer provided by Uniform saves you from having to write logic to interact directly with Prismic.
How Uniform stores the selected document
The following is an example of what Uniform stores for the parameter.
{
"type": "prismicEntry",
"value": {
"entryId": "######################",
"source": "######################"
}
}
There are two identifiers in this value:
- The document that was selected.
- The source key for the linked content source.
Add the enhancer
Uniform provides an enhancer so you don't need to write the API calls to Prismic to retrieve data for documents.
For more information about the enhancer, see the package documentation.
In Prismic, get the following values:
- Access token (required if your API is secured)
- Repository ID
Add the following npm packages to your front-end application:
@prismicio/client
@uniformdev/canvas-prismic
prismic-domIn a text editor, open the file where you retrieve the composition definition from Uniform.
About this stepYou are looking for the code calls the async function
getComposition
. The code below assumes the object returned is set in a variablecomposition
.Add the following import statements:
import { EnhancerBuilder, enhance } from '@uniformdev/canvas';
import {
createPrismicEnhancer,
PrismicClientList,
CANVAS_PRISMIC_PARAMETER_TYPES,
} from '@uniformdev/canvas-prismic';
import * as Prismic from '@prismicio/client';
import PrismicDOM from 'prismic-dom';Add the following code:
const endpoint = Prismic.getEndpoint('!!! YOUR PRISMIC REPOSITORY ID !!!');
const client = Prismic.createClient(endpoint, {
accessToken: '!!! YOUR PRISMIC ACCESS TOKEN (OPTIONAL) !!!',
});About this stepWe recommend you moving the Prismic credentials to environment variables rather than hard-coding them in the front-end app.
Add the following code:
const clientList = new PrismicClientList({ client });
const prismicEnhancer = createPrismicEnhancer({ clients: clientList });Add the following code:
await enhance({
composition,
enhancers: new EnhancerBuilder().parameterType(
CANVAS_PRISMIC_PARAMETER_TYPES,
prismicEnhancer
),
context: {},
});About this stepThis registers the enhancer to be used for any occurrence of the Prismic Document parameter.
Now, the parameter value in the composition is mutated to include the field values for the selected Prismic document (instead of just being identifiers).