You are here

public function FullcalendarEventManagerForm::ajaxEventStatusChange in Booking and Availability Management Tools for Drupal 8

The callback for the change_event_status widget of the event manager form.

File

modules/bat_fullcalendar/src/Form/FullcalendarEventManagerForm.php, line 192
Contains \Drupal\bat_fullcalendar\Form\FullcalendarEventManagerForm.

Class

FullcalendarEventManagerForm

Namespace

Drupal\bat_fullcalendar\Form

Code

public function ajaxEventStatusChange($form, FormStateInterface $form_state) {
  $values = $form_state
    ->getValues();
  $start_date = new \DateTime($values['bat_start_date']);
  $end_date = new \DateTime($values['bat_end_date']);
  $entity_id = $values['entity_id'];
  $event_id = $values['event_id'];
  $event_type = $values['event_type'];
  $state_id = $values['change_event_status'];
  $event = bat_event_create([
    'type' => $event_type,
  ]);
  $event->uid = $this
    ->currentUser()
    ->id();
  $event_dates = [
    'value' => $start_date
      ->format('Y-m-d\\TH:i:00'),
    'end_value' => $end_date
      ->format('Y-m-d\\TH:i:00'),
  ];
  $event
    ->set('event_dates', $event_dates);
  $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, $entity_id);
  $event
    ->set('event_state_reference', $state_id);
  $event
    ->save();
  $state_options = bat_unit_state_options($event_type);
  $form['form_wrapper_bottom'] = [
    '#prefix' => '<div>',
    '#markup' => t('New Event state is <strong>@state</strong>.', [
      '@state' => $state_options[$state_id],
    ]),
    '#suffix' => '</div>',
    '#weight' => 9,
  ];
  return $form;
}