# Contentstack parameters

> After you add the Contentstack integration to your Uniform project, new parameter types will be available.

Source: https://docs.uniform.app/docs/integrations/content/contentstack/contentstack-classic/contentstack-in-uniform/contentstack-parameters

After you add the Contentstack integration to your Uniform
project, a number of new parameter types will be available
when you configure components using Uniform Canvas.

| Parameter type | Description |
| --- | --- |
| [Contentstack Entry Selector](#contentstack-entry-selector) | Select a single entry from Contentstack. |
| [Contentstack Multi Entry Selector](#contentstack-multi-entry-selector) | Select multiple entries from Contentstack. |
| [Contentstack Query Entry Selector](#contentstack-query-entry-selector) | Specify search criteria that's used to select a single or multiple entries from Contentstack. |

## Contentstack Entry Selector

This parameter type allows a Uniform user to select a single
Contentstack entry.

### Add parameter to component

To allow a user to select an entry from Contentstack,
you must add a parameter to a component. The parameter is used
to store the identifier to the selected entry when the user
selects an entry.

1. In Uniform, navigate to your component.
2. Add a new parameter using the parameter type **Contentstack Entry Selector**.
3. The following values can be specified:

   | Name | Description |
   | --- | --- |
   | Allowed Content Types | Specifies which content types will be available to select from when the parameter value is edited. You can select multiple content types to allow the user to select from a larger set of entries. |
   | Required | Indicates whether the value is required when the component is used. |

   ![Contentstack Entry Selector parameter with a single content type selected.](https://docs.uniform.app/images/integrations/content/contentstack/contentstack-in-uniform/use-parameters/entry-selector/parameter-config.png)

### Edit parameter value

When you use a component with a Contentstack Entry Selector
parameter, by default no entry will be selected. You are
prompted to select an entry.

1. Click **Select**.

   ![](https://docs.uniform.app/images/integrations/content/contentstack/contentstack-in-uniform/use-parameters/entry-selector/edit-value-default.png)
2. Click the entry you want to select.

   ![](https://docs.uniform.app/images/integrations/content/contentstack/contentstack-in-uniform/use-parameters/entry-selector/edit-value-filters.png)

   > **About this step:**
   >
   > A couple of filters are available. The dropdown allows you to filter by content type. The text box allows you to filter by entry title.
3. Click **Accept** to save your selection.

   ![](https://docs.uniform.app/images/integrations/content/contentstack/contentstack-in-uniform/use-parameters/entry-selector/edit-value-selected.png)
4. You will see details about the entry you selected, including the title and some metadata.

   ![](https://docs.uniform.app/images/integrations/content/contentstack/contentstack-in-uniform/use-parameters/entry-selector/edit-value-saved.png)

   > **About this step:**
   >
   > After your selection is saved, you are presented with the following options:
   >
   > - If you want to edit the selected entry in Contentstack, click **Edit**.
   > - If you want to deselect the entry, click **Unlink**.

### Configure an enhancer

When an entry is selected, Uniform only stores the identifier
for the entry. Your front-end application must retrieve the
details for the entry. Uniform provides an enhancer to
simplify this process.

> **Tip:**
>
> Using the enhancer provided by Uniform saves you from having
> to write logic to interact directly with Contentstack.

#### How Uniform stores the selected entry

The following is an example of what Uniform
stores for the parameter.

```json
{
  "type": "contentstackEntrySelector",
  "value": {
    "entryUid": "###################",
    "contentTypeUid": "###################"
  }
}
```

There are two identifiers in this value:

1. The entry that was selected.
2. The content type for the selected entry.

#### Add the enhancer

Uniform provides an enhancer so you don't need to write the API
calls to Contentstack to retrieve data for entries.

1. In Contentstack, create an API key and delivery token for the
   enhancer to use to make API calls to Contentstack.
2. Add the following npm packages to your front-end application:

   ```bash
   npm install contentstack
   ```

   ```bash
   npm install @uniformdev/canvas
   ```

   ```bash
   npm install @uniformdev/canvas-contentstack
   ```
3. In a text editor, open the file where you retrieve the
   composition definition from Uniform.

   > **About this step:**
   >
   > You are looking for the code calls the async function
   > `getComposition`. The code below assumes the
   > object returned is set in a variable `composition`.
4. Add the following import statements (highlighted lines represent Contentstack specific additions):

   ```javascript
   import { EnhancerBuilder, enhance } from '@uniformdev/canvas';
   import contentstack from 'contentstack';
   import { 
       createContentstackEnhancer,
       CANVAS_CONTENTSTACK_PARAMETER_TYPES,
   } from '@uniformdev/canvas-contentstack';
   ```
5. Create an instance of Contentstack enhancer (depending on your region):

   **US**

   ```javascript
   const csClient = contentstack.Stack({
       api_key: '!!! YOUR CONTENTSTACK API KEY !!!',
       delivery_token: '!!! YOUR CONTENTSTACK DELIVERY TOKEN !!!',
       environment: '!!! YOUR CONTENTSTACK ENVIRONMENT !!!',
       region: contentstack.Region.US,
   });
   ```

   **EU**

   ```javascript
   const csClient = contentstack.Stack({
       api_key: '!!! YOUR CONTENTSTACK API KEY !!!',
       delivery_token: '!!! YOUR CONTENTSTACK DELIVERY TOKEN !!!',
       environment: '!!! YOUR CONTENTSTACK ENVIRONMENT !!!',
       region: contentstack.Region.EU,
   });
   ```

   > **About this step:**
   >
   > Please use environment variables here (`process.env.CONTENTSTACK_KEY` for example) instead of hard-coded values.
6. Create an instance of enhancer for Entry parameters:

   ```javascript
   const entryEnhancer = createContentstackEnhancer({ client: csClient });
   ```
7. Register the enhancer for this parameter type with `EnhancerBuilder`. This registers the enhancer to be used for any occurrence of the Contentstack Entry Selector parameter.

   ```javascript
   await enhance({
     composition,
     enhancers: new EnhancerBuilder()
       .parameterType(
         CANVAS_CONTENTSTACK_PARAMETER_TYPES,
         entryEnhancer
       ),
     context: {},
   });
   ```

> **Next steps:**
>
> Now the parameter value in the composition is mutated to
> include the field values for the selected Contentstack
> entry (instead of just being identifiers).

## Contentstack Multi Entry Selector

This parameter type is [added](#add-parameter-to-component) and
[edited](#edit-parameter-value) the same way as [Contentstack Entry Selector](#contentstack-entry-selector).
When editing the parameter value, you can select additional entries
by clicking the button **Select new**.

![Example of the editing interface for an Contentstack Multi Entry Selector parameter on a component.](https://docs.uniform.app/images/integrations/content/contentstack/contentstack-in-uniform/use-parameters/multi-entry-selector.png)

### Configure an enhancer

When entries are selected, Uniform only stores the identifiers
for the entries. Your front-end application must retrieve the
details for the entries. Uniform provides an enhancer to
simplify this process.

> **Tip:**
>
> Using the enhancer provided by Uniform saves you from having
> to write logic to interact directly with Contentstack.

#### How Uniform stores the selected entries

The following is an example of what Uniform
stores for the parameter.

```json
{
  "type": "contentstackMultiEntrySelector",
  "value": {
    "entries": [
      {
        "entryUid": "###################",
        "contentTypeUid": "places"
      },
      {
        "entryUid": "###################",,
        "contentTypeUid": "places"
      }
    ]
  }
}
```

#### Add the enhancer

Uniform provides an enhancer so you don't need to write the API
calls to Contentstack to retrieve data for entries.

1. In Contentstack, create an API key and delivery token for the
   enhancer to use to make API calls to Contentstack.
2. If you haven't already, add the following npm packages to your front-end application:

   ```bash
   npm install contentstack
   ```

   ```bash
   npm install @uniformdev/canvas
   ```

   ```bash
   npm install @uniformdev/canvas-contentstack
   ```
3. In a text editor, open the file where you retrieve the
   composition definition from Uniform.

   > **About this step:**
   >
   > You are looking for the code calls the async function
   > `getComposition`. The code below assumes the
   > object returned is set in a variable `composition`.
4. Add the following import statements, notice we import a different enhancer function `createContentstackMultiEnhancer` this time:

   ```javascript
   import contentstack from 'contentstack';
   import { EnhancerBuilder, enhance } from '@uniformdev/canvas';
   import { createContentstackMultiEnhancer,
            CANVAS_CONTENTSTACK_MULTI_PARAMETER_TYPES }
          from '@uniformdev/canvas-contentstack';
   ```
5. Create an instance of Contentstack client (depending on your region):

   **US**

   ```javascript
   const csClient = contentstack.Stack({
       api_key: '!!! YOUR CONTENTSTACK API KEY !!!',
       delivery_token: '!!! YOUR CONTENTSTACK DELIVERY TOKEN !!!',
       environment: '!!! YOUR CONTENTSTACK ENVIRONMENT !!!',
       region: contentstack.Region.US,
   });
   ```

   **EU**

   ```javascript
   const csClient = contentstack.Stack({
       api_key: '!!! YOUR CONTENTSTACK API KEY !!!',
       delivery_token: '!!! YOUR CONTENTSTACK DELIVERY TOKEN !!!',
       environment: '!!! YOUR CONTENTSTACK ENVIRONMENT !!!',
       region: contentstack.Region.EU,
   });
   ```

   > **About this step:**
   >
   > Please use environment variables here (`process.env.CONTENTSTACK_KEY` for example) instead of hard-coded values.
6. Create an instance of the multi enhancer by passing the `client` into it:

   ```javascript
   const multiEntryEnhancer = createContentstackMultiEnhancer({ client: csClient });
   ```
7. Add `contentstackMultiEnhancer` to `EnhancerBuilder`. This step registers the enhancer to be used for any occurrence of the Contentstack Multi Entry Selector parameter:

   ```javascript
   await enhance({
     composition,
     enhancers: new EnhancerBuilder()
       .parameterType(
         CANVAS_CONTENTSTACK_MULTI_PARAMETER_TYPES,
         multiEntryEnhancer
       ),
     context: {},
   });
   ```

> **Next steps:**
>
> Now the parameter value in the composition is mutated to
> include the field values for the selected Contentstack
> entries (instead of just being identifiers).

## Contentstack Query Entry Selector

This parameter type allows a Uniform user to configure a query
that will determine the Contentstack entries that are retrieved.

### Add parameter to component

This parameter type is [added](#add-parameter-to-component) the same way as [Contentstack Entry Selector](#contentstack-entry-selector).

### Edit parameter value

Configuring this parameter type involves specifying the way
a product search should work. You are able to control the
following search settings:

| Field | Description |
| --- | --- |
| Filter by entry type | Only entries that match the selected entry type are included. |
| Count | The maximum number of entries that are returned. |
| Sort | The entry field used to sort the entries that are returned. |
| Sort Order | Whether the entries should be sorted in ascending or descending order. |

![Example of the editing interface for a Contentstack Query Entry Selector parameter on a component.](https://docs.uniform.app/images/integrations/content/contentstack/contentstack-in-uniform/use-parameters/query-entry-selector/edit-parameter-value.png)

### Configure an enhancer

When an entry query is configured, Uniform only stores the
search criteria, not the results of the search. Your
front-end application must perform the search using
that criteria. Uniform provides an enhancer to simplify
this process.

> **Tip:**
>
> Using the enhancer provided by Uniform saves you from having
> to write logic to interact directly with Contentstack.

#### How Uniform stores the selected settings

The following is an example of what Uniform
stores for the parameter.

```json
{
  "type": "contentstackQueryEntrySelector",
  "value": {
    "contentType": {
      "id": "places",
      "name": "Places"
    },
    "limit": 2,
    "sortOrder": "asc"
  }
}
```

There are three identifiers in this value:

1. The content type to use in the search.
2. The maximum number of entries to search for.
3. The sort order applies to the search results.

#### Add the enhancer

Uniform provides an enhancer so you don't need to write the API
calls to Contentstack to retrieve data for entries.

1. In Contentstack, create an API key and delivery token for the
   enhancer to use to make API calls to Contentstack.
2. If you haven't already, add the following npm packages to your front-end application:

   ```bash
   npm install contentstack
   ```

   ```bash
   npm install @uniformdev/canvas
   ```

   ```bash
   npm install @uniformdev/canvas-contentstack
   ```
3. In a text editor, open the file where you retrieve the
   composition definition from Uniform.

   > **About this step:**
   >
   > You are looking for the code calls the async function
   > `getComposition`. The code below assumes the
   > object returned is set in a variable `composition`.
4. Add the following import statements:

   ```javascript
   import contentstack from 'contentstack';
   import { EnhancerBuilder, enhance } from '@uniformdev/canvas';
   import { 
       createContentstackQueryEnhancer,
       CANVAS_CONTENTSTACK_QUERY_PARAMETER_TYPES,
   } from '@uniformdev/canvas-contentstack';
   ```
5. Create an instance of Contentstack client (depending on your region):

   **US**

   ```javascript
   const csClient = contentstack.Stack({
       api_key: '!!! YOUR CONTENTSTACK API KEY !!!',
       delivery_token: '!!! YOUR CONTENTSTACK DELIVERY TOKEN !!!',
       environment: '!!! YOUR CONTENTSTACK ENVIRONMENT !!!',
       region: contentstack.Region.US,
   });
   ```

   **EU**

   ```javascript
   const csClient = contentstack.Stack({
       api_key: '!!! YOUR CONTENTSTACK API KEY !!!',
       delivery_token: '!!! YOUR CONTENTSTACK DELIVERY TOKEN !!!',
       environment: '!!! YOUR CONTENTSTACK ENVIRONMENT !!!',
       region: contentstack.Region.EU,
   });
   ```

   > **About this step:**
   >
   > Please use environment variables here (`process.env.CONTENTSTACK_KEY` for example) instead of hard-coded values.
6. Create an instance of enhancer for Query parameters:

   ```javascript
   const queryEnhancer = createContentstackQueryEnhancer({ client: csClient });
   ```
7. Register this enhancer with `EnhancerBuilder`:

   ```javascript
   await enhance({
     composition,
     enhancers: new EnhancerBuilder()
       .parameterType(
         CANVAS_CONTENTSTACK_QUERY_PARAMETER_TYPES,
         queryEnhancer
       ),
     context: {},
   });
   ```

   > **About this step:**
   >
   > This registers the enhancer to be used for any occurrence
   > of the Contentstack Query Entry Selector parameter.

> **Next steps:**
>
> Now the parameter value in the composition is mutated to
> include the field values for the selected Contentstack
> entries (instead of just being settings).
