You are here

function registration_form in Entity Registration 8

Same name and namespace in other branches
  1. 8.2 includes/registration.forms.inc \registration_form()
  2. 7.2 includes/registration.forms.inc \registration_form()
  3. 7 includes/registration.forms.inc \registration_form()

Form callback: create or edit a registration.

Parameters

Registration $registration: The registration object to edit or for a create form an empty registration object with a node defined.

2 string references to 'registration_form'
registration_menu in ./registration.module
@FIXME This implementation of hook_menu() cannot be automatically converted because it contains logic (i.e., branching statements, function calls, object instantiation, etc.) You will need to convert it manually. Sorry!
registration_register_page in ./registration.module
Page callback: Add a new registration to a host entity.

File

includes/registration.forms.inc, line 15
Form definitions and callbacks for Registration.

Code

function registration_form($form, &$form_state, Registration $registration) {
  $wrapper = entity_metadata_wrapper('registration', $registration);
  $user = $wrapper->user
    ->value();
  $host = $wrapper->entity
    ->value();
  $state = $wrapper->state
    ->value();
  $type = $wrapper->type
    ->value();
  $form_state['registration'] = $registration;
  $who_options = registration_access_people($registration);

  // Default value for who is registering.
  $who_default = NULL;
  if (isset($registration->registration_id)) {
    $who_default = $registration
      ->registrant_type(\Drupal::currentUser());
  }
  elseif (count($who_options) == 1) {
    $keys = array_keys($who_options);
    $who_default = reset($keys);
  }

  // Show a message if there's only one option as we're going to hide the form.
  if (count($who_options) == 1 && !\Drupal::currentUser()
    ->isAnonymous()) {
    $form['who_message'] = array(
      '#markup' => '<div class="registration-who-msg">' . t('You are registering: %who', array(
        '%who' => current($who_options),
      )) . '</div>',
    );
  }
  $form['who_is_registering'] = array(
    '#type' => 'select',
    '#title' => t('This registration is for:'),
    '#options' => $who_options,
    '#default_value' => $who_default,
    '#required' => TRUE,
    '#access' => count($who_options) > 1,
  );
  $form['user'] = array(
    '#type' => 'textfield',
    '#title' => t('User'),
    '#default_value' => $user ? $user->name : '',
    '#maxlength' => 60,
    '#size' => 30,
    '#description' => t('Select a user by typing their username to get a list of matches.'),
    '#autocomplete_path' => 'user/autocomplete',
    '#access' => isset($who_options[REGISTRATION_REGISTRANT_TYPE_USER]),
    '#states' => array(
      'visible' => array(
        ':input[name="who_is_registering"]' => array(
          'value' => REGISTRATION_REGISTRANT_TYPE_USER,
        ),
      ),
    ),
  );
  $form['anon_mail'] = array(
    '#type' => 'textfield',
    '#title' => t('Email'),
    '#description' => t('The email to associate with this registration.'),
    '#default_value' => isset($registration->anon_mail) ? $registration->anon_mail : '',
    '#size' => 40,
    '#maxlength' => 255,
    '#access' => isset($who_options[REGISTRATION_REGISTRANT_TYPE_ANON]),
    '#states' => array(
      'visible' => array(
        ':input[name="who_is_registering"]' => array(
          'value' => REGISTRATION_REGISTRANT_TYPE_ANON,
        ),
      ),
    ),
  );
  $settings = registration_entity_settings($registration->entity_type, $registration->entity_id);
  $capacity = $settings['capacity'];
  $limit = isset($settings['settings']['maximum_spaces']) ? $settings['settings']['maximum_spaces'] : 1;

  // Just in case it was unset:
  $settings['settings']['maximum_spaces'] = $limit;
  $remaining = $capacity - registration_event_count($registration->entity_type, $registration->entity_id);
  if ($capacity && $limit) {
    $description = t('The number of spaces you wish to reserve. @spaces_remaining spaces remaining. You may register up to @max spaces.', array(
      '@spaces_remaining' => $remaining,
      '@max' => min($limit, $remaining),
    ));
  }
  elseif ($capacity) {
    $description = t('The number of spaces you wish to reserve. @spaces_remaining spaces remaining.', array(
      '@spaces_remaining' => $remaining,
    ));
  }
  elseif ($limit) {
    $description = t('The number of spaces you wish to reserve. You may register up to @max spaces.', array(
      '@max' => $limit,
    ));
  }
  else {
    $description = t('The number of spaces you wish to reserve.');
  }
  $form['count'] = array(
    '#type' => 'textfield',
    '#title' => t('Spaces'),
    '#description' => $description,
    '#default_value' => isset($registration->count) ? $registration->count : 1,
    '#size' => \Drupal\Component\Utility\Unicode::strlen($remaining),
    '#access' => isset($settings['settings']['maximum_spaces']) && $settings['settings']['maximum_spaces'] == 1 ? FALSE : TRUE,
    '#element_validate' => array(
      'element_validate_integer_positive',
      '_registration_validate_space_request',
    ),
  );
  $default_state = registration_get_default_state($type);
  $states = registration_get_states_options(array(
    'show_on_form' => TRUE,
  ));

  // Ensure default state is in options or it won't be set.
  if (!in_array($default_state->label, $states)) {
    $states[$default_state->name] = $default_state->label;
  }
  $form['state'] = array(
    '#type' => 'select',
    '#title' => t('State'),
    '#description' => t('State of this registration'),
    '#default_value' => $state ? $state
      ->identifier() : $default_state
      ->identifier(),
    '#options' => $states,
    '#access' => !empty($states) && \Drupal::currentUser()
      ->hasPermission('edit ' . $registration->type . ' registration state'),
  );
  field_attach_form('registration', $registration, $form, $form_state);
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save Registration'),
  );

  // Add a delete button for existing registration:
  if (isset($registration->registration_id) && entity_access('delete', 'registration', $registration)) {
    $form['actions']['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete'),
      '#submit' => array(
        'registration_form_delete_submit',
      ),
      "#limit_validation_errors" => array(),
    );
  }
  if ($host && entity_access('view', $registration->entity_type, $host)) {
    $uri = entity_uri($registration->entity_type, $host);
    $form['actions']['cancel'] = array(
      '#type' => 'link',
      '#title' => t('Cancel'),
      '#href' => $uri['path'],
    );
  }
  $form['#registration_settings'] = $settings;
  return $form;
}