SmartDateDefaultFormatter.php in Smart Date 8.2
File
src/Plugin/Field/FieldFormatter/SmartDateDefaultFormatter.php
View source
<?php
namespace Drupal\smart_date\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\datetime\Plugin\Field\FieldFormatter\DateTimeDefaultFormatter;
use Drupal\smart_date\Entity\SmartDateFormat;
use Drupal\smart_date\SmartDateTrait;
class SmartDateDefaultFormatter extends DateTimeDefaultFormatter {
use SmartDateTrait;
public static function defaultSettings() {
return [
'format' => 'default',
'force_chronological' => 0,
] + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$form = parent::settingsForm($form, $form_state);
unset($form['format_type']);
if (isset($form['timezone_override'])) {
$form['timezone_override']['#description'] = $this
->t('The time zone selected here will be used unless overridden on an individual date.');
}
$smartDateFormatOptions = $this
->getAvailableSmartDateFormatOptions();
$form['format'] = [
'#type' => 'select',
'#title' => $this
->t('Smart Date Format'),
'#description' => $this
->t('Choose which display configuration to use.'),
'#default_value' => $this
->getSetting('format'),
'#options' => $smartDateFormatOptions,
];
$form['force_chronological'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Force chronogical'),
'#description' => $this
->t('Override any manual sorting or other differences.'),
'#default_value' => $this
->getSetting('force_chronological'),
];
return $form;
}
public function settingsSummary() {
$summary[] = $this
->getSetting('timezone_override') === '' ? t('No timezone override.') : t('Timezone overridden to %timezone.', [
'%timezone' => $this
->getSetting('timezone_override'),
]);
$summary[] = t('Smart date format: %format.', [
'%format' => $this
->getSetting('format'),
]);
return $summary;
}
protected function getAvailableSmartDateFormatOptions() {
$formatOptions = [];
$smartDateFormats = \Drupal::entityTypeManager()
->getStorage('smart_date_format')
->loadMultiple();
foreach ($smartDateFormats as $type => $format) {
if ($format instanceof SmartDateFormat) {
$formatted = static::formatSmartDate(time(), time() + 3600, $format
->getOptions(), NULL, 'string');
$formatOptions[$type] = $format
->label() . ' (' . $formatted . ')';
}
}
return $formatOptions;
}
}