View source
<?php
use Drupal\Core\Database\Query\AlterableInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityFormInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\Core\Render\Element;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\bat_event_series\Entity\EventSeries;
use Drupal\bat_event_series\Entity\EventSeriesType;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use RRule\RRule;
function bat_event_series_type_add_event_state_reference($bundle) {
$field_name = 'event_state_reference';
$field_storage = FieldStorageConfig::loadByName('bat_event_series', $field_name);
$field = FieldConfig::loadByName('bat_event_series', $bundle, $field_name);
if (empty($field_storage)) {
$field_storage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'bat_event_series',
'type' => 'entity_reference',
'cardinality' => 1,
'locked' => 1,
'settings' => [
'target_type' => 'state',
],
]);
$field_storage
->save();
}
if (empty($field)) {
$field = FieldConfig::create([
'field_storage' => $field_storage,
'entity_type' => 'bat_event_series',
'label' => 'State',
'bundle' => $bundle,
'required' => TRUE,
'settings' => [
'handler' => 'default',
'handler_settings' => [],
],
]);
$field
->save();
$form_display = \Drupal::entityTypeManager()
->getStorage('entity_form_display')
->load('bat_event_series.' . $bundle . '.default');
if (!$form_display) {
$form_display = EntityFormDisplay::create([
'targetEntityType' => 'bat_event_series',
'bundle' => $bundle,
'mode' => 'default',
'status' => TRUE,
]);
}
$form_display
->setComponent($field_name, [
'type' => 'entity_reference_autocomplete',
'weight' => 3,
]);
$form_display
->save();
}
}
function bat_event_series_type_add_event_dates_field($bundle) {
$field_name = 'event_dates';
$field_storage = FieldStorageConfig::loadByName('bat_event_series', $field_name);
$field = FieldConfig::loadByName('bat_event_series', $bundle, $field_name);
if (empty($field_storage)) {
$field_storage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'bat_event_series',
'type' => 'daterange',
'cardinality' => 1,
'locked' => 1,
'settings' => [
'datetime_type' => 'datetime',
],
]);
$field_storage
->save();
}
if (empty($field)) {
$field = FieldConfig::create([
'field_storage' => $field_storage,
'entity_type' => 'bat_event_series',
'label' => 'Event Dates',
'bundle' => $bundle,
'required' => TRUE,
'settings' => [
'datetime_type' => 'datetime',
],
]);
$field
->save();
$form_display = \Drupal::entityTypeManager()
->getStorage('entity_form_display')
->load('bat_event_series.' . $bundle . '.default');
if (!$form_display) {
$form_display = EntityFormDisplay::create([
'targetEntityType' => 'bat_event_series',
'bundle' => $bundle,
'mode' => 'default',
'status' => TRUE,
]);
}
$form_display
->setComponent($field_name, [
'type' => 'daterange_default',
'weight' => 1,
]);
$form_display
->save();
}
}
function bat_event_series_type_add_target_entity_field($bundle, $target_entity_type) {
$entity_info = \Drupal::entityTypeManager()
->getDefinition($target_entity_type);
$field_name = 'event_' . $target_entity_type . '_reference';
$field_storage = FieldStorageConfig::loadByName('bat_event_series', $field_name);
$field = FieldConfig::loadByName('bat_event_series', $bundle, $field_name);
if (empty($field_storage)) {
$field_storage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'bat_event_series',
'type' => 'entity_reference',
'cardinality' => 1,
'locked' => 1,
'settings' => [
'target_type' => $target_entity_type,
],
]);
$field_storage
->save();
}
if (empty($field)) {
$field = FieldConfig::create([
'field_storage' => $field_storage,
'entity_type' => 'bat_event_series',
'label' => $entity_info
->getLabel()
->__toString(),
'bundle' => $bundle,
'required' => TRUE,
'settings' => [
'handler' => 'default',
'handler_settings' => [],
],
]);
$field
->save();
$form_display = \Drupal::entityTypeManager()
->getStorage('entity_form_display')
->load('bat_event_series.' . $bundle . '.default');
if (!$form_display) {
$form_display = EntityFormDisplay::create([
'targetEntityType' => 'bat_event_series',
'bundle' => $bundle,
'mode' => 'default',
'status' => TRUE,
]);
}
$form_display
->setComponent($field_name, [
'type' => 'entity_reference_autocomplete',
'weight' => 3,
]);
$form_display
->save();
}
}
function bat_event_series_access(EntityInterface $entity, $operation, AccountInterface $account) {
return bat_entity_access($entity, $operation, $account);
}
function bat_event_series_type_access(EntityInterface $entity, $operation, AccountInterface $account) {
$account
->hasPermission('administer bat_event_series_type entities');
}
function bat_event_series_query_bat_event_series_access_alter(AlterableInterface $query) {
foreach ($query
->getTables() as $table) {
if ($table['table'] === 'event_series') {
bat_entity_access_query_alter($query, 'bat_event_series', $table['alias']);
break;
}
}
}
function bat_event_series_get_types($type_name = NULL, $reset = FALSE) {
if ($reset) {
\Drupal::entityTypeManager()
->getStorage('bat_event_series_type')
->resetCache();
}
$types = EventSeriesType::loadMultiple();
return isset($type_name) ? $types[$type_name] : $types;
}
function bat_event_series_delete(EventSeries $event) {
$event
->delete();
}
function bat_event_series_delete_multiple(array $event_ids) {
$events = EventSeries::loadMultiple($event_ids);
foreach ($events as $event) {
$event
->delete();
}
}
function bat_event_series_create($values = []) {
return EventSeries::create($values);
}
function bat_event_series_type_load($type) {
return EventSeriesType::load($type);
}
function bat_event_series_load($event_id = NULL, $reset = FALSE) {
if ($reset) {
\Drupal::entityTypeManager()
->getStorage('bat_event_series')
->resetCache([
$event_id,
]);
}
return EventSeries::load($event_id);
}
function bat_event_series_load_multiple(array $event_ids = NULL, $reset = FALSE) {
if ($reset) {
\Drupal::entityTypeManager()
->getStorage('bat_event_series')
->resetCache($event_ids);
}
return EventSeries::loadMultiple($event_ids);
}
function bat_event_series_entity_insert(EntityInterface $entity) {
if ($entity
->bundle() == 'bat_event_type') {
bat_event_series_create_event_series_field($entity
->id());
}
}
function bat_event_series_theme() {
return [
'bat_event_series' => [
'render element' => 'elements',
'template' => 'bat_event_series',
],
'bat_event_series_add_list' => [
'variables' => [
'content' => NULL,
],
],
];
}
function template_preprocess_bat_event_series_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_event_series.add_form', [
'event_series_type' => $type
->id(),
])),
];
}
}
}
function template_preprocess_bat_event_series(array &$variables) {
$event_series = $variables['elements']['#bat_event_series'];
foreach (Element::children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
$start = new \DateTime($event_series
->get('event_dates')->value);
$rrule = new RRule($event_series
->getRRule(), $start);
$rrule_human_readable = ucfirst($rrule
->humanReadable([
'date_formatter' => 'bat_event_series_rrule_date_formatter',
]));
$variables['content']['rrule'] = [
'#markup' => '<h2>' . t('RRule') . '</h2><div>' . $rrule_human_readable . '</div>',
];
$variables['content']['events_label'] = [
'#markup' => '<h2>' . t('Events') . '</h2>',
];
$variables['content']['events'] = views_embed_view('event_series', 'events', $event_series
->id());
}
function bat_event_series_rrule_date_formatter($date) {
return $date
->format('Y-m-d');
}
function bat_event_series_create_event_series_field($bundle) {
$field_name = 'event_series';
$field_storage = FieldStorageConfig::loadByName('bat_event', $field_name);
$field = FieldConfig::loadByName('bat_event', $bundle, $field_name);
if (empty($field_storage)) {
$field_storage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'bat_event',
'type' => 'entity_reference',
'cardinality' => 1,
'locked' => 1,
'settings' => [
'target_type' => 'bat_event_series',
],
]);
$field_storage
->save();
}
if (empty($field)) {
$field = FieldConfig::create([
'field_storage' => $field_storage,
'entity_type' => 'bat_event',
'label' => 'Event series',
'bundle' => $bundle,
'required' => FALSE,
'settings' => [
'handler' => 'default',
'handler_settings' => [],
],
]);
$field
->save();
}
}
function bat_event_series_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$form_object = $form_state
->getFormObject();
if ($form_object instanceof EntityFormInterface) {
$entity = $form_object
->getEntity();
if ($entity
->getEntityTypeId() == 'bat_event') {
if ($form_id == 'bat_event_' . $entity
->bundle() . '_edit_form' || $form_id == 'bat_event_' . $entity
->bundle() . '_form') {
if ($event_series_id = $entity
->get('event_series')->target_id) {
$form['event_series'] = [
'#markup' => '<div>' . Link::createFromRoute(t('View other reservations in this series'), 'entity.bat_event_series.canonical', [
'bat_event_series' => $event_series_id,
])
->toString() . '</div>',
'#weight' => 999,
];
$form['delete_event_series'] = [
'#markup' => '<div>' . Link::createFromRoute(t('Delete remaining events in this series'), 'entity.bat_event_series.delete_events_form', [
'bat_event_series' => $event_series_id,
])
->toString() . '</div>',
'#title' => t('Delete remaining events in this series'),
'#weight' => 1000,
];
}
}
}
}
}