You are here

public function MeetingForm::validateForm in Opigno Moxtra 8

Same name and namespace in other branches
  1. 3.x src/Form/MeetingForm.php \Drupal\opigno_moxtra\Form\MeetingForm::validateForm()

Button-level validation handlers are highly discouraged for entity forms, as they will prevent entity validation from running. If the entity is going to be saved during the form submission, this method should be manually invoked from the button-level validation handler, otherwise an exception will be thrown.

Overrides ContentEntityForm::validateForm

File

src/Form/MeetingForm.php, line 193

Class

MeetingForm
Provides a form for creating/editing a opigno_moxtra_meeting entity.

Namespace

Drupal\opigno_moxtra\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  parent::validateForm($form, $form_state);

  /** @var \Drupal\opigno_moxtra\MeetingInterface $entity */
  $entity = $this->entity;
  $date = $form_state
    ->getValue('date');
  if (isset($date[0]['value_wrapper'])) {
    $start_date = OpignoDateRangeWidget::createDateTimeFromWrapper($date[0]['value_wrapper']);
    $now = DrupalDateTime::createFromTimestamp($this->time
      ->getRequestTime());
    if ($start_date <= $now) {
      $form_state
        ->setError($form['date'], 'The start date should not be in the past');
    }
  }
  if (isset($date[0]['end_value_wrapper'])) {
    $end_date = OpignoDateRangeWidget::createDateTimeFromWrapper($date[0]['end_value_wrapper']);
    if (isset($start_date) && $end_date < $start_date) {
      $form_state
        ->setError($form['date'], 'The end date cannot be before the start date');
    }
  }
  if (empty($form_state
    ->getErrors())) {
    $session_key = $entity
      ->getSessionKey();
    if (empty($session_key)) {

      // Create meeting in the Moxtra.
      $user = $this
        ->currentUser();
      $user_id = $user
        ->id();
      $title = $form_state
        ->getValue('title');

      // Get ISO-8601 date without a timezone when meeting starts.
      $start_date_string = !empty($start_date) ? $start_date
        ->setTimezone(new \DateTimeZone('UTC'))
        ->format('Y-m-d\\TH:i:s\\Z') : NULL;

      // Get ISO-8601 date without a timezone when meeting ends.
      $end_date_string = !empty($end_date) ? $end_date
        ->setTimezone(new \DateTimeZone('UTC'))
        ->format('Y-m-d\\TH:i:s\\Z') : NULL;
      $response = $this->moxtraService
        ->createMeeting($user_id, $title, $start_date_string, $end_date_string);
      if ((int) $response['http_code'] === 200) {
        $entity
          ->setBinderId($response['data']['schedule_binder_id']);
        $entity
          ->setSessionKey($response['data']['session_key']);
      }
      else {
        $form_state
          ->setError($form, $this
          ->t("Can't create live meeting. Moxtra error: @message", [
          '@message' => !empty($response['message']) ? $response['message'] : $response['error'],
        ]));
      }
    }
  }
}