Set and Get $form_state with ajax button submit function? 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-&gt;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' =&gt; 'fieldset',
  '#prefix' =&gt; '&lt;div id="wrapper"&gt;',
  '#suffix' =&gt; '&lt;/div&gt;',
];

$form['fieldset']['list'] = [
  '#type' =&gt; 'item',
  '#title' =&gt; t('List'),
  '#description' =&gt; $list,
];

$form['fieldset']['submit'] = [
  '#type' =&gt; 'submit',
  '#value' =&gt; $this-&gt;t('Submit'),
  '#submit' =&gt; [static::class, 'customFunction'],
  '#ajax' =&gt; [
    'callback' =&gt; '::customCallback',
    'wrapper' =&gt; 'wrapper',
  ],
];

public function customCallback(array &amp;$form, FormStateInterface $form_state) {
  $list = $form_state-&gt;get('list');
  $form_state-&gt;setRebuild(TRUE);
}

public static function customFunction(array &amp;$form, FormStateInterface $form_state) {
  $form_state-&gt;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' =&gt; 'item',
    '#title' =&gt; t('List'),
    '#description' =&gt; '',
  ];

  $form['fieldset']['submit'] = [
    '#type' =&gt; 'submit',
    '#value' =&gt; $this-&gt;t('Submit'),
    '#submit' =&gt; [static::class, 'customFunction'],
    '#ajax' =&gt; [
      'callback' =&gt; '::customCallback',
      'wrapper' =&gt; 'wrapper',
    ],
  ];

}

public function customCallback(array &amp;$form, FormStateInterface $form_state) {

  $values = $form_state-&gt;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(
    '&lt;ul&gt;@li&lt;/ul&gt;', [
      '@li' =&gt; t('&lt;li&gt;' . implode('&lt;/li&gt;&lt;li&gt;', $li) . '&lt;/li&gt;'),
    ]
  );

  $form['fieldset']['list']['#description'] = $markup;
  return $form['fieldset'];
}

public static function customFunction(array &amp;$form, FormStateInterface $form_state) {
  // Empty.
}


But this avoids the question as asked.
Should be:

public function customCallback(array &amp;$form, FormStateInterface $form_state) {
  $list = $form_state-&gt;get('list');
  $form['fieldset']['list']['#description'] = $list;
  return $form['fieldset'];
}