1

I'm trying to add a simple checkbox in a Drupal 8 Entity Form that displays like the old Form API:

$form['approved'] = [
  '#type' => 'checkbox',
  '#title' => t('Approved'),
  '#description' => t('Check this to indicate this person is approved for membership'),
];

Using D8 style field definitions, this should be something like

$fields['approved'] = BaseFieldDefinition::create('boolean')
  ->setLabel(t('Approved'))
  ->setDescription(t('Volunteer is approved for trusted work.'))
  ->setSettings(array(
    'display_label' => TRUE,
  ))
  ->setDisplayOptions('view', array(
    'type' => 'unicode-yes-no',
    'weight' => -3,
  ))
  ->setDisplayOptions('form', array(
    'type' => 'boolean_checkbox',
    'weight' => -3,
  ))
  ->setDisplayConfigurable('form', TRUE)
  ->setDisplayConfigurable('view', TRUE);

This doesn't quite work; I'm getting a checkbox with the label "On".

How do I fix this? Also has anybody gathered the various parameters used for setDisplayOptions() in one place and documented how these map to the Form API and the Render API? I've been looking at the sources in core/lib/Drupal/Core, and I'm not finding much. And there isn't even a test for the boolean_checkbox widget, so no code to look at there either.

1

You can always just create a field in the UI and extract the settings from there. Or look at the defaultSettings() method in the plugin class, where you can find this:

/**
 * {@inheritdoc}
 */
public static function defaultSettings() {
  return array(
    'display_label' => FALSE,
  ) + parent::defaultSettings();
}

So, add 'settings' => ['display_label' => TRUE] to your form display definition and you have what you want.

| improve this answer | |
  • @Bendir -- I can certainly use the UI and extract the configuration entity file, but since I'm trying to update the Examples module, I want to use the same technique already used by the content_entity_example, which does not use that technique. Hence BaseFieldDefinition. – Torenware Networks Oct 3 '15 at 21:24
  • I don't mean to extract the configuration file, i mean to extract the configuration. Configurable and base fields use the exact same API, it's just a different way to specify it. So you can create a field in the UI, configure the widget/formatter, then then copy the configuration for it into your base field definition. Everything you can define in the UI can also be put into a base field definition. – Berdir Oct 3 '15 at 21:39
  • @Bendir -- yeah, I got that. But there isn't much documentation as to how to do that translation. Also: it turns out that the relevant configuration is spread over four files. You have to know which files to look at. – Torenware Networks Oct 3 '15 at 23:59
1

@Bendir has the right idea, but for my field, the configuration was spread over four different config files. These were, for adding a field "approved_example" to the "Article" node type:

  • field.field.node.article.field_approval_example.yml
  • field.storage.node.field_approval_example.yml
  • core.entity_form_display.node.article.default.yml
  • core.entity_view_display.node.article.default.yml

    $fields['approved'] = BaseFieldDefinition::create('boolean')
     ->setLabel(t('Approved'))
     ->setDescription(t('Volunteer is approved for trusted work.'))
     ->setDisplayOptions('view', array(
       'settings' => [
         'format' => 'unicode-yes-no',
       ],
       'weight' => -3,
     ))
     ->setDisplayOptions('form', array(
       'settings' => [
         'display_label' => TRUE,
       ],
       'type' => 'boolean_checkbox',
       'weight' => -3,
     ))
     ->setDisplayConfigurable('form', TRUE)
     ->setDisplayConfigurable('view', TRUE);

The documentation problem is that settings can appear at several levels, and that the distinction between "options" and "settings" is not clear in the documentation.

I also think that making On/Off buttons the default UI for fields was the wrong thing for Core to do -- even getting a simple checkbox done in the UI is not very discoverable -- but the code in this answer seems to do the trick.

| improve this answer | |
  • Thanks for discovering this -- have you considered filing documentation patches and/or editing the handbook pages to make this better? – user49 Oct 5 '15 at 6:33

Your Answer

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

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