Custom labels for enum values. possible?

Just wanted to chime in that I came looking for this as well.

2 Likes

Thirded! :slight_smile:

Probably a tad early to share this but we are working on this for the v4. Keep in mind this image is a very early prototype so things might change a bit.

3 Likes

Looks great! Thank you.

That’s awesome DMehaffy!

Thanks for keeping us in the loop. Looking forward to v4!

Wow, this is much better!

Will you allow the user to select multiple values, and will this work with GraphQL?

I believe we are adding an array type that could function like this but I’d need to check with @Jab and @maeva to see if we were going to add a multi-select with predefined keys or not.

2 Likes

The multi-select won’t be available for the moment :slight_smile: (cc @DMehaffy )

1 Like

Has this been included in v4? I thought I noticed changes to the ‘enumeration’ fieldtype at one point when I was testing. Now, in v4.0.0-beta.13, nothing.

Not on first release, we had to cut back on a few things but I believe this will come in the near future

I have to say, this is very disappointing, to not have a label/value fieldtype for creating dropdown options. I recently tried the new v4 release and set up content-types for all my options fields in order to pull in via ‘Relation’ field, as was recommended by strapi personnel. Here’s what the database looks like just after setting up the content-types, without connecting by Relation. Does it look strange to you, too?

Please try to resolve the deficiencies of the enumeration field, or create the above Status field type, or simply a Category field type as soon as possible.

I would love this feature too :slight_smile:

@DMehaffy Have you forgotten about this feature ? :wink: - Or is it still on the roadmap ?

It somewhat got put on the backburner for now.

Maybe a good solution could be define on the schema something like this:

 "select": {
      "type": "enumeration",
      "enum": [
         {
          value: "research",
          label: "Research"
         },
         {
          value: "article",
          label: "Article"
         }]
}
1 Like

It has been a while since this was discussed but would really be a helpful option if there is any update on it.

Any update on this? it’s such a useful feature . hope this gets implemented soon

It’s already October 2023, and I see there was no solution for this yet, is there an update or something ? what is blocking this ?

Hi, everyone. There is no solution from Strapi at the moment. There are 2 ways to solve this problem. I don’t like both of them, but there’s no other

Custom field
It is possible to create a custom field. But there is a nuance here. We will not be able to create a custom field with the enumerable type. The reason is that when adding such a field to a content type, Strapi first checks the field being added, and for enumerable it checks for enum and defaultValue properties, which cannot be set via customFieldRegister.

So this solution can only be based on using string or json type. Which is not optimal because we are deprived of enum type and checks at the API level. How exactly to make such a field, you can look at other examples of how to prepare it. The logic itself will be based on the use of options. A good example is the multy-select custom field plugin.

Patch the generateOptions.js method
Not the most ideal, but a working solution. We’ll create a Map where we’ll write the <option, label> structure. Strapi has a generalized Inputs component that creates its own structure to display options, it contains a key, option and most importantly label, which we will substitute if it is found.

Solution for Strapi 14.12.0
Path: node_modules/@strapi/admin/admin/src/content-manager/components/Inputs/utils/generateOptions.js

// We define our map
const labels = new Map([["Color", "Колір"]]);

const generateOptions = (options, isRequired = false) => {
  return [
    {
      metadatas: {
        intlLabel: {
          id: "components.InputSelect.option.placeholder",
          defaultMessage: "Choose here",
        },
        disabled: isRequired,
        hidden: isRequired,
      },
      key: "__enum_option_null",
      value: "",
    },
    ...options.map((option) => {
     // On eeach option get label
      const label = labels.get(option);

      return {
        metadatas: {
         // Set founded label if it exists
          intlLabel: {
            id: label || option,
            defaultMessage: label || option,
          },
          hidden: false,
          disabled: false,
        },
        key: option,
        value: option,
      };
    }),
  ];
};

export default generateOptions;

That’s all after that you can use patch-package to commit the changes.

Solution for Strapi above 4.15.0 (Since now Strapi comes already compiled in mjs and js files)
Path: /node_modules/@strapi/admin/dist/_chunks

Need to find files index-3f8cf4d5.js and index-95e8649d.mjs.
Also, we can find the right location by searching for the string __enum_option_null.

Before the Inputs component, add your Map for example

const optionsLabelsMapFix = new Map([["Color", "Колір"]]);

And then next to __enum_option_null

    ...(fieldSchema.enum ?? []).map((option) => {
      const label = optionsLabelsMapFix.get(option)

      return {
        metadatas: {
          intlLabel: {
            id: label || option,
            defaultMessage: label || option
          },
          hidden: false,
          disabled: false
        },
        key: option,
        value: option
      };
    })

This needs to be done for both js and mjs files. After that, you need to keep an eye on Strapi changes for a possible update.

Did anyone manage to get this to work and could you help me understand this better please? This explanation is not very clear to me.