Q:

How to save dynamic select list value into config table in Drupal 8?

I wanted to show list of fields per content type in admin config form. After saving the form, selected list value should get save into config table. 
public function buildForm(array $form, FormStateInterface $form_state) {
  foreach (node_type_get_types() as $content_type) {
    $entity_type_id = 'node';
    $fields = \Drupal::entityManager()->getFieldDefinitions('node', $content_type->get('type'));
    if ($fields) {
      $options = array();
      foreach ($fields as $field_name => $field_definition) {
        if (!empty($field_definition->getTargetBundle())) {
          $options[$field_name] = $field_definition->getLabel();
        }
      }
      $form['test']['test_' . $content_type->get('type') . '_field'] = array(
        '#type' => 'select',
        '#title' => $content_type->get('name'),
        '#options' => array_merge(array('0' => t('Auto')), $options),
        '#default_value' => $config->get('test_' . $content_type->get('type') . '_field') ? $config->get('test_' . $content_type->get('type') . '_field') : '',
      );
    }
  }
}

Here is submit handler 
public function submitForm(array &$form, FormStateInterface $form_state) {

foreach (node_type_get_types() as $content_type) {
  $this->config('test.settings')
  ->set('test_' . $content_type->get('type') . '_field', $form_state->getValue('test', 'test_' . $content_type->get('type') . '_field'))
  ->save();
}     
parent::submitForm($form, $form_state); }

After saving admin form, test_article_field and test_page_field  is getting saved instead of selected field.
Any help would be appreciated?

A:

There is some problem in the submit handler.
Replace
$this->config('test.settings')
 ->set('test_' . $content_type->get('type') . '_field', $form_state->getValue('test', 'test_' . $content_type->get('type') . '_field'))
 ->save();

line with
$this->config('test.settings')
  ->set('test_' . $content_type->get('type') . '_field', $form_state->getValues('test_' . $content_type->get('type') . '_field'))
  ->save();

Hope that this helps you.

