You are here

public function BetterFieldDescriptionsSettingsForm::submitForm in Better Field Descriptions 8

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides ConfigFormBase::submitForm

File

src/Form/BetterFieldDescriptionsSettingsForm.php, line 152

Class

BetterFieldDescriptionsSettingsForm
Displays the better_field_descriptions settings form.

Namespace

Drupal\better_field_descriptions\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {

  // Get the config settings.
  $config = $this
    ->config('better_field_descriptions.settings');

  // Get list of fields selected for better descriptions.
  $bfdescriptions = $config
    ->get('better_field_descriptions');

  // We don't want our settings to contain 0-values, only selected values.
  $bfds = [];

  // Default fields values.
  $bfd = [];
  foreach ($form_state
    ->getValue('bundles') as $entity_type => $bundles) {
    foreach ($bundles as $bundle_machine_name => $bundle) {
      foreach ($bundle as $field_machine_name => $value) {

        // $value is (int) 0 if the field was not selected in the form.
        if (is_string($value)) {
          $bfds[$entity_type][$bundle_machine_name][$field_machine_name] = $field_machine_name;

          // Get any existing description and/or label.
          $description = $bfdescriptions[$entity_type][$bundle_machine_name][$field_machine_name]['description'] ?? 'Sample Description';
          $label = $bfdescriptions[$entity_type][$bundle_machine_name][$field_machine_name]['label'] ?? 'Label';
          $position = $bfdescriptions[$entity_type][$bundle_machine_name][$field_machine_name]['position'] ?? 1;

          // Store the existing description/label or default values.
          $bfd[$entity_type][$bundle_machine_name][$field_machine_name]['description'] = $description;
          $bfd[$entity_type][$bundle_machine_name][$field_machine_name]['label'] = $label;
          $bfd[$entity_type][$bundle_machine_name][$field_machine_name]['position'] = $position;
        }
      }
    }
  }
  $config
    ->set('better_field_descriptions_settings', $bfds);
  $config
    ->set('better_field_descriptions', $bfd);
  $config
    ->save();
  parent::submitForm($form, $form_state);
}