You are here

public function BatEventUiBulkUpdateForm::submitForm in Booking and Availability Management Tools for Drupal 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

modules/bat_event_ui/src/Form/BatEventUiBulkUpdateForm.php, line 96
Contains \Drupal\bat_event_ui\Form\BatEventUiBulkUpdateForm.

Class

BatEventUiBulkUpdateForm

Namespace

Drupal\bat_event_ui\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $values = $form_state
    ->getValues();
  $start_date = new \DateTime($values['bat_start_date']);
  $end_date = new \DateTime($values['bat_end_date']);
  $end_date
    ->sub(new \DateInterval('PT1M'));
  $event_type = bat_event_type_load($values['event_type']);
  $event_state = $values['state'];
  $type = bat_type_load($values['type']);
  $units = bat_unit_load_multiple(NULL, [
    'unit_type_id' => $type
      ->id(),
  ]);
  foreach ($units as $unit) {
    $event = bat_event_create([
      'type' => $event_type
        ->id(),
      'uid' => $type->uid->entity->uid->value,
    ]);
    $event_dates = [
      'value' => $start_date
        ->format('Y-m-d'),
      'end_value' => $end_date
        ->format('Y-m-d'),
    ];
    $event
      ->set('event_dates', $event_dates);
    $target_field_name = 'event_' . $event_type
      ->getTargetEntityType() . '_reference';
    $event
      ->set($target_field_name, $unit
      ->id());
    $event
      ->set('event_state_reference', $event_state);
    $event
      ->save();
  }
}