You are here

public function FullcalendarEventManagerForm::eventManagerAjaxSubmit 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 234
Contains \Drupal\bat_fullcalendar\Form\FullcalendarEventManagerForm.

Class

FullcalendarEventManagerForm

Namespace

Drupal\bat_fullcalendar\Form

Code

public function eventManagerAjaxSubmit($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'];
  $field_name = $values['field_name'];
  $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($field_name, $values[$field_name]);
  $event
    ->save();
  $unit = $this->entityTypeManager
    ->getStorage($event_type_entity
    ->getTargetEntityType())
    ->load($entity_id);
  $elements = $event->{$field_name}
    ->view([
    'label' => 'hidden',
  ]);
  $value = $this->renderer
    ->render($elements);
  $form['form_wrapper_bottom'] = [
    '#prefix' => '<div>',
    '#markup' => t('Value for <b>@name</b> changed to <b>@value</b>', [
      '@name' => $unit
        ->label(),
      '@value' => trim(strip_tags($value
        ->__toString())),
    ]),
    '#suffix' => '</div>',
    '#weight' => 9,
  ];
  return $form;
}