You are here

public function TimeWidget::formElement in Field Time 1.0.x

Overrides WidgetInterface::formElement

See also

\Drupal\field_time\Element\TimeElement::preRenderTime()

File

src/Plugin/Field/FieldWidget/TimeWidget.php, line 61

Class

TimeWidget
Plugin implementation of the 'time_widget' widget.

Namespace

Drupal\field_time\Plugin\Field\FieldWidget

Code

public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {

  // Determine if we're showing seconds in the widget.
  $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,
  ];

  // Add the step attribute if we're showing seconds in the widget.
  if ($show_seconds) {
    $additional['#attributes']['step'] = $this
      ->getSetting('step');
  }

  // Set a property to determine the format in TimeElement::preRenderTime().
  $additional['#show_seconds'] = $show_seconds;
  $element['value'] = $element + $additional;
  return $element;
}