You are here

function template_preprocess_hms_natural_language in HMS Field 8

Preprocess theme hms_natural_language.

TODO: Investigate running since options (see theme_hms) Would be cool if we can also make this format a 'Forrest Gump' format.

File

./hms_field.module, line 91
Contains hms_field.module..

Code

function template_preprocess_hms_natural_language(&$variables) {
  $hms_service = \Drupal::service('hms_field.hms');
  $labels = $hms_service
    ->factor_map(TRUE);

  // Assign keyed values array.
  $values = array_combine(explode(':', $variables['format']), explode(':', $hms_service
    ->seconds_to_formatted($variables['value'], $variables['format'], TRUE)));
  $variables['labels'] = $labels;
  $variables['formatted_values'] = array();

  // Build array for output in template.
  foreach ($values as $key => $value) {
    if ($value != 0) {
      $variables['formatted_values'][$key] = \Drupal::translation()
        ->formatPlural($value, '@count ' . $labels[$key]['label single'], '@count ' . $labels[$key]['label multiple']);
    }
  }
}