Dynamic Enumeration
Add a new type of enumeration that works like a combobox, where you can get data already registered in the enumeration previously, or add a new item to select list.
Dynamic Enumeration
Introduction | Features | Installation | Configuration | Api | License
:tada: Introduction
This plugin adds a new type of enumeration that works like a combobox, where you can get data already registered in the enumeration previously, or add a new item to the select options.
:page_facing_up: Features
- New field
Dynamic Enumerationappears in admincontent-type-builderpage, in thecustomtab. - Save only registration options and remove options that is not being used.
- Works inside
ComponentsandDynamic Zones. - Separates each field options by its
APIorComponent - You can create
Globalsfields, which keep the same list between differentAPIandComponents - You can load the options in plugin
APIorService. For list the data in your frontend, for example.
:cd: Installation
Install this plugin by adding it to the dependencies of your Strapi project.
# Yarn
yarn add strapi-plugin-dynamic-enumeration-field
# NPM
npm install strapi-plugin-dynamic-enumeration-field
# PNPM
pnpm install strapi-plugin-dynamic-enumeration-fieldAfter that you must add it to your plugin configuration file.
:wrench: Configuration
Add the code below to your ./config/plugins.js file:
1module.exports = ({ env }) => ({
2 // ...
3 'dynamic-enumeration': {
4 enabled: true,
5 },
6 // ...
7});After that, you'll need to build your admin panel:
npm run buildAdding globals field
You can add several global field keys in the config part of the plugin settings.
1module.exports = ({ env }) => ({
2 // ...
3 'dynamic-enumeration': {
4 enabled: true,
5 config: {
6 globals: {
7 // Unique Key Name
8 'my-global-enumeration': {
9 // Frontend Name
10 name: 'My Global Enumeration',
11 // Fixed default values
12 defaults: ['Strapi', 'Node.js', 'React.js'],
13 },
14 // ...Others globals fields
15 },
16 },
17 },
18 // ...
19});:book: Api
The plugin adds two Api and Service to your application:
Get Enumeration Values
You can get the options added to a field, sending the uid of the API or Component, and the name of the field.
The uid It is in the format api::<api>.<api> for api, and <component-folder>.<component-file-name> for components
See the example below:
EXAMPLE
1const uid = "api::my-api.my-api"
2const name = "city"
3const locale = 'en' // Optional, if you use i18b
4
5// Using Fetch
6await fetch(`${API_URL}/api/dynamic-enumeration?uid=${uid}&name=${name}&locale=${locale}`);
7// GET /api/dynamic-enumeration?uid=your-api&name=your-attribute-name&locale=locale
8
9// Using Service in Strapi
10const SERVICE_UID = 'plugin::dynamic-enumeration.dynamic-enumeration-service'
11const service = strapi.service(SERVICE_UID)
12const data = await service.getValues({
13 uid,
14 name,
15 locale // Optional
16})Get Globals Values
You can get the options added to all global field, sending the key of the Globals Field configured in your plugins file. ( See the settings part above )
See the example below:
EXAMPLE
1// In Your "./config/plugins.js" file
2module.exports = ({ env }) => ({
3 // ...
4 'dynamic-enumeration': {
5 enabled: true,
6 config: {
7 globals: {
8 // The Global Key
9 'my-field': {
10 // Configurations
11 },
12 },
13 },
14 },
15 // ...
16});
17
18// Using Fetch
19await fetch(`${API_URL}/api/dynamic-enumeration/globals?key=my-field`);
20// GET /api/dynamic-enumeration/globals?key=global-key&locale=locale
21
22// Using Service in Strapi
23const SERVICE_UID = 'plugin::dynamic-enumeration.dynamic-enumeration-service'
24const service = strapi.service(SERVICE_UID)
25const data = await service.getGlobalValues({
26 key: 'my-field',
27 locale // Optional
28}):memo: License
This project is under the MIT license. See the LICENSE file for more details.
Install now
npm install strapi-plugin-dynamic-enumeration-field
Create your own plugin
Check out the available plugin resources that will help you to develop your plugin or provider and get it listed on the marketplace.