You are here

public function ConfigureActivityForm::submitForm in Activity 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 FormInterface::submitForm

File

src/Form/ConfigureActivityForm.php, line 312

Class

ConfigureActivityForm
Configure activities form.

Namespace

Drupal\activity\Form

Code

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

  // If the event is not new, get hook from database.
  if ($this->hook == NULL) {
    $query = $this->activityService
      ->getActivityEventField($this->eventId, 'hook');
    $this->hook = $query[0]->hook;
  }

  // Get options to insert them in database.
  $userId = \Drupal::currentUser()
    ->id();
  $this->label = $form_state
    ->getValue('activity_label');
  $windowOption = $form_state
    ->getValue('activity_window');
  $roleOptions = $form_state
    ->getValue('activity_roles') == NULL ? NULL : array_filter(array_values($form_state
    ->getValue('activity_roles')));
  $contentTypes = $form_state
    ->getValue('activity_node_types') == NULL ? NULL : array_filter(array_values($form_state
    ->getValue('activity_node_types')));
  $message = $form_state
    ->getValue('activity_message');
  $messageArray = [
    'window' => $windowOption,
    'roles' => $roleOptions,
    'types' => $contentTypes,
    'message' => $message,
  ];

  // Update row based on event id.
  if ($this->eventId != 'new') {
    $this->database
      ->update('activity_events')
      ->fields([
      'label' => $this->label,
      'hook' => $this->hook,
      'userId' => $userId,
      'message' => json_encode($messageArray),
    ])
      ->condition('event_id', $this->eventId)
      ->execute();
  }
  else {
    $this->database
      ->insert('activity_events')
      ->fields([
      'label' => $this->label,
      'hook' => $this->hook,
      'userId' => $userId,
      'created' => \Drupal::time()
        ->getCurrentTime(),
      'message' => json_encode($messageArray),
    ])
      ->execute();
  }

  // Set label and hook to be null.
  $this
    ->deleteStore();
  $url = Url::fromUri('internal:/admin/activity');
  $form_state
    ->setRedirectUrl($url);
}