You are here

protected function DurationService::getTimePeriod in Duration Field 8.2

Same name and namespace in other branches
  1. 3.0.x src/Service/DurationService.php \Drupal\duration_field\Service\DurationService::getTimePeriod()

Returns a human-friendly value for a given time period key.

Parameters

string $type: The type of the human-readable value to retrieve.

int $value: The amount for that time period.

string $textLength: The length of text to use. Allowed values are 'full' and 'short'.

Return value

\Drupal\Core\StringTranslation\TranslatableMarkup The translated text value of the given type.

1 call to DurationService::getTimePeriod()
DurationService::getHumanReadableStringFromDateInterval in src/Service/DurationService.php
Get a human-readable string representing a DateTime interval.

File

src/Service/DurationService.php, line 181

Class

DurationService
Provides services for the Duration Field module.

Namespace

Drupal\duration_field\Service

Code

protected function getTimePeriod($type, $value, $textLength) {
  if ($type == 'year') {
    if ($textLength == 'full') {
      return $this
        ->formatPlural($value, '1 year', '@count years');
    }
    elseif ($textLength == 'short') {
      return $this
        ->formatPlural($value, '1 yr', '@count yr');
    }
  }
  elseif ($type == 'month') {
    if ($textLength == 'full') {
      return $this
        ->formatPlural($value, '1 month', '@count months');
    }
    elseif ($textLength == 'short') {
      return $this
        ->formatPlural($value, '1 mo', '@count mo');
    }
  }
  elseif ($type == 'day') {
    return $this
      ->formatPlural($value, '1 day', '@count days');
  }
  elseif ($type == 'hour') {
    if ($textLength == 'full') {
      return $this
        ->formatPlural($value, '1 hour', '@count hours');
    }
    elseif ($textLength == 'short') {
      return $this
        ->formatPlural($value, '1 hr', '@count hr');
    }
  }
  elseif ($type == 'minute') {
    if ($textLength == 'full') {
      return $this
        ->formatPlural($value, '1 minute', '@count minutes');
    }
    elseif ($textLength == 'short') {
      return $this
        ->formatPlural($value, '1 min', '@count min');
    }
  }
  elseif ($type == 'second') {
    if ($textLength == 'full') {
      return $this
        ->formatPlural($value, '1 second', '@count seconds');
    }
    elseif ($textLength == 'short') {
      return $this
        ->formatPlural($value, '1 s', '@count s');
    }
  }
}