0

I would like to know if it is possible to set-up a specific event registration webform to close automatically when the number of total reserved seats (reserved seats field) reaches a certain maximum.

A little background: I have a few different event registration forms on my website, each event is held in a different venue with a maximum number of seats. The forms each have a reserved seats field, users can reserve up-to 2 seats(max) per submission. I cannot use the submission limit feature of the webform because the number of submissions do not map directly to the number of total seats that users are reserving.

Is it possible to have the webform calculate the sum of the reserved seats field and close the form when it reaches a certain maximum number? How would I go about setting that up?

0

You would have to create custom WebformHandler that calculates the sum for a specified element and closes the form. This handler could be reusable and contributed back to Drupal.

The WebformHandler would allow site builders to select the 'Number' element, specify the allows total per webform, source entity, and/or user and then block the form once the specified limit is reached.

  • Okay, I'm kind of new to drupal, is there a guide on how to create custom Webform Handlers? Can you suggest an existing handler that I can look to for example code? – ssa4b Mar 20 '18 at 20:44
  • There are a few articles about Webform Handlers here. drupal.org/docs/8/modules/webform/… – jrockowitz Mar 20 '18 at 21:29
0

I built a custom handler to process seat reservations. I just wanted to share my code for anyone else looking to do the same thing. @jrockowitz let me know if you want to add this into webform core.

<?php
namespace Drupal\webform_reservations\Plugin\WebformHandler;


use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\Plugin\WebformHandlerBase;
use Drupal\webform\webformSubmissionInterface;

/**
* Form submission handler.
*
* @WebformHandler(
* id = "webform_reservations_handler",
* label = @Translation("Reservations Handler"),
* category = @Translation("Form Handler"),
* description = @Translation("Manage maximum seat reservations"),
* cardinality = 
 \Drupal\webform\Plugin\WebformHandlerInterface::CARDINALITY_SINGLE,
* results = \Drupal\webform\Plugin\WebformHandlerInterface::RESULTS_PROCESSED,
* )
*/

class ReservationHandler extends WebformHandlerBase {


 /**
 * {@inheritdoc}
 */

  public function submitForm(array &$form, FormStateInterface $form_state, 
  WebformSubmissionInterface $webform_submission) {
  $webform = $webform_submission->getWebform();
  $formid = $webform->id();

//query the db for all submissions of current webform submission
$query = \Drupal::entityQuery('webform_submission');
$query->condition('webform_id', $formid);
$result = $query->execute();

//load all the submissions from the results
$storage = \Drupal::entityTypeManager()->getStorage('webform_submission');
$submissions = $storage->loadMultiple($result);
$submission_data = array();


foreach ($submissions as $submission) {
$submission_data[] = $submission->getData();
}

$current_reserved = 0;

//add up all the values in the 'seats needed' field of each submission and save it to a variable.     
for ($i = 0; $i < count($submission_data); $i++){
    $current_reserved += $submission_data[$i]['seats_needed'];
}

//when a registration form is submitted, check if the current reserved seats exceeds the value specifed in the maximum allowed seats field of the form.
if ($current_reserved >= $webform->getElement('max_seats')['#value']) {
    $webform->setStatus('closed');
    $webform->save();

 $message = 'The form has ' . $current_reserved . ' reserved seats, the max seats is currently set to ' . $webform->getElement('max_seats')['#value'] .  ' the form will now be closed';        
  } else {$message = 'The form has ' . $current_reserved . ' reserved seats, the max seats is currently set to ' . $webform->getElement('max_seats')['#value'] .  ', the form will stay open';}        

//log the output to the drupal logger, to indicate if the webform is still open or closed after each submission.        
\Drupal::logger('webform_reservations')->info($message);    

}    
}
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.