I am writing a Drupal 8 module, and I have some code that adds an entity reference field to a form.

$form['commitment']['goal'] = array(
    '#type' => 'entity_autocomplete',
    '#title' => 'Goal',
    '#target_type' => 'node',
    '#selection_settings' => ['target_bundles' => ['goal']],

This works, except that I want a select list instead of an autocomplete.

How can I accomplish this?

  • If you search the Drupal core codebase for '#type' => ' you'll see plenty of examples. (hint - it's 'select'). Also if you're creating new modules instead of configuring via the UI and using CMI to export your configurations then it's worth getting familiar with drupalconsole.com as that will take a lot of the pain out of it for you by providing skeleton code. Also remember custom modules = technical debt. If Drupal doesn't do what you need it to do already (which it does a lot) try adapting what's there, joining in another contrib module's development, or try to adapt your requirem Commented Jul 27, 2017 at 17:17
  • 2
    Yes, I want 'select' instead of 'entity_autocomplete', but how do I populate it with a list of my current Goal nodes? Replacing 'entity_autocomplete' with 'select' is not sufficient.
    – Dave R
    Commented Jul 27, 2017 at 17:21
  • Again, search for examples, there'll be plenty already in the code. Find something similar in Drupal core through the UI where it does what you want to do, and copy. Commented Jul 27, 2017 at 17:51
  • also if you're finding this difficult why are you creating a custom module? Why not create your content type and field through the UI where it's super-simple and you don't need to write any code? Is it essential you write a custom module? Why not create through the UI then use CMI to export the configuration, that's the 'usual' way. Commented Jul 27, 2017 at 17:53
  • I'm working with Dave on this issue and we appreciate the feedback. However, we're trying to create a form that includes fields from two different nodes. Normally something that we might do with inline_entity_form module, but can't in this case. I don't know how to accomplish our goal in the UI. We've successfully updated the form with the new field, but it's using the wrong widget. We are trying to figure out how to specify the correct widget in code.
    – stpaultim
    Commented Jul 27, 2017 at 21:40

2 Answers 2


Basing on the OP solution, I would suggest the follow code.

$options = [];
$goal_nodes = \Drupal::entityTypeManager()->getStorage('node')->loadByProperties([
  'type' => 'goal',

foreach ($goal_nodes as $goal_node) {
  $options[$goal_node->id()] = $goal_node->label();

$form['commitment']['goal'] = [
  '#type' => 'select',
  '#title' => t('Goal'),
  '#options' => $options,

This has still two issues:

  1. Entities are not translated
  2. Bundle label ('Goal') is not programmatically coming
  • Entity labels actually are translated. See ContentEntityBase::getEntityKey()
    – mbomb007
    Commented Mar 29, 2023 at 15:26

Found out after 2 hours of search in drupal core:

->setDisplayOptions('form', array(
            'type' => 'options_select'

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.