class OfficeHoursDatelist in Office Hours 8
Provides a one-line text field form element.
Plugin annotation
@FormElement("office_hours_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\office_hours\Element\OfficeHoursDatelist
- 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 OfficeHoursDatelist
File
- src/
Element/ OfficeHoursDatelist.php, line 14
Namespace
Drupal\office_hours\ElementView source
class OfficeHoursDatelist extends Datelist {
/**
* {@inheritdoc}
*/
public function getInfo() {
$parent_info = parent::getInfo();
$info = [
'#input' => TRUE,
'#tree' => TRUE,
'#process' => [
[
static::class,
'processOfficeHoursSlot',
],
],
'#element_validate' => [
[
static::class,
'validateOfficeHoursSlot',
],
],
// @see Drupal\Core\Datetime\Element\Datelist.
'#date_part_order' => [
'year',
'month',
'day',
'hour',
'minute',
],
// @see Drupal\Core\Datetime\Element\Datetime.
'#date_date_element' => 'none',
// {'none'|'date'}
'#date_time_element' => 'time',
// {'none'|'time'|'text'}
'#date_date_format' => 'none',
'#date_time_callbacks' => [],
// Can be used to add a jQuery time picker or an 'All day' checkbox.
'#date_year_range' => '1900:2050',
// @see Drupal\Core\Datetime\Element\DateElementBase.
'#date_timezone' => '+0000',
];
// #process, #validate bottom-up.
$info['#process'] = array_merge($parent_info['#process'], $info['#process']);
$info['#element_validate'] = array_merge($parent_info['#element_validate'], $info['#element_validate']);
return $info + $parent_info;
}
/**
* Callback for office_hours_select element.
*
* Takes #default_value and dissects it in hours, minutes and ampm indicator.
* Mimics the date_parse() function.
* - g = 12-hour format of an hour without leading zeros 1 through 12
* - G = 24-hour format of an hour without leading zeros 0 through 23
* - h = 12-hour format of an hour with leading zeros 01 through 12
* - H = 24-hour format of an hour with leading zeros 00 through 23
*
* @param array $element
* @param mixed $input
* @param \Drupal\Core\Form\FormStateInterface $form_state
*
* @return array|mixed|null
*/
public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
if ($input == FALSE) {
// Prepare the numeric value: use a DateTime value.
$time = $element['#default_value'];
$date = NULL;
try {
if (is_array($time)) {
$date = OfficeHoursDateHelper::createFromArray($time);
}
elseif (is_numeric($time)) {
$timezone = $element['#date_timezone'];
// The Date function needs a fixed format, so format $time to '0030'.
$time = OfficeHoursDatetime::get($time, 'Hi');
$date = OfficeHoursDateHelper::createFromFormat('Gi', $time, $timezone);
}
} catch (\Exception $e) {
$date = NULL;
}
$element['#default_value'] = $date;
}
// Set empty minutes field to '00' for better UX.
if (!empty($input['hour']) && isset($input['minute']) && empty($input['minute'])) {
$input['minute'] = '00';
}
$input = parent::valueCallback($element, $input, $form_state);
return $input;
}
/**
* Process the office_hours_select element before showing it.
*
* @param $element
* @param \Drupal\Core\Form\FormStateInterface $form_state
* @param $complete_form
*
* @return array
* The screen element.
*/
public static function processOfficeHoursSlot(&$element, FormStateInterface $form_state, &$complete_form) {
$element['hour']['#options'] = $element['#hour_options'];
return $element;
}
/**
* Validate the hours selector element.
*
* @param $element
* @param \Drupal\Core\Form\FormStateInterface $form_state
* @param $complete_form
*/
public static function validateOfficeHoursSlot(&$element, FormStateInterface $form_state, &$complete_form) {
$input = $element['#value'];
$value = '';
if (isset($input['object']) && $input['object']) {
$value = (string) $input['object']
->format('Gi');
// Set the value for usage in OfficeHoursList::validateOfficeHoursSlot().
$element['#value'] = $value;
}
$form_state
->setValueForElement($element, $value);
}
}
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:: |
protected static | function | Rounds minutes and seconds to nearest requested value. | |
Datelist:: |
public static | function | Expands a date element into an array of individual elements. | |
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. | |
OfficeHoursDatelist:: |
public | function |
Returns the element properties for this element. Overrides Datelist:: |
|
OfficeHoursDatelist:: |
public static | function | Process the office_hours_select element before showing it. | |
OfficeHoursDatelist:: |
public static | function | Validate the hours selector element. | |
OfficeHoursDatelist:: |
public static | function |
Callback for office_hours_select element. 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. |