View source
<?php
use Drupal\Core\Url;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Link;
use Drupal\Core\Session\AccountInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\bat_booking\Entity\Booking;
use Drupal\bat_booking\Entity\BookingBundle;
function bat_booking_access(EntityInterface $entity, $operation, AccountInterface $account) {
return bat_entity_access($entity, $operation, $account);
}
function bat_booking_type_access($op, $unit = NULL, $account = NULL) {
return \Drupal::currentUser()
->hasPermission('administer bat_booking_bundle entities', $account);
}
function bat_booking_create($values = []) {
return Booking::create($values);
}
function bat_booking_type_create($values = []) {
return BookingBundle::create($values);
}
function bat_booking_type_load($bundle, $reset = FALSE) {
if ($reset) {
\Drupal::entityTypeManager()
->getStorage('bat_booking_bundle')
->resetCache();
}
return BookingBundle::load($bundle);
}
function bat_booking_get_bundles($type_name = NULL, $reset = FALSE) {
if ($reset) {
\Drupal::entityTypeManager()
->getStorage('bat_booking_bundle')
->resetCache();
}
$types = BookingBundle::loadMultiple();
return isset($type_name) ? $types[$type_name] : $types;
}
function bat_booking_type_save(BookingBundle $booking_type) {
$booking_type
->save();
}
function bat_booking_type_delete(BookingBundle $bundle) {
$bundle
->delete();
}
function bat_booking_load($booking_id, $reset = FALSE) {
$bookings = bat_booking_load_multiple([
$booking_id,
], [], $reset);
return reset($bookings);
}
function bat_booking_load_multiple($booking_ids = [], $conditions = [], $reset = FALSE) {
if ($reset) {
\Drupal::entityTypeManager()
->getStorage('bat_booking')
->resetCache();
}
if (!empty($conditions)) {
$query = \Drupal::entityQuery('bat_booking');
if (!empty($booking_ids)) {
$query
->condition('id', $booking_ids, 'IN');
}
foreach ($conditions as $key => $value) {
$query
->condition($key, $value);
}
$booking_ids = $query
->execute();
}
return Booking::loadMultiple($booking_ids);
}
function bat_booking_delete(Booking $booking) {
$booking
->delete();
}
function bat_booking_theme() {
return [
'bat_booking_add_list' => [
'variables' => [
'content' => NULL,
],
],
];
}
function template_preprocess_bat_booking_add_list(&$variables) {
$variables['types'] = [];
if (!empty($variables['content'])) {
foreach ($variables['content'] as $type) {
$variables['types'][$type
->id()] = [
'type' => $type
->id(),
'add_link' => Link::fromTextAndUrl($type
->label(), new Url('entity.bat_booking.add', [
'booking_bundle' => $type
->id(),
])),
];
}
}
}
function bat_booking_add_start_date_field($bundle) {
$field_name = 'booking_start_date';
$field_storage = FieldStorageConfig::loadByName('bat_booking', $field_name);
$field = FieldConfig::loadByName('bat_booking', $bundle, $field_name);
if (empty($field_storage)) {
$field_storage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'bat_booking',
'type' => 'datetime',
'cardinality' => 1,
'locked' => 1,
'settings' => [
'cache_count' => 4,
'cache_enabled' => 0,
'granularity' => [
'day' => 'day',
'hour' => 'hour',
'minute' => 'minute',
'month' => 'month',
'second' => 0,
'year' => 'year',
],
'timezone_db' => '',
'todate' => '',
'tz_handling' => 'none',
],
]);
$field_storage
->save();
}
if (empty($field)) {
$field = FieldConfig::create([
'field_name' => $field_name,
'entity_type' => 'bat_booking',
'label' => 'Start Date',
'bundle' => $bundle,
'required' => FALSE,
'settings' => [
'default_value' => 'blank',
'default_value2' => 'same',
'default_value_code' => '',
'default_value_code2' => '',
'user_register_form' => FALSE,
],
]);
$field
->save();
}
}
function bat_booking_add_end_date_field($bundle) {
$field_name = 'booking_end_date';
$field_storage = FieldStorageConfig::loadByName('bat_booking', $field_name);
$field = FieldConfig::loadByName('bat_booking', $bundle, $field_name);
if (empty($field_storage)) {
$field_storage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'bat_booking',
'type' => 'datetime',
'cardinality' => 1,
'locked' => 1,
'settings' => [
'cache_count' => 4,
'cache_enabled' => 0,
'granularity' => [
'day' => 'day',
'hour' => 'hour',
'minute' => 'minute',
'month' => 'month',
'second' => 0,
'year' => 'year',
],
'timezone_db' => '',
'todate' => '',
'tz_handling' => 'none',
],
]);
$field_storage
->save();
}
if (empty($field)) {
$field = FieldConfig::create([
'field_name' => $field_name,
'entity_type' => 'bat_booking',
'label' => 'End Date',
'bundle' => $bundle,
'required' => FALSE,
'settings' => [
'default_value' => 'blank',
'default_value2' => 'same',
'default_value_code' => '',
'default_value_code2' => '',
'user_register_form' => FALSE,
],
]);
$field
->save();
}
}
function bat_booking_add_event_reference_field($bundle) {
$field_name = 'booking_event_reference';
$field_storage = FieldStorageConfig::loadByName('bat_booking', $field_name);
$field = FieldConfig::loadByName('bat_booking', $bundle, $field_name);
if (empty($field_storage)) {
$field_storage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'bat_booking',
'type' => 'entity_reference',
'cardinality' => 1,
'locked' => 1,
'settings' => [
'target_type' => 'bat_event',
],
]);
$field_storage
->save();
}
if (empty($field)) {
$field = FieldConfig::create([
'field_name' => $field_name,
'entity_type' => 'bat_booking',
'label' => 'Event',
'bundle' => $bundle,
'required' => FALSE,
]);
$field
->save();
}
}
function bat_booking_entity_insert(EntityInterface $entity) {
if ($entity
->bundle() == 'bat_booking_bundle') {
bat_booking_add_start_date_field($entity
->id());
bat_booking_add_end_date_field($entity
->id());
bat_booking_add_event_reference_field($entity
->id());
}
}