You are here

function template_preprocess_hms in HMS Field 8

Preprocess theme hms.

Attach JS when needed.

File

./hms_field.module, line 46
Contains hms_field.module..

Code

function template_preprocess_hms(&$variables) {
  $classes = array(
    'hms',
    str_replace(':', '-', 'hms-format-' . $variables['format']),
  );
  $value = $variables['value'];
  $offset = $variables['offset'];
  $default_value = $variables['default_value'];
  if ($variables['running_since'] !== 0) {
    if (!$offset && !$default_value && $value) {

      // Backwards compatible.
      $offset = $value;
      $default_value = $value;
      $value = 0;
    }
    $value = $default_value;

    // It is not possible to run longer then from 1970-01-01 00:00:01
    $classes[] = 'hms-running';

    // We also need to pass the running since value to JS.
    // When format h is presented, the underlaying value can be at 3599
    // The next second h needs to update.
    // Be sure to pass running_since as time() (== GMT time)
    if ($variables['running_since'] < 0) {
      $variables['running_since'] = REQUEST_TIME;
    }
    $classes[] = 'hms-since-' . $variables['running_since'];
    $classes[] = 'hms-offset-' . $offset;
    $classes[] = 'hms-leading_zero-' . $variables['leading_zero'];
    if ($offset) {
      $value = REQUEST_TIME - $variables['running_since'] + $offset;
    }
    $variables['#attached']['library'][] = 'hms_field/hms_field';
    $variables['#attached']['drupalSettings']['hms_field']['servertime'] = REQUEST_TIME;
    $variables['#attached']['drupalSettings']['hms_field']['factor_map'] = \Drupal::service('hms_field.hms')
      ->factor_map();
  }
  $variables['classes'] = implode(' ', $classes);
  $variables['hms_value_formatted'] = \Drupal::service('hms_field.hms')
    ->seconds_to_formatted($value, $variables['format'], $variables['leading_zero']);
}