Hi all.
I have a simple form with a textfield with autocomplete that update with his value a field container to display a checkboxes field. The container will be updated but do not display the checkboxes or radios. Select type field works fine.
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['company_name'] = [
'#type' => 'textfield',
'#title' => t('1: Find a Company by Name'),
'#autocomplete_route_name' => 'ect_company.find_company_by_name_controller_findByName',
'#autocomplete_route_parameters' => [
'type' => 'broker',
],
'#ajax' => [
'callback' => array($this, 'findUsers'),
'event' => 'autocompleteselect',
'wrapper' => 'users-wrapper',
'progress' => array(
'type' => 'throbber',
'message' => t('Searching Users...'),
),
],
];
$form['users_wrapper'] = [
'#type' => 'container',
'#attributes' => ['id' => 'users-wrapper'],
];
$form['#attached']['library'][] = 'ect_event/drupal.ect_event_add_user.form';
$form_state->setCached(FALSE);
return $form;
}
/**
* Ajax callback to find company users.
*/
public function findUsers(array &$form, FormStateInterface $form_state) {
$companyName = $form_state->getValue('company_name');
if (preg_match('/\((\d+)\)/', $companyName, $matches)) {
$companyId = $matches[1];
$schedulerData = [
[
"value" => "11",
"label" => "Tivo Preserva",
],
[
"value" => "90",
"label" => "Ente Fet",
]
];
foreach ($schedulerData as $item) {
$options[$item['value']] = $item['label'];
}
$form['users_wrapper']['users'] = [
'#type' => 'checkboxes',
'#title' => t('2: Select users to add from the Company Members'),
'#options' => $options,
];
}
else {
// Create an Ajaxcommand response to display the message
$message = $this->t('Error on retrieve company id');
}
return $form['users_wrapper'];
}