You are here

function datetimehideseconds_process_element in DateTime hide Seconds 8

Element process callback for datetime fields.

1 string reference to 'datetimehideseconds_process_element'
datetimehideseconds_element_info_alter in ./datetimehideseconds.module
Implements hook_element_info_alter().

File

./datetimehideseconds.module, line 15

Code

function datetimehideseconds_process_element($element) {
  if (!empty($element['#datetimehideseconds']['hide'])) {
    $element['time']['#attributes']['title'] = t('Enter a valid time - e.g. @format', [
      '@format' => (new \DateTime())
        ->format('h:i'),
    ]);
    if ($element['#date_time_element'] !== 'none') {
      $element['#date_time_format'] = 'H:i';
    }
    if (!empty($element['time']['#value'])) {
      $parts = explode(':', $element['time']['#value']);
      $parts = array_splice($parts, 0, 2);
      $element['time']['#value'] = implode(':', $parts);
    }

    // Remove seconds in browsers that support HTML5 type=date.
    $element['time']['#attributes']['step'] = 60;
  }
  return $element;
}