View source
<?php
namespace Drupal\datetime_extras\Plugin\Field\FieldWidget;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Datetime\Entity\DateFormat;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\datetime_range\Plugin\Field\FieldWidget\DateRangeDefaultWidget;
use Drupal\duration_field\Service\DurationServiceInterface;
use Drupal\duration_field\Service\GranularityServiceInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class DateRangeDurationWidget extends DateRangeDefaultWidget {
protected $durationService;
protected $granularityService;
public function setDurationService(DurationServiceInterface $duration_service) {
$this->durationService = $duration_service;
}
public function setGranularityService(GranularityServiceInterface $granularity_service) {
$this->granularityService = $granularity_service;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$instance
->setDurationService($container
->get('duration_field.service'));
$instance
->setGranularityService($container
->get('duration_field.granularity.service'));
return $instance;
}
public static function defaultSettings() {
return [
'default_duration' => [],
'duration_granularity' => 'd:h:i',
'time_increment' => '1',
] + parent::defaultSettings();
}
protected function getTimeIncrementOptions() {
return [
1 => $this
->t('1 second'),
30 => $this
->t('30 seconds'),
60 => $this
->t('1 minute'),
300 => $this
->t('5 minutes'),
600 => $this
->t('10 minutes'),
900 => $this
->t('15 minutes'),
1800 => $this
->t('30 minutes'),
3600 => $this
->t('1 hour'),
86400 => $this
->t('1 day'),
];
}
protected function getDefaultDurationInterval() {
$default_duration = $this
->getSetting('default_duration');
return $this->durationService
->convertDateArrayToDateInterval($default_duration);
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$element = [];
$element['duration_granularity'] = [
'#type' => 'granularity',
'#title' => t('Duration granularity'),
'#default_value' => $this
->getSetting('duration_granularity'),
];
$element['default_duration'] = [
'#type' => 'duration',
'#title' => t('Default duration'),
'#default_value' => $this
->getDefaultDurationInterval(),
'#granularity' => $this
->getSetting('duration_granularity'),
'#cardinality' => $this->fieldDefinition
->getFieldStorageDefinition()
->getCardinality(),
'#element_validate' => [],
];
$element['time_increment'] = [
'#type' => 'select',
'#title' => $this
->t('Time increment'),
'#default_value' => $this
->getSetting('time_increment'),
'#options' => $this
->getTimeIncrementOptions(),
];
return $element;
}
public function settingsSummary() {
$default_duration_interval = $this
->getDefaultDurationInterval();
$duration_granularity = $this
->getSetting('duration_granularity');
$time_increment = $this
->getSetting('time_increment');
$increment_options = $this
->getTimeIncrementOptions();
$summary = [];
$granularity_array = $this->granularityService
->convertGranularityStringToGranularityArray($duration_granularity);
$default_duration = $this->durationService
->getHumanReadableStringFromDateInterval($default_duration_interval, $granularity_array, ' ', 'short');
$summary['default_duration'] = $this
->t('Default duration: @duration', [
'@duration' => $default_duration,
]);
$summary['duration_granularity'] = $this
->t('Duration granularity: @granularity', [
'@granularity' => $duration_granularity,
]);
$summary['time_increment'] = $this
->t('Time increment : @increment', [
'@increment' => $increment_options[$time_increment],
]);
return $summary;
}
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$form_element = parent::formElement($items, $delta, $element, $form, $form_state);
$increment = $this
->getSetting('time_increment');
foreach ([
'value',
'end_value',
] as $sub_element) {
$form_element[$sub_element]['#date_increment'] = $increment;
if ($increment >= 86400) {
$form_element[$sub_element]['#date_time_format'] = '';
$form_element[$sub_element]['#date_time_element'] = 'none';
$form_element[$sub_element]['#date_time_callbacks'] = [];
}
}
$form_element['end_value']['#required'] = FALSE;
$item = $items[$delta];
if ($item->start_date) {
$start_date = $item->start_date;
}
if ($item->end_date) {
$end_date = $item->end_date;
}
if (!empty($start_date) && !empty($end_date)) {
$interval = $start_date
->diff($end_date);
}
$form_element['end_type'] = [
'#type' => 'radios',
'#options' => [
'duration' => $this
->t('Duration'),
'end_date' => $this
->t('End date'),
],
'#prefix' => '<div class="container-inline">',
'#suffix' => '</div>',
'#default_value' => 'duration',
'#weight' => '-5',
];
$form_element['value']['#weight'] = '-10';
$form_element['end_value']['#weight'] = '0';
$end_type_name = $this->fieldDefinition
->getName() . '[' . $delta . '][end_type]';
$form_element['end_value']['#theme_wrappers'] = [
'container',
];
$form_element['end_value']['#states']['visible'][] = [
':input[name="' . $end_type_name . '"]' => [
'value' => 'end_date',
],
];
$form_element['duration'] = [
'#type' => 'duration',
'#cardinality' => $this->fieldDefinition
->getFieldStorageDefinition()
->getCardinality(),
'#granularity' => $this
->getSetting('duration_granularity'),
'#date_increment' => $increment,
'#weight' => '10',
'#states' => [
'visible' => [
':input[name="' . $end_type_name . '"]' => [
'value' => 'duration',
],
],
],
];
if (empty($interval)) {
$interval = $this
->getDefaultDurationInterval();
}
$form_element['duration']['#default_value'] = $interval;
array_unshift($form_element['#element_validate'], [
get_class($this),
'validateDuration',
]);
return $form_element;
}
public static function validateDuration(array &$element, FormStateInterface $form_state, array &$complete_form) {
if ($element['end_type']['#value'] === 'duration') {
if (!empty($element['value']['#value']['object']) && $element['value']['#value']['object'] instanceof DrupalDateTime && !empty($element['duration']['#value'])) {
$date = clone $element['value']['#value']['object'];
$date
->add($element['duration']['#value']);
$end_element['#parents'] = array_merge($element['#parents'], [
'end_value',
]);
$form_state
->setValueForElement($end_element, $date);
$element['end_value']['#value'] = [
'date' => $date
->format(DateFormat::load('html_date')
->getPattern()),
'time' => $date
->format(DateFormat::load('html_time')
->getPattern()),
'object' => $date,
];
}
}
elseif (!empty($element['#required']) && empty($element['end_value']['#value']['object'])) {
$form_state
->setError($element, t('You must define either a duration or an end date.'));
}
}
}