TimeWidget.php in Field Time 1.0.x
File
src/Plugin/Field/FieldWidget/TimeWidget.php
View source
<?php
namespace Drupal\field_time\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
class TimeWidget extends WidgetBase {
public static function defaultSettings() {
return [
'enabled' => FALSE,
'step' => 5,
] + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
return [
'enabled' => [
'#type' => 'checkbox',
'#title' => $this
->t('Add seconds parameter to input widget'),
'#default_value' => $this
->getSetting('enabled'),
],
'step' => [
'#type' => 'textfield',
'#title' => $this
->t('Step to change seconds'),
'#open' => TRUE,
'#default_value' => $this
->getSetting('step'),
'#states' => [
'visible' => [
':input[name$="[enabled]"]' => [
'checked' => TRUE,
],
],
],
],
] + parent::settingsForm($form, $form_state);
}
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$show_seconds = (bool) $this
->getSetting('enabled');
$value = $items[$delta]->value ?? NULL;
if ($show_seconds && strlen($value) === 5) {
$value .= ':00';
}
$additional = [
'#type' => 'time',
'#default_value' => $value,
];
if ($show_seconds) {
$additional['#attributes']['step'] = $this
->getSetting('step');
}
$additional['#show_seconds'] = $show_seconds;
$element['value'] = $element + $additional;
return $element;
}
}
Classes
Name |
Description |
TimeWidget |
Plugin implementation of the 'time_widget' widget. |