You are here

public function SettingsForm::submitForm in Blog API 8

Form submit.

Parameters

array $form:

\Drupal\Core\Form\FormStateInterface $form_state:

Overrides ConfigFormBase::submitForm

File

src/Form/SettingsForm.php, line 226

Class

SettingsForm
Class SettingsForm.

Namespace

Drupal\blogapi\Form

Code

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

  // Save the enabled content types and default text format.
  $config = $this
    ->config('blogapi.settings');
  $config
    ->set('content_types', $form_state
    ->getValue('content_types'))
    ->save();
  $config
    ->set('text_format', $form_state
    ->getValue('text_format'))
    ->save();

  // Save the body and taxonomy fields for every selected content type.
  $enabled_cts = $config
    ->get('content_types');
  foreach ($enabled_cts as $ct) {
    if (!empty($ct)) {

      // Save the body field.
      $field_body = 'body_' . $ct;
      $body = $form_state
        ->getValue($field_body);
      if (!is_null($body)) {
        $config
          ->set('body_' . $ct, $body)
          ->save();
      }

      // Save the taxonomy field.
      $field_vocab = 'taxonomy_' . $ct;
      $vocab = $form_state
        ->getValue($field_vocab);
      if (!is_null($vocab)) {
        $config
          ->set('taxonomy_' . $ct, $vocab)
          ->save();
      }

      // Save the comment field.
      $field_comment = 'comment_' . $ct;
      $comment = $form_state
        ->getValue($field_comment);
      if (!is_null($comment)) {
        $config
          ->set('comment_' . $ct, $comment)
          ->save();
      }
    }
  }
  $this
    ->messenger()
    ->addStatus($this
    ->t('BlogAPI settings have been saved.'));
}