You are here

public function TimeWidget::formElement in Time Field For Drupal 8.x / 9.x 8

Same name and namespace in other branches
  1. 2.x src/Plugin/Field/FieldWidget/TimeWidget.php \Drupal\time_field\Plugin\Field\FieldWidget\TimeWidget::formElement()

Overrides WidgetInterface::formElement

See also

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

File

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

Class

TimeWidget
Plugin implementation of the 'time_widget' widget.

Namespace

Drupal\time_field\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');
  $additional = [
    '#type' => 'time',
    '#default_value' => isset($items[$delta]->value) ? Time::createFromTimestamp($items[$delta]->value)
      ->formatForWidget($show_seconds) : NULL,
  ];

  // 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;
}