class HijriDateList in Hijri 3.0.x
Same name and namespace in other branches
- 8.2 src/Element/HijriDateList.php \Drupal\hijri\Element\HijriDateList
- 1.0.x src/Element/HijriDateList.php \Drupal\hijri\Element\HijriDateList
Override the datelist element.
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\hijri\Element\HijriDateList
- 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 HijriDateList
1 file declares its use of HijriDateList
- hijri.module in ./
hijri.module - This module provides Hijri Date integration with Drupal core date field and with other Drupal contributions such as Views and Date.
File
- src/
Element/ HijriDateList.php, line 15
Namespace
Drupal\hijri\ElementView source
class HijriDateList extends Datelist {
/**
* {@inheritdoc}
*/
public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
// If the input is defined.
if ($input !== FALSE) {
if (isset($input['year']) && $input['year'] > 0) {
// The year value has been set.
$return = $input;
$date = date('c');
$return['object'] = $date;
// $form_state->setError($element, t('Test error.'));
return $return;
}
}
else {
// This will be called when prepare the input for form.
}
return $input;
}
/**
* {@inheritdoc}
*/
public static function processDatelist(&$element, FormStateInterface $form_state, &$complete_form) {
// 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'])) {
// We will do nothing.
}
else {
$element['#date_timezone'] = date_default_timezone_get();
}
// Load the date helper to prepare some needful arrays.
$date_helper = new DateHelper();
$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':
// Generating the days array. We will assume September since September
// is only 30 days.
$options = $date_helper
->days($element['#required'], 9, 2000);
$format = 'j';
$title = t('Day');
break;
case 'month':
/**
*
*
* @var \Drupal\hijri\HijriFormatter $hijri
*/
$hijri = \Drupal::service('hijri.formatter');
$options = $hijri
->hijriMonthNames();
$format = 'n';
$title = t('Month');
break;
case 'year':
// @todo User should be able to set year range in field settings.
/**
*
*
* @var \Drupal\hijri\HijriFormatter $hijri
*/
$hijri = \Drupal::service('hijri.formatter');
// @todo Support year range instead of current year.
$current_year = $hijri
->format(time(), 'custom', 'Y');
$format = 'n';
$title = t('Year');
$min = '';
$max = '';
$range = range(empty($min) ? intval($current_year - 3) : $min, empty($max) ? intval($current_year + 3) : $max);
// Making the keys equal values.
$range = array_combine($range, $range);
// We may allow for an empty option if applicable.
$options = $range;
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 | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
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. | |
HijriDateList:: |
public static | function |
Expands a date element into an array of individual elements. Overrides Datelist:: |
|
HijriDateList:: |
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:: |
|
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
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:: |
2 |
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. | 98 |
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. | 4 |
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. |