class DurationElement in Duration Field 8.2
Same name and namespace in other branches
- 3.0.x src/Element/DurationElement.php \Drupal\duration_field\Element\DurationElement
Defines the 'duration' form element.
Takes a PHP DateInterval or an ISO 8601 duration string as its value, and returns a PHP DateInterval value when submitted.
Properties:
- #default_value: A valid duration (either DateInterval or string).
- #date_increment: The number of seconds between valid duration values. 1 for every second, 60 for 1 minute, 300 for 5 minutes, etc.
- #granularity: The granularity string, for example 'y:m:d:h:i:s'.
- #required_elements: Granularity string of required elements, if any.
Usage example:
$form['duration'] = [
'#type' => 'duration',
'#title' => $this->t('Duration),
// Only 15 minute increments.
'#date_increment' => 900,
// Hide seconds.
'#granularity' => 'y:m:d:h:i',
// Require hours and minutes.
'#required_elements' => 'h:i',
);
Plugin annotation
@FormElement("duration");
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\duration_field\Element\DurationElement uses ProcessStatesTrait
- 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 DurationElement
See also
Drupal\duration_field\Plugin\DataType\GranularityStringData
3 #type uses of DurationElement
- DurationElementStatesTestForm::buildForm in tests/
modules/ duration_field_form_test/ src/ Form/ DurationElementStatesTestForm.php - Form constructor.
- DurationFormElementTest::buildForm in tests/
src/ Kernel/ DurationFormElementTest.php - Form constructor.
- DurationWidget::formElement in src/
Plugin/ Field/ FieldWidget/ DurationWidget.php - Returns the form for a single field widget.
File
- src/
Element/ DurationElement.php, line 41
Namespace
Drupal\duration_field\ElementView source
class DurationElement extends FormElement {
use ProcessStatesTrait;
/**
* {@inheritdoc}
*/
public function getInfo() {
$class = get_class($this);
return [
'#input' => TRUE,
'#tree' => TRUE,
'#date_increment' => '1',
'#granularity' => 'y:m:d:h:i:s',
'#required_elements' => '',
'#element_validate' => [
[
$class,
'validateElement',
],
],
'#pre_render' => [
[
$class,
'preRenderElement',
],
],
'#process' => [
'Drupal\\Core\\Render\\Element\\RenderElement::processAjaxForm',
[
$class,
'processElement',
],
],
'#theme_wrappers' => [
'form_element',
],
];
}
/**
* {@inheritdoc}
*/
public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
if ($input !== FALSE && !is_null($input)) {
return \Drupal::service('duration_field.service')
->convertDateArrayToDateInterval($input);
}
}
/**
* {@inheritdoc}
*/
public static function processElement(&$element, FormStateInterface $form_state, &$complete_form) {
$duration_service = \Drupal::service('duration_field.service');
$granularity_service = \Drupal::service('duration_field.granularity.service');
$value = FALSE;
if (isset($element['#value']) && $element['#value']) {
$value = $element['#value'];
}
elseif (isset($element['#default_value']) && $element['#default_value']) {
$value = $element['#default_value'];
}
if (is_string($value) && !$duration_service
->checkDurationInvalid($value)) {
$value = new DateInterval($value);
}
if (!$value) {
$value = $duration_service
->createEmptyDateInterval();
}
// Create a wrapper for the elements. This is done in this manner
// rather than nesting the elements in a wrapper with #prefix and #suffix
// so as to not end up with [wrapper] as part of the name attribute
// of the elements.
$div = '<div';
$classes = [
'duration-inner-wrapper',
];
if (!empty($element['#states'])) {
self::processStates($element);
}
foreach ($element['#attributes'] as $attribute => $attribute_value) {
if (is_string($attribute_value)) {
$div .= ' ' . $attribute . "='" . $attribute_value . "'";
}
elseif ($attribute == 'class') {
$classes = array_merge($classes, $attribute_value);
}
}
$div .= ' class="' . implode(' ', $classes) . '"';
$div .= '>';
// For reasons that unfortunately I don't remember at this time, all
// elements of $element need to be at the same level, and cannot be nested.
// as such, the opening div wrapper is created as #markup, with the closing
// wrapper to come after the content.
$element['wrapper_open'] = [
'#markup' => $div,
'#weight' => -1,
];
$time_mappings = [
'y' => t('Years'),
'm' => t('Months'),
'd' => t('Days'),
'h' => t('Hours'),
'i' => t('Minutes'),
's' => t('Seconds'),
];
foreach ($time_mappings as $key => $label) {
// Only include elements that are part of the given granularity.
if ($granularity_service
->includeGranularityElement($key, $element['#granularity'])) {
$element[$key] = [
'#id' => $element['#id'] . '-' . $key,
'#type' => 'number',
'#title' => $label,
// $value is a DateInterval object. This outputs the numeric value for
// the key.
'#default_value' => $value
->format('%' . $key),
// Only require elements that are part of the given require elements
// granularity.
'#required' => $granularity_service
->includeGranularityElement($key, $element['#required_elements']),
'#weight' => 0,
'#min' => 0,
];
// Apply the ajax of the main duration element to each granularity
// input.
if (!empty($element['#ajax'])) {
$element[$info['key']]['#ajax'] = $element['#ajax'];
}
}
}
if (!empty($element['#date_increment']) && (int) $element['#date_increment'] > 0) {
if ($element['#date_increment'] < 60) {
if (!empty($element['s'])) {
$element['s']['#step'] = $element['#date_increment'];
}
}
elseif ($element['#date_increment'] < 3600) {
if (!empty($element['i'])) {
$element['i']['#step'] = (int) ($element['#date_increment'] / 60);
}
}
elseif ($element['#date_increment'] < 86400) {
if (!empty($element['h'])) {
$element['h']['#step'] = (int) ($element['#date_increment'] / 3600);
}
}
else {
if (!empty($element['d'])) {
$element['d']['#step'] = (int) ($element['#date_increment'] / 86400);
}
}
}
// The closing wrapper. See notes on the opening wrapper.
$element['wrapper_close'] = [
'#markup' => '</div>',
'#weight' => 1,
];
// Attach the CSS for the display of the output.
$element['#attached']['library'][] = 'duration_field/element';
return $element;
}
/**
* {@inheritdoc}
*/
public static function preRenderElement(array $element) {
// Set the wrapper as a container to the inner values.
$element['#attributes']['type'] = 'container';
Element::setAttributes($element, [
'id',
'name',
'value',
]);
static::setAttributes($element, [
'form-duration',
]);
return $element;
}
/**
* {@inheritdoc}
*
* Converts the date array to a PHP DateInterval object, and sets the object
* as the value of the form element. All handlers after this point will
* receive the PHP DateInterval element as the value for this form element.
*/
public static function validateElement(&$element, FormStateInterface $form_state, $form) {
$date_array = $form_state
->getValue($element['#parents']);
$form_state
->setValueForElement($element, \Drupal::service('duration_field.service')
->convertDateArrayToDateInterval($date_array));
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
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 | |
DurationElement:: |
public | function |
Returns the element properties for this element. Overrides ElementInterface:: |
|
DurationElement:: |
public static | function | ||
DurationElement:: |
public static | function | ||
DurationElement:: |
public static | function | Converts the date array to a PHP DateInterval object, and sets the object as the value of the form element. All handlers after this point will receive the PHP DateInterval element as the value for this form element. | |
DurationElement:: |
public static | function |
Determines how user input is mapped to an element's #value property. Overrides FormElement:: |
|
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 |
ProcessStatesTrait:: |
public static | function | Processes a form element for #states support. | |
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. |