class WebformTime in Webform 6.x
Same name in this branch
- 6.x src/Element/WebformTime.php \Drupal\webform\Element\WebformTime
- 6.x src/Plugin/WebformElement/WebformTime.php \Drupal\webform\Plugin\WebformElement\WebformTime
Same name and namespace in other branches
- 8.5 src/Element/WebformTime.php \Drupal\webform\Element\WebformTime
Provides a webform element for time selection.
$form['time'] = array(
'#type' => 'webform_time',
'#title' => $this
->t('Time'),
'#default_value' => '12:00 AM',
);
Plugin annotation
@FormElement("webform_time");
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\webform\Element\WebformTime
- 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 WebformTime
3 #type uses of WebformTime
- DateTime::form in src/
Plugin/ WebformElement/ DateTime.php - Gets the actual configuration webform array to be built.
- WebformExampleCustomFormSettingsForm::buildForm in modules/
webform_example_custom_form/ src/ Form/ WebformExampleCustomFormSettingsForm.php - Form constructor.
- WebformTime::form in src/
Plugin/ WebformElement/ WebformTime.php - Gets the actual configuration webform array to be built.
File
- src/
Element/ WebformTime.php, line 23
Namespace
Drupal\webform\ElementView source
class WebformTime extends FormElement {
/**
* {@inheritdoc}
*/
public function getInfo() {
$class = get_class($this);
return [
'#input' => TRUE,
'#theme' => 'input__webform_time',
'#process' => [
[
$class,
'processWebformTime',
],
],
'#pre_render' => [
[
$class,
'preRenderWebformTime',
],
],
'#theme_wrappers' => [
'form_element',
],
'#timepicker' => FALSE,
'#time_format' => 'H:i',
'#size' => 12,
'#maxlength' => 12,
];
}
/**
* {@inheritdoc}
*/
public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
if ($input === FALSE) {
// Set default value using GNU PHP date format.
// @see https://www.gnu.org/software/tar/manual/html_chapter/tar_7.html#Date-input-formats.
if (!empty($element['#default_value'])) {
try {
// Evaluate if the value can be time formatted, including relative
// values like 'now' or '+2 hours'.
new \DateTime($element['#default_value']);
} catch (\Exception $exception) {
\Drupal::messenger()
->addError($exception
->getMessage());
return NULL;
}
$element['#default_value'] = static::formatTime('H:i', strtotime($element['#default_value']));
return $element['#default_value'];
}
else {
return NULL;
}
}
return $input;
}
/**
* Processes a time webform element.
*
* @param array $element
* The webform element to process. Properties used:
* - #time_format: The time format used in PHP formats.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param array $complete_form
* The complete webform structure.
*
* @return array
* The processed element.
*/
public static function processWebformTime(&$element, FormStateInterface $form_state, &$complete_form) {
// Add validate callback.
$element += [
'#element_validate' => [],
];
array_unshift($element['#element_validate'], [
get_called_class(),
'validateWebformTime',
]);
$element['#attached']['library'][] = 'webform/webform.element.time';
$element['#attributes']['data-webform-time-format'] = !empty($element['#time_format']) ? $element['#time_format'] : DateFormat::load('html_time')
->getPattern();
return $element;
}
/**
* Webform element validation handler for #type 'webform_time'.
*
* Note that #required is validated by _form_valistatic::formatTime() already.
*/
public static function validateWebformTime(&$element, FormStateInterface $form_state, &$complete_form) {
$has_access = Element::isVisibleElement($element);
$value = $element['#value'];
if (trim($value) === '') {
return;
}
$time = strtotime($value);
// Make the submitted value is parsable and in the specified
// custom format (ex: g:i A) or default format (ex: H:i).
//
// This accounts for time values generated by an HTML5 time input or
// the jQuery Timepicker.
//
// @see \Drupal\webform\Plugin\WebformElement\DateBase::validateDate
// @see https://github.com/jonthornton/jquery-timepicker
// @see js/webform.element.time.js
$is_valid_time = $time && ($value === static::formatTime('H:i', $time) || $value === static::formatTime('H:i:s', $time) || $value === static::formatTime($element['#time_format'], $time));
if (!$is_valid_time) {
if ($has_access) {
if (isset($element['#title'])) {
$form_state
->setError($element, t('%name must be a valid time.', [
'%name' => $element['#title'],
]));
}
else {
$form_state
->setError($element);
}
}
return;
}
$name = empty($element['#title']) ? $element['#parents'][0] : $element['#title'];
$time_format = $element['#time_format'];
// Ensure that the input is greater than the #min property, if set.
if ($has_access && isset($element['#min'])) {
$min = strtotime($element['#min']);
if ($time < $min) {
$form_state
->setError($element, t('%name must be on or after %min.', [
'%name' => $name,
'%min' => static::formatTime($time_format, $min),
]));
}
}
// Ensure that the input is less than the #max property, if set.
if ($has_access && isset($element['#max'])) {
$max = strtotime($element['#max']);
if ($time > $max) {
$form_state
->setError($element, t('%name must be on or before %max.', [
'%name' => $name,
'%max' => static::formatTime($time_format, $max),
]));
}
}
// Convert time to 'H:i:s' format.
$value = static::formatTime('H:i:s', $time);
$element['#time_format'] = 'H:i:s';
$element['#value'] = $value;
$form_state
->setValueForElement($element, $value);
}
/**
* Adds form-specific attributes to a 'date' #type element.
*
* @param array $element
* An associative array containing the properties of the element.
*
* @return array
* The $element with prepared variables ready for #theme 'input__time'.
*/
public static function preRenderWebformTime(array $element) {
if (!empty($element['#timepicker'])) {
// Render simple text field that is converted to timepicker.
$element['#attributes']['type'] = 'text';
// Apply #time_format to #default_value.
if (!empty($element['#value']) && !empty($element['#default_value']) && $element['#value'] === $element['#default_value']) {
$element['#value'] = static::formatTime($element['#attributes']['data-webform-time-format'], strtotime($element['#value']));
}
}
else {
$element['#attributes']['type'] = 'time';
}
Element::setAttributes($element, [
'id',
'name',
'type',
'value',
'size',
'maxlength',
'min',
'max',
'step',
]);
static::setAttributes($element, [
'form-time',
'webform-time',
]);
return $element;
}
/**
* Format custom time.
*
* @param string $custom_format
* A PHP date format string suitable for input to date().
* @param int $timestamp
* (optional) A UNIX timestamp to format.
*
* @return string
* Formatted time.
*/
protected static function formatTime($custom_format, $timestamp = NULL) {
/** @var \Drupal\Core\Datetime\DateFormatterInterface $date_formatter */
$date_formatter = \Drupal::service('date.formatter');
return $date_formatter
->format($timestamp ?: time(), 'custom', $custom_format);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
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. | |
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. | |
WebformTime:: |
protected static | function | Format custom time. | |
WebformTime:: |
public | function |
Returns the element properties for this element. Overrides ElementInterface:: |
|
WebformTime:: |
public static | function | Adds form-specific attributes to a 'date' #type element. | |
WebformTime:: |
public static | function | Processes a time webform element. | |
WebformTime:: |
public static | function | Webform element validation handler for #type 'webform_time'. | |
WebformTime:: |
public static | function |
Determines how user input is mapped to an element's #value property. Overrides FormElement:: |