You are here

public function UserSettings::submitForm in Notify 8

Same name and namespace in other branches
  1. 2.0.x src/Form/UserSettings.php \Drupal\notify\Form\UserSettings::submitForm()
  2. 1.0.x src/Form/UserSettings.php \Drupal\notify\Form\UserSettings::submitForm()

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/UserSettings.php, line 231

Class

UserSettings
Defines a form that configures forms module settings.

Namespace

Drupal\notify\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $values = $form_state
    ->getValues();
  $uid = $values['uid'];
  \Drupal::database()
    ->delete('notify')
    ->condition('uid', $uid)
    ->execute();
  $id = \Drupal::database()
    ->insert('notify')
    ->fields([
    'uid' => $uid,
    'status' => $values['status'],
    'node' => $values['node'],
    'teasers' => $values['teasers'],
    'comment' => $values['comment'],
  ])
    ->execute();
  $subscriptions = [];

  // Remember that this is a custom subscriber.
  $subscriber = _notify_user_has_subscriptions($uid);
  if (!$subscriber) {
    \Drupal::database()
      ->insert('notify_subscriptions')
      ->fields([
      'uid' => $uid,
      'type' => 'magic custom subscription',
    ])
      ->execute();
  }
  foreach ($values as $key => $value) {
    if (preg_match("/^" . NOTIFY_NODE_TYPE . "/", $key)) {
      $key = substr($key, 17);
      $id = \Drupal::database()
        ->select('notify_subscriptions', 'n')
        ->fields('n', [
        'id',
        'uid',
        'type',
      ])
        ->condition('uid', $uid)
        ->condition('type', $key)
        ->execute()
        ->fetchObject();
      if ($id) {
        $id = $id->id;
        if (!$value) {
          \Drupal::database()
            ->delete('notify_subscriptions')
            ->condition('id', $id)
            ->execute();
        }
      }
      else {
        if ($value) {
          \Drupal::database()
            ->insert('notify_subscriptions')
            ->fields([
            'uid' => $uid,
            'type' => $key,
          ])
            ->execute();
        }
      }
    }
  }
  $this->messenger
    ->addMessage($this
    ->t('Notify settings saved.'));
}