View source
<?php
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\smart_date\Plugin\Field\FieldType\SmartDateFieldItemList;
use Drupal\smart_date\SmartDateTrait;
function smart_date_token_info() {
if (!\Drupal::hasService('token.entity_mapper')) {
return;
}
$types = [];
$tokens = [];
foreach (\Drupal::entityTypeManager()
->getDefinitions() as $entity_type_id => $entity_type) {
if (!$entity_type
->entityClassImplements(ContentEntityInterface::class)) {
continue;
}
$token_type = \Drupal::service('token.entity_mapper')
->getTokenTypeForEntityType($entity_type_id);
if (empty($token_type)) {
continue;
}
$fields = \Drupal::service('entity_field.manager')
->getFieldStorageDefinitions($entity_type_id);
foreach ($fields as $field_name => $field) {
if ($field
->getType() != 'smartdate') {
continue;
}
$tokens[$token_type . '-' . $field_name]['value-custom'] = [
'name' => t('Start, custom format'),
'description' => NULL,
'dynamic' => TRUE,
'module' => 'smart_date',
];
$tokens[$token_type . '-' . $field_name]['end_value-custom'] = [
'name' => t('End, custom format'),
'description' => NULL,
'dynamic' => TRUE,
'module' => 'smart_date',
];
$tokens[$token_type . '-' . $field_name]['format'] = [
'name' => t('Formatted by Smart Date Format'),
'description' => t('Provide the id of a specific smart date format to use it for formattng.'),
'dynamic' => TRUE,
'module' => 'smart_date',
];
$tokens[$token_type . '-' . $field_name]['value-format'] = [
'name' => t('Start, formatted by Smart Date Format'),
'description' => t('Provide the id of a specific smart date format to use it for formattng.'),
'dynamic' => TRUE,
'module' => 'smart_date',
];
$tokens[$token_type . '-' . $field_name]['end_value-format'] = [
'name' => t('End, formatted by Smart Date Format'),
'description' => t('Provide the id of a specific smart date format to use it for formattng.'),
'dynamic' => TRUE,
'module' => 'smart_date',
];
}
}
return [
'types' => $types,
'tokens' => $tokens,
];
}
function smart_date_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
$replacements = [];
if (empty($data['field_property'])) {
return $replacements;
}
foreach ($tokens as $token => $original) {
$list = $data[$data['field_name']];
if (!$list instanceof SmartDateFieldItemList) {
continue;
}
$delta = 0;
$parts = explode(':', $token, 2);
if (is_numeric($parts[0])) {
if (count($parts) > 1) {
$delta = $parts[0];
$parts = explode(':', $parts[1], 2);
$property_name = $parts[0];
$format_value = $parts[1];
}
else {
continue;
}
}
else {
$property_name = $parts[0];
$format_value = $parts[1];
}
if (!$format_value) {
continue;
}
$name_parts = explode('-', $property_name);
$approach = array_pop($name_parts);
$field = $list
->first();
if ($approach == 'custom') {
if ($name_parts && ($prop_needed = array_pop($name_parts))) {
$field_ts = $field
->get($prop_needed)
->getValue();
$replacements[$original] = \Drupal::service('date.formatter')
->format($field_ts, '', $format_value);
}
}
elseif ($approach == 'format') {
$format = \Drupal::getContainer()
->get('entity_type.manager')
->getStorage('smart_date_format')
->load($format_value);
$settings = $format
->getOptions();
if ($name_parts && ($prop_needed = array_pop($name_parts))) {
$field_ts = $field
->get($prop_needed)
->getValue();
$replacements[$original] = SmartDateTrait::formatSmartDate($field_ts, $field_ts, $settings);
}
else {
$start = $field
->get('value')
->getValue();
$end = $field
->get('end_value')
->getValue();
$replacements[$original] = SmartDateTrait::formatSmartDate($start, $end, $settings);
}
}
}
return $replacements;
}