You are here

function registration_form_submit in Entity Registration 8

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

Submit callback for registration_form().

File

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

Code

function registration_form_submit($form, &$form_state) {
  $registration = $form_state['registration'];

  // Set the registration's author uid:
  if (!isset($registration->registration_id)) {
    $registration->author_uid = \Drupal::currentUser()->uid;
  }
  $registration->count = $form_state['values']['count'];
  if (!empty($form_state['values']['state'])) {
    $registration->state = $form_state['values']['state'];
  }
  switch ($form_state['values']['who_is_registering']) {
    case REGISTRATION_REGISTRANT_TYPE_ANON:
      $registration->anon_mail = trim($form_state['values']['anon_mail']);
      break;
    case REGISTRATION_REGISTRANT_TYPE_ME:
      $user = \Drupal::currentUser();
      $registration->user_uid = $user->uid;
      break;
    case REGISTRATION_REGISTRANT_TYPE_USER:
      if ($reg_user = user_load_by_name($form_state['values']['user'])) {
        $registration->user_uid = $reg_user->uid;
      }
      break;
  }

  // Notify field widgets.
  field_attach_submit('registration', $registration, $form, $form_state);

  // Save the registration and redirect.
  if (registration_save($registration)) {
    $reg_config = registration_entity_settings($registration->entity_type, $registration->entity_id);
    if (!empty($reg_config['settings']['confirmation']) && strlen($reg_config['settings']['confirmation'])) {
      drupal_set_message(t($reg_config['settings']['confirmation']));
    }
    $wrapper = entity_metadata_wrapper('registration', $registration);
    $host = $wrapper->entity
      ->value();

    // Set redirect to configured value, if there is one.
    if (!empty($reg_config['settings']['confirmation_redirect']) && strlen($reg_config['settings']['confirmation_redirect'])) {
      $form_state['redirect'] = $reg_config['settings']['confirmation_redirect'];
    }
    else {

      // Redirect to registration:
      if (entity_access('view', 'registration', $registration)) {
        $uri = entity_uri('registration', $registration);
        $form_state['redirect'] = $uri['path'];
      }
      else {
        if (entity_access('view', $registration->entity_type, $host)) {
          $uri = entity_uri($registration->entity_type, $host);
          $form_state['redirect'] = $uri['path'];
        }
      }
    }
  }
  else {
    drupal_set_message(t('There was a problem submitting your registration.'));
  }
}