View source
<?php
namespace Drupal\duration_field\Element;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Render\Element\FormElement;
use Drupal\duration_field\Service\DurationService;
class Duration extends FormElement {
public function getInfo() {
$class = get_class($this);
return [
'#input' => TRUE,
'#tree' => TRUE,
'#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 DurationService::convertValue($input);
}
return NULL;
}
public static function processElement(&$element, FormStateInterface $form_state, &$complete_form) {
$granularity = explode(':', $element['#granularity']);
$required_elements = explode(':', $element['#required_elements']);
$value = FALSE;
if (isset($element['#value']) && $element['#value']) {
$value = new \DateInterval($element['#value']);
}
$time_mappings = [
'y' => [
'label' => t('Years'),
'key' => 'year',
],
'm' => [
'label' => t('Months'),
'key' => 'month',
'format' => 'm',
],
'd' => [
'label' => t('Days'),
'key' => 'day',
],
'h' => [
'label' => t('Hours'),
'key' => 'hour',
],
'i' => [
'label' => t('Minutes'),
'key' => 'minute',
],
's' => [
'label' => t('Seconds'),
'key' => 'second',
],
];
$div = '<div';
$classes = [
'duration-inner-wrapper',
];
if (!empty($element['#states'])) {
drupal_process_states($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,
];
foreach ($time_mappings as $key => $info) {
if (preg_grep('/' . $key . '/i', $granularity)) {
$element[$info['key']] = [
'#id' => $element['#id'] . '-' . $info['key'],
'#type' => 'number',
'#title' => $info['label'],
'#value' => $value ? $value
->format('%' . $key) : 0,
'#required' => preg_grep('/' . $key . '/i', $required_elements) ? TRUE : FALSE,
'#weight' => 0,
'#min' => 0,
];
if (!empty($element['#ajax'])) {
$element[$info['key']]['#ajax'] = $element['#ajax'];
}
}
}
$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) {
$mappings = [
'year' => 'Y',
'month' => 'M',
'day' => 'D',
'hour' => 'H',
'minute' => 'M',
'second' => 'S',
];
$values = [];
foreach ($mappings as $key => $duration_key) {
if (isset($element[$key])) {
$values[$key] = $element[$key]['#value'];
}
}
$form_state
->setValue($element['#parents'], DurationService::convertValue($values));
}
}