Add Entity Reference to Drupal 8 Form Field?

How can users add products to a simple plugin config using an autocomplete field?

I tried to use Config Entity but it looks the same as Form API (and I can't use entity fields there).


Solution 1:

I was able to do this in Drupal 8 using the form API and the entity_autocomplete type.

$form['stories'] = [
    '#type' => 'entity_autocomplete',
    '#target_type' => 'node',
    '#title' => $this->t('Stories'),
    '#description' => $this->t('Select some stories.'),
    '#default_value' => $default_entities,
    '#tags' => TRUE,
    '#selection_settings' => array(
        'target_bundles' => array('page', 'article'),
    ),
    '#weight' => '0',
];