class DatexDateTime in Datex 8
Plugin annotation
@FormElement("datetime");
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\Core\Render\Element\RenderElement implements ElementInterface
- class \Drupal\Core\Render\Element\FormElement implements FormElementInterface
- class \Drupal\Core\Datetime\Element\DateElementBase
- class \Drupal\Core\Datetime\Element\Datetime
- class \Drupal\datex\Element\DatexDateTime
- class \Drupal\Core\Datetime\Element\Datetime
- class \Drupal\Core\Datetime\Element\DateElementBase
- class \Drupal\Core\Render\Element\FormElement implements FormElementInterface
- class \Drupal\Core\Render\Element\RenderElement implements ElementInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of DatexDateTime
1 file declares its use of DatexDateTime
- DatexTimestampDatetimeNoDefaultWidget.php in src/
Plugin/ Field/ FieldWidget/ DatexTimestampDatetimeNoDefaultWidget.php
File
- src/
Element/ DatexDateTime.php, line 14
Namespace
Drupal\datex\ElementView source
class DatexDateTime extends Datetime {
public function getInfo() {
return [
'#date_date_element' => 'text',
] + parent::getInfo();
}
public static function formatExample($format) {
return (new DatexDrupalDateTime())
->format($format);
}
public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
$e_cal = datex_factory();
if (!$e_cal) {
return parent::valueCallback($element, $input, $form_state);
}
if ($input !== FALSE) {
$date_input = $element['#date_date_element'] != 'none' && !empty($input['date']) ? $input['date'] : '';
$time_input = $element['#date_time_element'] != 'none' && !empty($input['time']) ? $input['time'] : '';
$date_format = $element['#date_date_element'] != 'none' ? static::getHtml5DateFormat($element) : '';
$time_format = $element['#date_time_element'] != 'none' ? static::getHtml5TimeFormat($element) : '';
$timezone = !empty($element['#date_timezone']) ? $element['#date_timezone'] : NULL;
$e_cal = datex_factory($timezone, 'en');
// Seconds will be omitted in a post in case there's no entry.
$date = NULL;
if (!empty($time_input) && strlen($time_input) == 5) {
$time_input .= ':00';
}
try {
$date_time_format = trim($date_format . ' ' . $time_format);
$date_time_input = trim($date_input . ' ' . $time_input);
if (empty($date_time_input)) {
// pass
}
elseif ($e_cal
->parse($date_time_input, $date_time_format)) {
$date = DrupalDateTime::createFromTimestamp($e_cal
->getTimestamp(), $timezone);
}
else {
$form_state
->setError($element, t('Date is not valid.'));
}
} catch (\Exception $e) {
$date = NULL;
}
$input = [
'date' => $date_input,
'time' => $time_input,
'object' => $date,
];
}
else {
$date = $element['#default_value'];
if ($date) {
$date = DatexDrupalDateTime::convert($date);
}
if ($date instanceof DrupalDateTime && !$date
->hasErrors()) {
$input = [
'date' => $date
->format($element['#date_date_format']),
'time' => $date
->format($element['#date_time_format']),
'object' => $date,
];
}
else {
$input = [
'date' => '',
'time' => '',
'object' => NULL,
];
}
}
return $input;
}
public static function processDatetime(&$element, FormStateInterface $form_state, &$complete_form) {
$element = static::xProcessDatetime($element, $form_state, $complete_form);
$date = !empty($element['#value']['object']) ? $element['#value']['object'] : NULL;
if ($element['#date_date_element'] != 'none') {
$e_cal = datex_factory($element['#date_timezone'], 'en');
if ($date instanceof DrupalDateTime && !$date
->hasErrors()) {
}
else {
}
}
if (isset($element['date'])) {
if ($e_cal) {
$attr =& $element['date']['#attributes'];
$date = !empty($date) ? $date : new \DateTime();
if (isset($element['#date_year_range'])) {
$range = DatexDateList::datexDatetimeRangeYears($element['#date_year_range'], $date, 'gregorian');
}
$e_cal
->setDateLocale($range[0], 1, 1);
$attr['min'] = $e_cal
->getTimestamp();
$e_cal
->setDateLocale($range[1], 1, 1);
$attr['max'] = $e_cal
->getTimestamp();
$attr['data-datex-calendar'] = $e_cal
->getCalendarName();
$attr['autocomplete'] = 'off';
$attr['data-datex-format'] = $element['#date_date_format'];
}
}
return $element;
}
private static function xProcessDatetime(&$element, FormStateInterface $form_state, &$complete_form) {
$format_settings = [];
// The value callback has populated the #value array.
$date = !empty($element['#value']['object']) ? $element['#value']['object'] : NULL;
// Set a fallback timezone.
if ($date instanceof DrupalDateTime) {
$element['#date_timezone'] = $date
->getTimezone()
->getName();
}
elseif (empty($element['#timezone'])) {
$element['#date_timezone'] = drupal_get_user_timezone();
}
$element['#tree'] = TRUE;
if ($element['#date_date_element'] != 'none') {
$date_format = $element['#date_date_element'] != 'none' ? static::getHtml5DateFormat($element) : '';
$date_value = !empty($date) ? $date
->format($date_format, $format_settings) : $element['#value']['date'];
// Creating format examples on every individual date item is messy, and
// placeholders are invalid for HTML5 date and datetime, so an example
// format is appended to the title to appear in tooltips.
$extra_attributes = [
'title' => t('Date (e.g. @format)', [
'@format' => static::formatExample($date_format),
]),
'type' => $element['#date_date_element'],
];
// Adds the HTML5 date attributes.
if ($date instanceof DrupalDateTime && !$date
->hasErrors()) {
$html5_min = clone $date;
$range = DatexDateList::datexDatetimeRangeYears($element['#date_year_range'], DrupalDateTime::createFromTimestamp($date
->getTimestamp()));
$html5_min
->setDate($range[0], 1, 1)
->setTime(0, 0, 0);
$html5_max = clone $date;
$html5_max
->setDate($range[1], 12, 31)
->setTime(23, 59, 59);
$extra_attributes += [
'min' => $html5_min
->format($date_format, $format_settings),
'max' => $html5_max
->format($date_format, $format_settings),
];
}
$element['date'] = [
'#type' => 'date',
'#title' => t('Date'),
'#title_display' => 'invisible',
'#value' => $date_value,
'#attributes' => $element['#attributes'] + $extra_attributes,
'#required' => $element['#required'],
'#size' => max(12, strlen($element['#value']['date'])),
'#error_no_message' => TRUE,
'#date_date_format' => $element['#date_date_format'],
];
// Allows custom callbacks to alter the element.
if (!empty($element['#date_date_callbacks'])) {
foreach ($element['#date_date_callbacks'] as $callback) {
if (function_exists($callback)) {
$callback($element, $form_state, $date);
}
}
}
}
if ($element['#date_time_element'] != 'none') {
$time_format = $element['#date_time_element'] != 'none' ? static::getHtml5TimeFormat($element) : '';
$time_value = !empty($date) ? $date
->format($time_format, $format_settings) : $element['#value']['time'];
// Adds the HTML5 attributes.
$extra_attributes = [
'title' => t('Time (e.g. @format)', [
'@format' => static::formatExample($time_format),
]),
'type' => $element['#date_time_element'],
'step' => $element['#date_increment'],
];
$element['time'] = [
'#type' => 'date',
'#title' => t('Time'),
'#title_display' => 'invisible',
'#value' => $time_value,
'#attributes' => $element['#attributes'] + $extra_attributes,
'#required' => $element['#required'],
'#size' => 12,
'#error_no_message' => TRUE,
];
// Allows custom callbacks to alter the element.
if (!empty($element['#date_time_callbacks'])) {
foreach ($element['#date_time_callbacks'] as $callback) {
if (function_exists($callback)) {
$callback($element, $form_state, $date);
}
}
}
}
return $element;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DateElementBase:: |
protected static | function | Specifies the start and end year to use as a date range. | |
DateElementBase:: |
protected static | function | Returns the most relevant title of a datetime element. | |
Datetime:: |
protected static | property | ||
Datetime:: |
protected static | function | Retrieves the right format for a HTML5 date element. | |
Datetime:: |
protected static | function | Retrieves the right format for a HTML5 time element. | |
Datetime:: |
public static | function |
Form element processing handler for the #ajax form property. Overrides RenderElement:: |
|
Datetime:: |
public static | function | Validation callback for a datetime element. | |
DatexDateTime:: |
public static | function |
Creates an example for a date format. Overrides Datetime:: |
|
DatexDateTime:: |
public | function |
Returns the element properties for this element. Overrides Datetime:: |
|
DatexDateTime:: |
public static | function |
Expands a datetime element type into date and/or time elements. Overrides Datetime:: |
|
DatexDateTime:: |
public static | function |
Determines how user input is mapped to an element's #value property. Overrides Datetime:: |
|
DatexDateTime:: |
private static | function | ||
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 | |
FormElement:: |
public static | function | Adds autocomplete functionality to elements. | |
FormElement:: |
public static | function | #process callback for #pattern form element property. | |
FormElement:: |
public static | function | #element_validate callback for #pattern form element property. | |
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. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 92 |
RenderElement:: |
public static | function | Adds Ajax information about an element to communicate with JavaScript. | |
RenderElement:: |
public static | function | Adds members of this group as actual elements for rendering. | |
RenderElement:: |
public static | function | Arranges elements into groups. | |
RenderElement:: |
public static | function |
Sets a form element's class attribute. Overrides ElementInterface:: |
|
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. |