Using Field Collections to Allocate Spaces with Validation

Created on 3 September 2016, about 8 years ago
Updated 12 June 2023, over 1 year ago

I am trying to build an event registration where the primary registrant can register many other attendees on his/her registration.

I want to collect Name, Email, Address, and Phone Number for the primary registrant and only Name and Email for the other attendees that he/she registers.

So far here is what I have done:

  • Set the Spaces allowed setting to 0 to allow for one registrant to reserve multiple spaces
  • Added a field collection (field_testing) with Name and Email to the registration type that I am using and set it to unlimited
  • Created a module that uses the following function (from this blog post.):
/**
 * Implements hook_entity_presave
 * @param type $entity
 * @param type $type
 */
function additional_registrants_entity_presave($entity, $type) {
}
  if ($type == 'registration') {
    $count = 1;
    $field_testing = $entity->field_testing;
    foreach ( $field_testing[LANGUAGE_NONE] as $item) {
      $count++;
    }
    $entity->count = $count;
  }
}
  • I want to create some functionality that will check to see if there are enough spaces left in the event for more people to register each time someone clicks the "add more" button on the field collection (field_testing) to add another attendee under their primary registration. So far this has meen my stumbling block. I tried to use the following code to accomplish this (again from this blog post.). Unfortunately this code does not work.
/*
 * Adds a validation hook to the beginning of the #element_validate
 * to provide custom validation on the required fields within the
 * widget.
 */
function additional_registrants_field_widget_form_alter(&$element, &$form_state, $context) {
  switch ($context['instance']['widget']['type']) {
    case 'field_collection_embed':
      // Check for our specific field.
      if ($context['instance']['field_name'] == 'field_testing') {
        array_unshift($element['#element_validate'], 'additional_registrants_field_collection_validate');
      }
      break;
  }
}

function additional_registrants_field_collection_validate($element, &$form_state, $form) {
  if ($form_state['triggering_element']['#name'] == 'field_testing_add_more') { 
    // ID and entity_type of the entity (in our case event node).
    $entity_id = $form_state['registration']->entity_id;
    $entity_type = $form_state['registration']->entity_type;
 
    // Get registration settings for the node
    $settings = registration_entity_settings($entity_type, $entity_id);
 
    // Get total capacity
    $capacity = $settings['capacity'];
    if ($capacity != 0) {
      $used_slots = registration_event_count($entity_type, $entity_id);
      $extra_registrants_count = 0;
      foreach ($form_state['values']['field_testing'][LANGUAGE_NONE] as $item) {
        $extra_registrants_count++;
      }
      // 1 from the registration, add the extra registrants already filled in and one more as we want to add one.
      $registration_slots = 1 + $extra_registrants_count;
 
      if ($capacity - $used_slots - $registration_slots < 0) {
        $node = node_load($entity_id);
        $error_message = t('This event is fully booked. You cannot add another person.');
        form_error($form['field_testing'][LANGUAGE_NONE][0]['field_reg_extra_name'], $error_message);
      }
    }
  }
}

Does anyone have any ideas as to how to make this work?

Thanks!

✨ Feature request
Status

Closed: outdated

Version

1.6

Component

Registration Core

Created by

πŸ‡ΊπŸ‡ΈUnited States andyanderso

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024