In my case, I need to get a list of config files to show in a form element. Extending from this Drupal Answers Q/A using form_state->set() inside ajax callback but cannot get the value back outside the callback, I see a suggested way of calling a static function in the submit.
However, I am missing some understanding of how to properly execute.
$list = '';
$form['fieldset'] = [
'#type' => 'fieldset',
'#prefix' => '<div id="wrapper">',
'#suffix' => '</div>',
];
$form['fieldset']['list'] = [
'#type' => 'item',
'#title' => t('List'),
'#description' => $list,
];
$form['fieldset']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Submit'),
'#submit' => [static::class, 'customFunction'],
'#ajax' => [
'callback' => '::customCallback',
'wrapper' => 'wrapper',
],
];
public function customCallback(array &$form, FormStateInterface $form_state) {
$list = $form_state->get('list');
$form_state->setRebuild(TRUE);
}
public static function customFunction(array &$form, FormStateInterface $form_state) {
$form_state->set('list', $li);
}
How does one get $form_state value into a form element from an ajax button submit function?
Update:
Here is what I have working for this case thanks to @No Sssweat.
public function buildForm(array $form, FormStateInterface $form_state) {
$form['fieldset']['list'] = [
'#type' => 'item',
'#title' => t('List'),
'#description' => '',
];
$form['fieldset']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Submit'),
'#submit' => [static::class, 'customFunction'],
'#ajax' => [
'callback' => '::customCallback',
'wrapper' => 'wrapper',
],
];
}
public function customCallback(array &$form, FormStateInterface $form_state) {
$values = $form_state->getValues();
$li = [];
$config_path = drupal_get_path('module', $values['fieldset']['module_name']) . $values['fieldset']['directory_path'];
if (is_dir($config_path)) {
if ($dh = opendir($config_path)) {
while (($fileName = readdir($dh)) !== false) {
if (pathinfo($fileName, PATHINFO_EXTENSION) == 'yml') {
$fileName = str_replace('.yml', '', $fileName);
$li[] = $fileName;
}
}
closedir($dh);
}
}
$markup = new TranslatableMarkup(
'<ul>@li</ul>', [
'@li' => t('<li>' . implode('</li><li>', $li) . '</li>'),
]
);
$form['fieldset']['list']['#description'] = $markup;
return $form['fieldset'];
}
public static function customFunction(array &$form, FormStateInterface $form_state) {
// Empty.
}
But this avoids the question as asked.