View source
<?php
namespace Drupal\duration_field\Element;
use DateInterval;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Render\Element\FormElement;
class DurationElement extends FormElement {
use ProcessStatesTrait;
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',
],
];
}
public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
if ($input !== FALSE && !is_null($input)) {
return \Drupal::service('duration_field.service')
->convertDateArrayToDateInterval($input);
}
}
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();
}
$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 .= '>';
$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) {
if ($granularity_service
->includeGranularityElement($key, $element['#granularity'])) {
$element[$key] = [
'#id' => $element['#id'] . '-' . $key,
'#type' => 'number',
'#title' => $label,
'#default_value' => $value
->format('%' . $key),
'#required' => $granularity_service
->includeGranularityElement($key, $element['#required_elements']),
'#weight' => 0,
'#min' => 0,
];
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);
}
}
}
$element['wrapper_close'] = [
'#markup' => '</div>',
'#weight' => 1,
];
$element['#attached']['library'][] = 'duration_field/element';
return $element;
}
public static function preRenderElement(array $element) {
$element['#attributes']['type'] = 'container';
Element::setAttributes($element, [
'id',
'name',
'value',
]);
static::setAttributes($element, [
'form-duration',
]);
return $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));
}
}