I am working with Drupal 8. And I want to create a field of entity_reference type. The field has a reference to context_question entity. And the field is displayed fine, but without any description beside the check boxes.

The context_question entity has a label field and I want exactly the label be displayed beside the checkbox in my first entity. What should I change in the code? Or do I need to create the fieldFormatter?

$fields['context_question'] = BaseFieldDefinition::create('entity_reference')
  ->setLabel(t('Context Question'))
  ->setDescription(t('Reference to context-question-type'))
  ->setSetting('target_type', 'context_question')
  ->setDisplayOptions('view', array(
    'label' => 'above',   
    'format' => 'label',
    'weight' => 15,
  ->setDisplayOptions('form', array(
    'type' => 'options_buttons',
    'weight' => 15,
  ->setDisplayConfigurable('form', TRUE)
  ->setDisplayConfigurable('view', TRUE);

1 Answer 1


I assume the context_question is your custom entity or coming from a contrib module?

There are two options:

  1. Add label key to context_question entity definition under entity_keys so that Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection pick it up. See Aggregator item for reference.

  2. Add a @EntityReferenceSelection plugin. You have to define one. Check TermSelection for how it is implemented for terms. It looks like this:

Term reference with options_button form display

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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