form API

If this does not work...

broken_fapi_autocomplete.php
$form['nodes'] = [
    '#title' => $this->t('Select articles'),
    '#type' => 'entity_autocomplete',
    '#target_type' => 'node',
    '#tags' => TRUE,
    '#selection_handler' => 'default',
    '#selection_settings' => ['target_bundles' => ['article']],
    '#default_value' => $default_nodes,
];

...try adding an increased maxlength value:

fixed_fapi_autocomplete.php
$form['nodes'] = [
    '#title' => $this->t('Select articles'),
    '#type' => 'entity_autocomplete',
    '#target_type' => 'node',
    '#maxlength' => 1024,
    '#tags' => TRUE,
    '#selection_handler' => 'default',
    '#selection_settings' => ['target_bundles' => ['article']],
    '#default_value' => $default_nodes,
];

Enable nested arrays in $form_states with:

tree.php
$form['my_text']['#tree'] = TRUE;

Here in an example code:

alter_structure.php
$form['my_text'] = [
  '#type' => 'details',
  '#title' => t('Text'),
  '#open' => TRUE,
  // Without this line you cannot access nested arrays.
  '#tree' => TRUE,
];

$default_value = '';
$form['my_text']['text'] = [
  '#type' => 'textfield',
  '#title' => t('Text to appear as the page.'),
  '#description' => t('If textfield is left empty no text will be displayed page'),
  '#default_value' => $default_value,
];
17.07.2025 | Lothar Ferreira Neumann

How to enable nested array structure in $form_states