Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add entity reference as a form field drupal 8? I it possible?

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).

like image 682
Przemek Leczycki Avatar asked Dec 24 '22 02:12

Przemek Leczycki


1 Answers

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',
];
like image 200
nrackleff Avatar answered Dec 31 '22 13:12

nrackleff