You are here

public function ExpertsForm::submitForm in FAQ_Ask 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/ExpertsForm.php, line 447
Contains \Drupal\faq_ask\Form\ExpertsForm.

Class

ExpertsForm
Form for the FAQ settings page - categories tab.

Namespace

Drupal\faq_ask\Form

Code

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

  // Remove unnecessary values.
  $form_state
    ->cleanValues();
  $this
    ->configFactory()
    ->getEditable('faq_ask.settings')
    ->set('expert_role', $form_state
    ->getValue('faq_expert_role'))
    ->set('vocabularies', $form_state
    ->getValue('vocabularies'))
    ->set('categorize', $form_state
    ->getValue('categorize'))
    ->set('expert_own', $form_state
    ->getValue('faq_expert_own'))
    ->set('notify', $form_state
    ->getValue('notify'))
    ->set('notify_asker', $form_state
    ->getValue('notify_asker'))
    ->set('notify_asker_simplenews_tid', $form_state
    ->getValue('notify_asker_simplenews_tid'))
    ->set('notify_asker_simplenews_confirm', $form_state
    ->getValue('notify_asker_simplenews_confirm'))
    ->set('notify_by_cron', $form_state
    ->getValue('notify_by_cron'))
    ->set('unanswered', $form_state
    ->getValue('unanswered'))
    ->set('default_expert', $form_state
    ->getValue('default_expert'))
    ->set('expert_advice', $form_state
    ->getValue('faq_expert_advice'))
    ->set('help_text', $form_state
    ->getValue('help_text'))
    ->set('admin_advice', $form_state
    ->getValue('admin_advice'))
    ->set('admin_advice', $form_state
    ->getValue('asker_advice'))
    ->save();

  // Get all the selected expert/category options.
  // First, we'll include the default expert for tid=0.
  $values = array();
  $values[] = array(
    'uid' => $form_state
      ->getValue('default_expert'),
    'tid' => 0,
  );
  foreach ($form_state
    ->getValue() as $name => $value) {
    if (substr($name, 0, 7) == 'expert_') {
      if ($value) {
        list($junk, $uid, $tid) = explode('_', $name);
        $values[] = array(
          'uid' => $uid,
          'tid' => $tid,
        );
      }
    }
  }

  // Delete the current values and save the new ones.
  if (!empty($values)) {

    // Delete old values.
    $query = \Drupal::database()
      ->delete('faq_expert')
      ->execute();

    // Inser new values.
    $query = \Drupal::database()
      ->insert('faq_expert');
    foreach ($values as $pair) {
      $query
        ->fields([
        'uid',
        'tid',
      ]);
      $query
        ->values($pair);
    }
    $query
      ->execute();
  }
  drupal_set_message(t('Configuration has been updated.'), 'status');
  parent::submitForm($form, $form_state);
}