You are here

public function UsersForm::submitForm in Notify 1.0.x

Same name and namespace in other branches
  1. 8 src/Form/UsersForm.php \Drupal\notify\Form\UsersForm::submitForm()
  2. 2.0.x src/Form/UsersForm.php \Drupal\notify\Form\UsersForm::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/UsersForm.php, line 134

Class

UsersForm
Defines a form that configures forms module settings.

Namespace

Drupal\notify\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $config = $this
    ->config('notify.settings');
  $values = $form_state
    ->getValues();
  if (isset($values['bulk']) && 1 == $values['bulk']) {
    $node = $config
      ->get('notify_def_node');
    $comment = $config
      ->get('notify_def_comment');
    $teasers = $config
      ->get('notify_def_teasers');
    $r = \Drupal::database()
      ->select('notify', 'n');
    $r
      ->fields('n', [
      'uid',
    ]);
    $r
      ->execute();
    $q = \Drupal::database()
      ->select('users', 'u');
    $q
      ->join('users_field_data', 'v', 'u.uid = v.uid');
    $q
      ->fields('v', [
      'uid',
      'name',
    ]);
    $q
      ->condition('u.uid', 0, '>');
    $q
      ->condition('v.status', 1, '=');
    $q
      ->condition('u.uid', $r, 'NOT IN');
    $result = $q
      ->execute();
    foreach ($result as $record) {
      \Drupal::database()
        ->insert('notify')
        ->fields([
        'uid' => $record->uid,
        'status' => 1,
        'node' => $node,
        'comment' => $comment,
        'teasers' => $teasers,
        'attempts' => 0,
      ])
        ->execute();
    }
  }
  elseif (!array_key_exists('users', $values)) {
    $this->messenger
      ->addMessage($this
      ->t('No users have notifications enabled.'), 'warning');
    return;
  }
  if (isset($values['users']) && $values['users']) {
    foreach ($values['users'] as $uid => $settings) {
      \Drupal::database()
        ->update('notify')
        ->fields([
        'node' => $settings['node'],
        'teasers' => $settings['teasers'],
        'comment' => $settings['comment'],
      ])
        ->condition('uid', $uid)
        ->execute();
    }
  }
  $this->messenger
    ->addMessage($this
    ->t('Users notify settings saved.'));
}