class PersianDateList in Persian Date for Drupal 8 8
Same name and namespace in other branches
- 8.4 src/Element/PersianDateList.php \Drupal\persian_date\Element\PersianDateList
Class PersianDateList
Plugin annotation
@FormElement("datelist");
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\Datelist
- class \Drupal\persian_date\Element\PersianDateList
- class \Drupal\Core\Datetime\Element\Datelist
- 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 PersianDateList
1 #type use of PersianDateList
- PersianDateTimeDatelistWidget::formElement in src/
Plugin/ Field/ FieldWidget/ PersianDateTimeDatelistWidget.php - Returns the form for a single field widget.
File
- src/
Element/ PersianDateList.php, line 23
Namespace
Drupal\persian_date\ElementView source
class PersianDateList extends Datelist {
public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
$parts = $element['#date_part_order'];
$increment = $element['#date_increment'];
$date = NULL;
if ($input !== FALSE) {
$return = $input;
if (empty(static::checkEmptyInputs($input, $parts))) {
if (isset($input['ampm'])) {
if ($input['ampm'] == 'pm' && $input['hour'] < 12) {
$input['hour'] += 12;
}
elseif ($input['ampm'] == 'am' && $input['hour'] == 12) {
$input['hour'] -= 12;
}
unset($input['ampm']);
}
$timezone = !empty($element['#date_timezone']) ? $element['#date_timezone'] : NULL;
try {
$date = DrupalDateTime::createFromArray($input, $timezone);
} catch (\Exception $e) {
$form_state
->setError($element, t('Selected combination of day and month is not valid.'));
}
if ($date instanceof DrupalDateTime && !$date
->hasErrors()) {
static::incrementRound($date, $increment);
}
}
}
else {
$return = array_fill_keys($parts, '');
if (!empty($element['#default_value'])) {
$date = $element['#default_value'];
if ($date instanceof DrupalDateTime && !$date
->hasErrors()) {
static::incrementRound($date, $increment);
foreach ($parts as $part) {
switch ($part) {
case 'day':
$format = 'j';
break;
case 'month':
$format = 'n';
break;
case 'year':
$format = 'Y';
break;
case 'hour':
$format = in_array('ampm', $element['#date_part_order']) ? 'g' : 'G';
break;
case 'minute':
$format = 'i';
break;
case 'second':
$format = 's';
break;
case 'ampm':
$format = 'a';
break;
default:
$format = '';
}
$return[$part] = $date
->format($format);
}
}
}
}
$timezone = isset($timezone) ?: null;
$date = PersianDrupalDateTime::createFromFormat('Y-m-d H:i:s', $date
->format('Y-m-d H:i:s'), $timezone);
$date = DrupalDateTime::createFromDateTime($date
->getDateTime());
$return['object'] = $date;
return $return;
}
public static function processDatelist(&$element, FormStateInterface $form_state, &$complete_form) {
// Load translated date part labels from the appropriate calendar plugin.
$date_helper = new PersianDateHelper();
// 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'] = $element['#date_timezone'];
}
else {
$element['#date_timezone'] = drupal_get_user_timezone();
}
$element['#tree'] = TRUE;
// Determine the order of the date elements.
$order = !empty($element['#date_part_order']) ? $element['#date_part_order'] : [
'year',
'month',
'day',
];
$text_parts = !empty($element['#date_text_parts']) ? $element['#date_text_parts'] : [];
// Output multi-selector for date.
foreach ($order as $part) {
switch ($part) {
case 'day':
$options = $date_helper
->days($element['#required']);
$format = 'j';
$title = t('Day');
break;
case 'month':
$options = $date_helper
->monthNamesAbbr($element['#required']);
$format = 'n';
$title = t('Month');
break;
case 'year':
$range = static::datetimeRangeYears($element['#date_year_range'], $date);
$options = $date_helper
->years($range[0], $range[1], $element['#required']);
$format = 'Y';
$title = t('Year');
break;
case 'hour':
$format = in_array('ampm', $element['#date_part_order']) ? 'g' : 'G';
$options = $date_helper
->hours($format, $element['#required']);
$title = t('Hour');
break;
case 'minute':
$format = 'i';
$options = $date_helper
->minutes($format, $element['#required'], $element['#date_increment']);
$title = t('Minute');
break;
case 'second':
$format = 's';
$options = $date_helper
->seconds($format, $element['#required'], $element['#date_increment']);
$title = t('Second');
break;
case 'ampm':
$format = 'a';
$options = $date_helper
->ampm($element['#required']);
$title = t('AM/PM');
break;
default:
$format = '';
$options = [];
$title = '';
}
$default = isset($element['#value'][$part]) && trim($element['#value'][$part]) != '' ? $element['#value'][$part] : '';
$value = $date instanceof DrupalDateTime && !$date
->hasErrors() ? $date
->format($format) : $default;
if (!empty($value) && $part != 'ampm') {
$value = intval($value);
}
$element['#attributes']['title'] = $title;
$element[$part] = [
'#type' => in_array($part, $text_parts) ? 'textfield' : 'select',
'#title' => $title,
'#title_display' => 'invisible',
'#value' => $value,
'#attributes' => $element['#attributes'],
'#options' => $options,
'#required' => $element['#required'],
'#error_no_message' => FALSE,
'#empty_option' => $title,
];
}
// 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);
}
}
}
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. | |
Datelist:: |
protected static | function | Checks the input array for empty values. | |
Datelist:: |
public | function |
Returns the element properties for this element. Overrides ElementInterface:: |
|
Datelist:: |
protected static | function | Rounds minutes and seconds to nearest requested value. | |
Datelist:: |
public static | function | Validation callback for a datelist element. | |
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. | |
PersianDateList:: |
public static | function |
Expands a date element into an array of individual elements. Overrides Datelist:: |
|
PersianDateList:: |
public static | function |
Validates the date type to adjust 12 hour time and prevent invalid dates.
If the date is valid, the date is set in the form. Overrides Datelist:: |
|
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 | Form element processing handler for the #ajax form property. | 1 |
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. |