You are here

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

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

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

Class

FullcalendarEventManagerForm

Namespace

Drupal\bat_fullcalendar\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $entity_id = 0, $event_type = 0, $event_id = 0, $start_date = 0, $end_date = 0) {
  if (!isset($form_state
    ->getUserInput()['form_id'])) {
    $form_state
      ->getUserInput()['form_id'] = '';
  }
  $new_event_id = $event_id;
  if ($form_state
    ->getValue('change_event_status')) {
    $new_event_id = $form_state
      ->getValue('change_event_status');
  }
  $form['#attributes']['class'][] = 'bat-management-form bat-event-form';

  // This entire form element will be replaced whenever 'changethis' is updated.
  $form['#prefix'] = '<div id="replace_textfield_div">';
  $form['#suffix'] = '</div>';
  $form['entity_id'] = [
    '#type' => 'hidden',
    '#value' => $entity_id,
  ];
  $form['event_type'] = [
    '#type' => 'hidden',
    '#value' => $event_type
      ->id(),
  ];
  $form['event_id'] = [
    '#type' => 'hidden',
    '#value' => $event_id,
  ];
  $form['bat_start_date'] = [
    '#type' => 'hidden',
    '#value' => $start_date
      ->format('Y-m-d H:i:s'),
  ];
  $form['bat_end_date'] = [
    '#type' => 'hidden',
    '#value' => $end_date
      ->format('Y-m-d H:i:s'),
  ];
  $unit = $this->entityTypeManager
    ->getStorage($event_type
    ->getTargetEntityType())
    ->load($entity_id);
  $form['event_title'] = [
    '#prefix' => '<h2>',
    '#markup' => t('@unit_name', [
      '@unit_name' => $unit
        ->label(),
    ]),
    '#suffix' => '</h2>',
  ];
  $date_format = $this
    ->configFactory()
    ->get('bat.settings')
    ->get('date_format') ?: 'Y-m-d H:i';
  $form['event_details'] = [
    '#prefix' => '<div class="event-details">',
    '#markup' => t('Date range selected: @startdate to @enddate', [
      '@startdate' => $start_date
        ->format($date_format),
      '@enddate' => $end_date
        ->format($date_format),
    ]),
    '#suffix' => '</div>',
  ];
  if ($event_type
    ->getFixedEventStates()) {
    $state_options = bat_unit_state_options($event_type
      ->id());
    $form['change_event_status'] = [
      '#title' => t('Change the state for this event to') . ': ',
      '#type' => 'select',
      '#options' => $state_options,
      '#ajax' => [
        'callback' => '::ajaxEventStatusChange',
        'wrapper' => 'replace_textfield_div',
      ],
      '#empty_option' => t('- Select -'),
    ];
  }
  else {
    if (isset($event_type->default_event_value_field_ids) && !empty($event_type->default_event_value_field_ids)) {
      $field_name = $event_type->default_event_value_field_ids;
      $form['field_name'] = [
        '#type' => 'hidden',
        '#value' => $field_name,
      ];
      $field_definition = $this->entityFieldManager
        ->getFieldDefinitions('bat_event', $event_type
        ->id())[$field_name];
      $items = new FieldItemList($field_definition, NULL, EntityAdapter::createFromEntity(bat_event_create([
        'type' => $event_type
          ->id(),
      ])));
      $form_display = EntityFormDisplay::load('bat_event.' . $event_type
        ->id() . '.default');
      $widget = $form_display
        ->getRenderer($field_name);
      $form['#parents'] = [];
      $form[$field_name] = $widget
        ->form($items, $form, $form_state);
      $form[$field_name]['#weight'] = 1;
      $form['submit'] = [
        '#type' => 'submit',
        '#value' => t('Update value'),
        '#weight' => 2,
        '#ajax' => [
          'callback' => '::eventManagerAjaxSubmit',
          'wrapper' => 'replace_textfield_div',
        ],
      ];
    }
  }
  return $form;
}