You are here

public function RegistrationForm::form in RNG - Events and Registrations 8

Same name and namespace in other branches
  1. 8.2 src/Form/RegistrationForm.php \Drupal\rng\Form\RegistrationForm::form()
  2. 3.x src/Form/RegistrationForm.php \Drupal\rng\Form\RegistrationForm::form()

Gets the actual form array to be built.

Overrides ContentEntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

src/Form/RegistrationForm.php, line 56

Class

RegistrationForm
Form controller for registrations.

Namespace

Drupal\rng\Form

Code

public function form(array $form, FormStateInterface $form_state) {

  /** @var \Drupal\rng\RegistrationInterface $registration */
  $registration = $this
    ->getEntity();
  $current_user = $this
    ->currentUser();
  $event = $registration
    ->getEvent();
  $event_meta = $this->eventManager
    ->getMeta($event);
  $form = parent::form($form, $form_state);
  if (!$registration
    ->isNew()) {
    $form['#title'] = $this
      ->t('Edit Registration', [
      '%event_label' => $event
        ->label(),
      '%event_id' => $event
        ->id(),
      '%registration_id' => $registration
        ->id(),
    ]);
  }
  $registrants = [];
  if ($registration
    ->isNew()) {

    /** @var \Drupal\rng\RegistrantFactory $registrant_factory */
    $registrant_factory = \Drupal::service('rng.registrant.factory');
    $count = $event_meta
      ->identitiesCanRegister('user', [
      $current_user
        ->id(),
    ]);
    if (count($count) > 0) {
      $registrant = $registrant_factory
        ->createRegistrant([
        'event' => $event,
      ]);
      $current_user = User::load($current_user
        ->id());
      $registrant
        ->setIdentity($current_user);
      $registrants[] = $registrant;
    }
  }
  else {
    $registrants = $registration
      ->getRegistrants();
  }
  $form['registrants_before'] = [
    '#type' => 'value',
    '#value' => $registrants,
  ];
  $form['people'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('People'),
    '#description' => $this
      ->t('Select people to associate with this registration.'),
    '#open' => TRUE,
    '#tree' => TRUE,
  ];
  $event_type = $event_meta
    ->getEventType();
  $min = $event_meta
    ->getRegistrantsMinimum();
  $max = $event_meta
    ->getRegistrantsMaximum();
  $form['people']['registrants'] = [
    '#type' => 'registrants',
    '#event' => $event,
    '#default_value' => $registrants,
    '#allow_creation' => $event_meta
      ->getCreatableIdentityTypes(),
    '#allow_reference' => $event_meta
      ->getIdentityTypes(),
    '#registrants_minimum' => $min !== EventMetaInterface::CAPACITY_UNLIMITED ? $min : NULL,
    '#registrants_maximum' => $max !== EventMetaInterface::CAPACITY_UNLIMITED ? $max : NULL,
    '#form_modes' => $event_type
      ->getIdentityTypeEntityFormModes(),
  ];
  if (!$registration
    ->isNew()) {
    $form['revision_information'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Revisions'),
      '#optional' => TRUE,
      '#open' => $current_user
        ->hasPermission('administer rng'),
      '#weight' => 20,
    ];
    $form['revision'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Create new revision'),
      '#description' => $this
        ->t('Revisions record changes between saves.'),
      '#default_value' => FALSE,
      '#access' => $current_user
        ->hasPermission('administer rng'),
      '#group' => 'revision_information',
    ];
  }
  return $form;
}