You are here

public function RegistrantFields::getFields in RNG - Events and Registrations 3.x

Same name and namespace in other branches
  1. 8.2 src/Form/RegistrantFields.php \Drupal\rng\Form\RegistrantFields::getFields()

GetFields returns the standard fields for a Registrant.

Parameters

array $form:

\Drupal\Core\Form\FormStateInterface $form_state:

\Drupal\rng\Entity\RegistrantInterface $registrant:

Return value

array

Throws

\Drupal\rng\Exception\InvalidEventException

File

src/Form/RegistrantFields.php, line 55

Class

RegistrantFields
Class RegistrantFields

Namespace

Drupal\rng\Form

Code

public function getFields(array $form, FormStateInterface $form_state, RegistrantInterface $registrant) {
  $parents = $form['#parents'] ?? [];

  /** @var \Drupal\rng\Entity\RegistrationInterface $registration */
  $registration = $registrant
    ->getRegistration();
  if ($registration) {
    $event = $registration
      ->getEvent();
    $event_meta = $registration
      ->getEventMeta();
    $event_type = $event_meta
      ->getEventType();
  }
  if (empty($event)) {
    throw new InvalidEventException('No event found for registrant.');
  }
  $form['identity_types'] = [
    '#type' => 'details',
    '#title' => 'Registrant Type',
    '#tree' => TRUE,
    '#open' => TRUE,
  ];
  $form['details'] = [
    '#type' => 'container',
    '#title' => 'Attendee Info',
  ];

  // Set #parents to 'top-level' by default.
  $form['#parents'] = $parents;
  $form['#type'] = 'container';
  $form['#event'] = $event;
  $form['#value'] = [
    $registrant,
  ];
  $form['#attributes']['class'][] = 'registrant-grid';
  $form['#wrapper_attributes']['class'][] = 'registrant-grid';
  $form['#attached']['library'][] = 'rng/rng.elements.registrants';
  $form['#element_validate'] = [
    [
      get_class($this),
      'validateForm',
    ],
  ];
  $form['#tree'] = TRUE;
  if ($identity = $registrant
    ->getIdentity()) {

    // show identity type
    // show identity display
    // if fields, show edit button
    // show remove button
    $type = $identity
      ->getEntityType()
      ->getLabel();
    $form['identity_types']['type'] = [
      '#type' => 'markup',
      '#markup' => $type,
    ];
    $form['identity_types']['remove_identity'] = [
      '#type' => 'submit',
      '#value' => t('Change Attendee'),
      '#submit' => [
        '\\Drupal\\rng\\Form\\RegistrantFields::removeIdentity',
        '::save',
      ],
    ];
    $form['details']['registrant'] = [
      '#type' => 'markup',
      '#markup' => $registrant
        ->label(),
    ];
  }
  else {

    // show register options buttons -
    // me
    // user - email address
    // profile - select/add widget
    $form_fields = [
      '#parents' => array_merge($parents, [
        'details',
      ]),
    ];
    $form_display = EntityFormDisplay::collectRenderDisplay($registrant, 'compact');
    $form_display
      ->buildForm($registrant, $form_fields, $form_state);
    $entity_fields = Element::children($form_fields);
    $form['details'] += $form_fields;
    $form['#allow_creation'] = $event_meta
      ->getCreatableIdentityTypes();
    $form['#allow_reference'] = $event_meta
      ->getIdentityTypes();

    // $element = RegistrantsElementUtility::findElement($form, $form_state);
    $utility = new RegistrantsElementUtility($form, $form_state);
    $types = $utility
      ->peopleTypeOptions();
    $default_type = array_keys($types)[0];
    $form['identity_types']['for_bundle'] = [
      '#type' => 'radios',
      '#title' => t('Person type'),
      '#options' => $types,
      '#default_value' => $default_type,
      '#parents' => array_merge($parents, [
        'identity_types',
      ]),
      '#tree' => TRUE,
    ];
    $selectors = $form['identity_types']['for_bundle']['#parents'];
    $item = array_shift($selectors);
    if (count($selectors)) {
      $item .= '[' . implode('][', $selectors) . ']';
    }
    $for_selector = ':input[name="' . $item . '"]';
    foreach ($entity_fields as $element) {
      $form['details'][$element]['#states'] = [
        'visible' => [
          [
            $for_selector => [
              'value' => 'anon:',
            ],
          ],
        ],
      ];
    }
    if ($event_type
      ->getAutoAttachUsers()) {
      $form['details'][$event_type
        ->getRegistrantEmailField()]['#states']['visible'][] = [
        $for_selector => [
          'value' => 'user:user',
        ],
      ];
    }
    $form['details']['myself'] = [
      '#type' => 'item',
      '#title' => t('Registration will be associated with your user account on save.'),
      '#states' => [
        'visible' => [
          [
            $for_selector => [
              'value' => 'myself:',
            ],
          ],
        ],
      ],
    ];
  }
  if ($registrant && !$registrant
    ->isNew()) {
    $form['#title'] = t('Edit registrant');
  }
  return $form;
}