class BookingConfirmationForm in Booking and Availability Management Tools for Drupal 8
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\bat_booking_example\Form\BookingConfirmationForm
Expanded class hierarchy of BookingConfirmationForm
File
- modules/
bat_booking/ bat_booking_example/ src/ Form/ BookingConfirmationForm.php, line 21 - Contains \Drupal\bat_booking_example\Form\BookingConfirmationForm.
Namespace
Drupal\bat_booking_example\FormView source
class BookingConfirmationForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'bat_booking_confirmation_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $start_date = NULL, $end_date = NULL, $type_id = NULL) {
$form['start_date'] = [
'#type' => 'hidden',
'#value' => $start_date
->format('Y-m-d'),
];
$form['end_date'] = [
'#type' => 'hidden',
'#value' => $end_date
->format('Y-m-d'),
];
$form['type_id'] = [
'#type' => 'hidden',
'#value' => $type_id,
];
$form['submit'] = [
'#type' => 'submit',
'#value' => 'Confirm booking',
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValues();
$event_type = 'availability_example';
$start_date = new \DateTime($values['start_date']);
$end_date = new \DateTime($values['end_date']);
$end_date
->sub(new \DateInterval('PT1M'));
$type_id = $values['type_id'];
$state_ids = array_keys(bat_event_get_states($event_type));
$state_store = new DrupalDBStore($event_type, DrupalDBStore::BAT_STATE);
$valid_states = array_merge([
0,
], array_slice($state_ids, 0, 1));
$drupal_units = bat_unit_load_multiple(FALSE, [
'unit_type_id' => $type_id,
]);
$bat_units = [];
foreach ($drupal_units as $unit_id => $unit) {
$bat_units[] = new Unit($unit_id, $unit
->getEventDefaultValue($event_type));
}
if (count($bat_units)) {
$calendar = new Calendar($bat_units, $state_store);
$response = $calendar
->getMatchingUnits($start_date, $end_date, $valid_states, []);
$valid_unit_ids = array_keys($response
->getIncluded());
if (count($valid_unit_ids)) {
// Create a new Event.
$event = bat_event_create([
'type' => $event_type,
'start_date' => $start_date
->format('Y-m-d H:i:s'),
'end_date' => $end_date
->format('Y-m-d H:i:s'),
'uid' => $this
->currentUser()
->id(),
]);
$event
->set('event_bat_unit_reference', reset($valid_unit_ids));
$event
->set('event_state_reference', end($state_ids));
$event
->save();
// Create a new Booking.
$booking = bat_booking_create([
'type' => 'standard',
'label' => 'Example Booking',
]);
$booking
->set('booking_start_date', $start_date
->format('Y-m-d H:i:s'));
$booking
->set('booking_end_date', $end_date
->format('Y-m-d H:i:s'));
$booking
->set('booking_event_reference', $event
->id());
$booking
->save();
$this
->messenger()
->addMessage(t('Booking created'));
}
else {
$this
->messenger()
->addError(t('No units'));
}
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BookingConfirmationForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
BookingConfirmationForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
BookingConfirmationForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
87 |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |