2

I created a Drupal 8 module for creating a custom field. I wrote the code basing on the documentation given in Create a custom field widget, Create a custom field formatter, and Create a custom field type.

I created the .info.yml file, three directories src/Plugin/Field (FieldFormatter, FieldType, and FieldWidget) where I put the files described in those documentation guides.
I could see the custom field in the field list, but it is shown as disabled.

Why does this happen?

3
  • 2
    Add a screenshot, it might also be useful to share your code. I'm not sure what you mean exactly.
    – Berdir
    Commented Aug 8, 2016 at 13:48
  • 1
    I have exactly the same problem. I've created a custom field type and when I create a field in the UI I can't place it anywhere under "Manage form display". The field is always in the 'disabled' section. The widget selection drop down says 'hidden' and doesn't have any other options.
    – tanc
    Commented Jan 4, 2017 at 15:54
  • There are so many tiny things that can make a custom field not work. I don't like it. Commented Mar 7, 2017 at 19:15

2 Answers 2

0

I had the same problem and the issue in my case was that I was using default_widget = "options_select" in my FieldType plugin but the options_select widget does not allow my custom field type.

The solution was to create a custom FieldWidget plugin which extends the OptionsSelectWidget and in the field_types declare my custom field type.

This allowed my field to be placed in the form correctly and the options widget to be used.

0

After the field is created and the field instance is added, you need to enable the field in the display settings for the form, for example with the following code.

entity_get_form_display('node', $bundle, 'default')
  ->setComponent('field_custom', array(
    'type' => 'string'
  ))
  ->save();

Not the answer you're looking for? Browse other questions tagged or ask your own question.