You are here

public static function TimeElement::preRenderTime in Time Field For Drupal 8.x / 9.x 8

Same name and namespace in other branches
  1. 2.x src/Element/TimeElement.php \Drupal\time_field\Element\TimeElement::preRenderTime()

Prepares a #type 'time' render element for input.html.twig.

Parameters

array $element: An associative array containing the properties of the element. Properties used: #title, #value, #description, #size, #maxlength, #placeholder, #required, #attributes.

Return value

array The $element with prepared variables ready for input.html.twig.

See also

\Drupal\time_field\Plugin\Field\FieldWidget\TimeWidget::formElement()

File

src/Element/TimeElement.php, line 75

Class

TimeElement
Provides a time field form element.

Namespace

Drupal\time_field\Element

Code

public static function preRenderTime(array $element) {
  $element['#attributes']['type'] = 'time';
  $element['#attributes']['class'] = [
    'form-time',
  ];

  // In ajax request value is set to raw timestamp
  // perform a better solution here.
  $isValuePassedInTimestampFormat = preg_match('/^\\d+$/', $element['#value']);
  if ($isValuePassedInTimestampFormat) {
    $element['#value'] = Time::createFromTimestamp($element['#value'])
      ->formatForWidget($element['#show_seconds']);
  }
  Element::setAttributes($element, [
    'id',
    'name',
    'value',
    'size',
    'maxlength',
    'placeholder',
  ]);
  static::setAttributes($element, [
    'form-text',
  ]);
  return $element;
}