I have a custom content entity and i'd like to use a map BaseFieldDefinition type to hold an arbitrary number of options.

This is in my custom content entity baseFieldsDefinitions():

$fields['options'] = BaseFieldDefinition::create('integer')
      ->setLabel(t('Task options'))
      ->setDescription(t('The task options.'))
           'type' => 'map',
            'default_value' => null

How do I display this as part of the entity form?

My instinct is that I'd need to create a custom widget, custom formatter and add the following to the base field definition:

->setDisplayOptions('form', array(
         'type' => 'custom_widget',
          'weight' => -6,

Any direction would be much appreciated.

  • Your instinct is right, what's stopping you from doing exactly that? Map doesn't have default widget/formatter as it's basicaly just a generic key/value storage. That said, your definition is fairly wrong, the argument to create() is the type, there is no type setting and default_value has not been a setting for years, there are separate methods to set the default. – Berdir Mar 13 '17 at 21:51
  • thanks, so I would use BaseFieldDefinition::create('map'), remove the ->setSettings(...), write my custom widget and then declare it in the setDisplayOptions('form', ...) as above? – RobotEyes Mar 14 '17 at 10:38
  • Yes, exactly... – Berdir Mar 14 '17 at 10:48
  • @RobotEyes Hei, Hope you could assist me. I also have a field with type map, but I can't display it when creating a view, so I think I might have to create a custom widget as you mentioned, how would I go about creating a custom widget for this one specific field? – Gerardlamo Apr 11 '17 at 11:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.