Newer
Older
<?php
/**
* @file
* Tokens for the recurring_events module.
*/
use Drupal\Core\Render\BubbleableMetadata;
/**
* Implements hook_token_info().
*/
function recurring_events_token_info() {
$eventinstance = [];
$eventinstance_type = [
'name' => t('Event Instance'),
'description' => t('Tokens for the eventinstance entity type.'),
'needs-data' => 'eventinstance',
];
$eventinstance['title'] = [
'description' => t('The title of the event instance.'),
];
$eventinstance['description'] = [
'description' => t('The description of the event instance.'),
];
$eventinstance['date'] = [
'name' => t('Event Instance Start Date'),
'description' => t('The start date of the event instance.'),
];
$eventinstance['end_date'] = [
'name' => t('Event Instance End Date'),
'description' => t('The end date of the event instance.'),
'description' => t('The URL of the event instance.'),
];
$eventseries = [];
$eventseries_type = [
'name' => t('Event Series'),
'description' => t('Tokens for the eventseries entity type.'),
'needs-data' => 'eventseries',
];
$eventseries['title'] = [
'description' => t('The title of the event series.'),
];
$eventseries['body'] = [
'name' => t('Event Series Body'),
'description' => t('The description of the event series.'),
];
return [
'types' => [
'eventinstance' => $eventinstance_type,
'eventseries' => $eventseries_type,
],
'tokens' => [
'eventinstance' => $eventinstance,
'eventseries' => $eventseries,
],
];
}
/**
* Implements hook_tokens().
*/
function recurring_events_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
$replacements = [];
if ($type == 'eventinstance' && !empty($data['eventinstance'])) {
$event_instance = $data['eventinstance'];

Aakash K O
committed
$timezone = new \DateTimeZone(date_default_timezone_get());
foreach ($tokens as $name => $original) {
$date_format = \Drupal::config('recurring_events.eventinstance.config')->get('date_format');
// Extract the date format if provided.
$custom_date_format_indicator = ":custom:";
if (strpos($name, $custom_date_format_indicator) !== FALSE) {
$date_format = substr($name, strpos($name, $custom_date_format_indicator) + strlen($custom_date_format_indicator));
$name = substr($name, 0, strpos($name, $custom_date_format_indicator));
}
$replacements[$original] = ($event_instance->hasField('title')) ? $event_instance->title->value : '';
$replacements[$original] = ($event_instance->hasField('description')) ? $event_instance->description->value : '';
$replacements[$original] = $event_instance->date->start_date->setTimezone($timezone)->format($date_format);
break;
case 'end_date':
$replacements[$original] = $event_instance->date->end_date->setTimezone($timezone)->format($date_format);
$replacements[$original] = $event_instance->toUrl('canonical')->setAbsolute(TRUE)->toString();
if ($type == 'eventseries' && !empty($data['eventseries'])) {
$event_series = $data['eventseries'];
foreach ($tokens as $name => $original) {
switch ($name) {
case 'title':
$replacements[$original] = $event_series->title->value;
break;
$replacements[$original] = $event_series->body->value;
break;
}
}
}