You are here

public function UnitSetStateAction::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_unit/src/Form/UnitSetStateAction.php, line 129
Contains \Drupal\bat_unit\Form\UnitSetStateAction.

Class

UnitSetStateAction

Namespace

Drupal\bat_unit\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  foreach (array_keys($this->unitInfo) as $unit_id) {
    $unit = bat_unit_load($unit_id);
    $values = $form_state
      ->getValues();
    $type = $unit->unit_type_id->entity;
    $type_bundle = bat_type_bundle_load($type
      ->bundle());
    $event_state = bat_event_load_state_by_machine_name($values['event_state'])
      ->id();
    $event_type = $values['event_type'];
    $start_date = new \DateTime($values['bat_start_date']);
    $end_date = new \DateTime($values['bat_end_date']);
    $end_date
      ->sub(new \DateInterval('PT1M'));
    if (isset($type_bundle->default_event_value_field_ids[$event_type]) && !empty($type_bundle->default_event_value_field_ids[$event_type])) {
      $event = bat_event_create([
        'type' => $event_type,
      ]);
      $event->start = $start_date
        ->getTimestamp();
      $event->end = $end_date
        ->getTimestamp();
      $event->uid = $type->uid->entity->uid->value;
      $event_type_entity = bat_event_type_load($event_type);

      // Construct target entity reference field name using this event type's target entity type.
      $target_field_name = 'event_' . $event_type_entity
        ->getTargetEntityType() . '_reference';
      $event
        ->set($target_field_name, $unit_id);
      $event
        ->set('event_state_reference', $event_state);
      $event
        ->save();
    }
  }
}