You are here

public function NotificationsWidgetSettingsForm::submitForm in Notifications widget 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/NotificationsWidgetSettingsForm.php, line 321

Class

NotificationsWidgetSettingsForm
Provides settings for Notification widget module.

Namespace

Drupal\notifications_widget\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  parent::submitForm($form, $form_state);
  $values = $form_state
    ->getValues();
  $commentExists = $this->moduleHandler
    ->moduleExists('comment');

  // Store users settings.
  $this
    ->storeEntityNotificationsSettings([
    'user' => 'user',
  ], $values);

  // Store content type settings.
  $nodeTypeStorage = $this->entityTypeManager
    ->getStorage('node_type');
  $nodeTypes = $nodeTypeStorage
    ->loadMultiple();
  $this
    ->storeEntityNotificationsSettings($nodeTypes, $values);

  // Store comment type settings.
  if ($commentExists) {
    $commentTypeStorage = $this->entityTypeManager
      ->getStorage('comment_type');
    $commentTypes = $commentTypeStorage
      ->loadMultiple();
    $this
      ->storeEntityNotificationsSettings($commentTypes, $values);
    $addtionalEntityTypes = explode(',', $this
      ->config('notifications_widget.settings')
      ->get('additional_entity_type'));
  }

  // Store taxonomy type settings.
  $termStorage = $this->entityTypeManager
    ->getStorage('taxonomy_vocabulary');
  $termTypes = $termStorage
    ->loadMultiple();
  $this
    ->storeEntityNotificationsSettings($termTypes, $values);

  // Store additional entity type.
  foreach ($addtionalEntityTypes as $entityType) {
    if ($this->entityTypeManager
      ->hasDefinition($entityType)) {
      $entityTypeStorage = $this->entityTypeManager
        ->getStorage($entityType);
      $additionalEntityTypes = $entityTypeStorage
        ->loadMultiple();
      if (!empty($additionalEntityTypes)) {
        $this
          ->storeEntityNotificationsSettings($additionalEntityTypes, $values);
      }
    }
  }
  $this
    ->config('notifications_widget.settings')
    ->set('notfication_widget_conf', 1)
    ->save();
}