Use parameters
After you install the Cloudinary integration, the following parameter types are available. You can use these to incorporate content from Cloudinary into your components and compositions.
Parameter type | Description |
---|---|
Cloudinary | Select one or more media items from Cloudinary. |
Cloudinary
This parameter type allows a Uniform user to select one or more Cloudinary media items.
Add parameter to component
In order to allow a user to select one or more media items from Cloudinary, you must add a parameter to a component. The parameter is used to store the identifiers to the selected media items when the user selects media items.
In Uniform, navigate to your component.
Add a new parameter using the parameter type Cloudinary Items.
The configuration parameters are all optional.
Cloudinary parameter with no options specified.
Edit parameter value
When you use a component with an Cloudinary parameter, by default no media item will be selected. You are prompted to add a media item.
Click Add media item.
Click the item you want to select and click Insert.
You will see details about the item 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 item in Cloudinary, click Edit.
- If you want to unselect the item, click Unlink.
- You can select additional media items by clicking Add media item.
Configure an enhancer
When an item is selected, Uniform only stores the identifier for the item. Your front-end application must retrieve the details for the item. 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 Cloudinary.
How Uniform stores the selected item
The following is an example of what Uniform stores for the parameter.
{
"type": "cloudinary-media-selector",
"value": [
{
"publicId": "######################",
"resourceType": "image",
"createdAt": "######################",
"width": "0000",
"height": "0000",
"format": "jpg",
"url": "######################",
"version": 0000000000,
"previewUrl": "######################",
"options": "",
"widths": "",
"ratio": "",
"transformation": "",
"globalImageOptions": "",
"alt": "",
"caption": ""
}
]
}
Add the enhancer
Uniform provides an enhancer so you don't need to write the API calls to Cloudinary to retrieve data for media items.
For more information about the enhancer, see the package documentation.
Add the following npm packages to your front-end application:
@uniformdev/canvas-cloudinary
In 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 {
createCloudinaryEnhancer,
CLOUDINARY_PARAMETER_TYPES,
} from '@uniformdev/canvas-cloudinary';Add the following code:
await enhance({
composition,
enhancers: new EnhancerBuilder().parameterType(
CLOUDINARY_PARAMETER_TYPES,
createCloudinaryEnhancer(),
),
context: {},
});About this stepThis registers the enhancer to be used for any occurrence of the Cloudinary Items parameter.
Now, the parameter value in the composition is mutated to include the field values for the selected Cloudinary media item (instead of just being an identifier).