class DatetimeRangeWidget in Typed Data API enhancements 8
Plugin implementation of the 'datetime_range' widget.
Plugin annotation
@TypedDataFormWidget(
id = "datetime_range",
label = @Translation("Datetime Range"),
description = @Translation("A datetime range input widget, containing two datetime values, for the start and end of the range."),
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\typed_data\Widget\FormWidgetBase implements ContainerFactoryPluginInterface, FormWidgetInterface
- class \Drupal\typed_data\Plugin\TypedDataFormWidget\DatetimeRangeWidget
- class \Drupal\typed_data\Widget\FormWidgetBase implements ContainerFactoryPluginInterface, FormWidgetInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of DatetimeRangeWidget
File
- src/
Plugin/ TypedDataFormWidget/ DatetimeRangeWidget.php, line 26
Namespace
Drupal\typed_data\Plugin\TypedDataFormWidgetView source
class DatetimeRangeWidget extends FormWidgetBase {
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return parent::defaultConfiguration() + [
'label' => NULL,
'description' => NULL,
];
}
/**
* {@inheritdoc}
*/
public function isApplicable(DataDefinitionInterface $definition) {
return is_subclass_of($definition
->getClass(), DateTimeInterface::class);
}
/**
* {@inheritdoc}
*/
public function form(TypedDataInterface $data, SubformStateInterface $form_state) {
$value = $data
->getValue();
$form = SubformState::getNewSubForm();
$form['#theme_wrappers'][] = 'fieldset';
$form['#title'] = $this->configuration['label'] ?: $data
->getDataDefinition()
->getLabel();
$now = \Drupal::time()
->getRequestTime();
$date_formatter = \Drupal::service('date.formatter');
$params = [
'%timezone' => $date_formatter
->format($now, 'custom', 'T (e) \\U\\T\\CP'),
'%daylight_saving' => $date_formatter
->format($now, 'custom', 'I') ? $this
->t('currently in daylight saving mode') : $this
->t('not in daylight saving mode'),
];
$timezone_info = $this
->t('Timezone : %timezone %daylight_saving.', $params);
$form['#description'] = ($this->configuration['description'] ?: $data
->getDataDefinition()
->getDescription()) . '<br>' . $timezone_info;
$form['#element_validate'][] = [
$this,
'validateStartEnd',
];
$form['value'] = [
'#type' => 'datetime',
'#title' => $this
->t('Start date'),
'#default_value' => $this
->createDefaultDateTime($value['value']),
'#required' => $data
->getDataDefinition()
->isRequired(),
];
$form['end_value'] = [
'#type' => 'datetime',
'#title' => $this
->t('End date'),
'#default_value' => $this
->createDefaultDateTime($value['end_value']),
'#required' => $data
->getDataDefinition()
->isRequired(),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function extractFormValues(TypedDataInterface $data, SubformStateInterface $form_state) {
$value = $form_state
->getValue('value');
$end_value = $form_state
->getValue('end_value');
$format = DateFormat::load('html_datetime')
->getPattern();
if ($value instanceof DrupalDateTime) {
$value = $value
->format($format);
}
if ($end_value instanceof DrupalDateTime) {
$end_value = $end_value
->format($format);
}
$data
->setValue([
'value' => $value,
'end_value' => $end_value,
]);
}
/**
* {@inheritdoc}
*/
public function flagViolations(TypedDataInterface $data, ConstraintViolationListInterface $violations, SubformStateInterface $formState) {
foreach ($violations as $violation) {
/** @var ConstraintViolationInterface $violation */
$formState
->setErrorByName('value', $violation
->getMessage());
}
}
/**
* {@inheritdoc}
*/
public function getConfigurationDefinitions(DataDefinitionInterface $definition) {
return [
'label' => DataDefinition::create('string')
->setLabel($this
->t('Label')),
'description' => DataDefinition::create('string')
->setLabel($this
->t('Description')),
];
}
/**
* Ensure that the start date <= the end date.
*
* @param array $element
* An associative array containing the properties and children of the
* generic form element.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param array $complete_form
* The complete form structure.
*/
public function validateStartEnd(array &$element, FormStateInterface $form_state, array &$complete_form) {
$start_date = $element['value']['#value']['object'];
$end_date = $element['end_value']['#value']['object'];
if ($start_date instanceof DrupalDateTime && $end_date instanceof DrupalDateTime) {
if ($start_date
->getTimestamp() !== $end_date
->getTimestamp()) {
$interval = $start_date
->diff($end_date);
if ($interval->invert === 1) {
$form_state
->setError($element, $this
->t('The @title end date cannot be before the start date', [
'@title' => $element['#title'],
]));
}
}
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DatetimeRangeWidget:: |
public | function |
Gets default configuration for this plugin. Overrides FormWidgetBase:: |
|
DatetimeRangeWidget:: |
public | function |
Extracts the data value from submitted form values. Overrides FormWidgetInterface:: |
|
DatetimeRangeWidget:: |
public | function |
Reports validation errors against actual form elements. Overrides FormWidgetInterface:: |
|
DatetimeRangeWidget:: |
public | function |
Creates the widget's form elements for editing the given data. Overrides FormWidgetInterface:: |
|
DatetimeRangeWidget:: |
public | function |
Defines the supported configuration settings. Overrides FormWidgetInterface:: |
|
DatetimeRangeWidget:: |
public | function |
Returns if the widget can be used for the provided data. Overrides FormWidgetInterface:: |
|
DatetimeRangeWidget:: |
public | function | Ensure that the start date <= the end date. | |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormWidgetBase:: |
protected | property | The typed data plugin manager. | |
FormWidgetBase:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
FormWidgetBase:: |
public | function | Create a default DrupalDateTime object. | |
FormWidgetBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
FormWidgetBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
FormWidgetBase:: |
public | function |
Constructs a FormWidgetBase object. Overrides PluginBase:: |
|
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |