You are here

protected function DurationHumanDisplayFormatter::getHumanFriendlyLabel in Duration Field 3.0.x

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldFormatter/DurationHumanDisplayFormatter.php \Drupal\duration_field\Plugin\Field\FieldFormatter\DurationHumanDisplayFormatter::getHumanFriendlyLabel()
  2. 8 src/Plugin/Field/FieldFormatter/DurationHumanDisplayFormatter.php \Drupal\duration_field\Plugin\Field\FieldFormatter\DurationHumanDisplayFormatter::getHumanFriendlyLabel()

Converts a key to a human readable value.

Parameters

string $key: The machine readable name to be converted.

bool $capitalize: Whether or not the return value should be capitalized.

Return value

string The converted value, if a mapping exists, otherwise the original key

2 calls to DurationHumanDisplayFormatter::getHumanFriendlyLabel()
DurationHumanDisplayFormatter::settingsForm in src/Plugin/Field/FieldFormatter/DurationHumanDisplayFormatter.php
Returns a form to configure settings for the formatter.
DurationHumanDisplayFormatter::settingsSummary in src/Plugin/Field/FieldFormatter/DurationHumanDisplayFormatter.php
Returns a short summary for the current formatter settings.

File

src/Plugin/Field/FieldFormatter/DurationHumanDisplayFormatter.php, line 204

Class

DurationHumanDisplayFormatter
Provides a human friendly formatter for the Duration field type.

Namespace

Drupal\duration_field\Plugin\Field\FieldFormatter

Code

protected function getHumanFriendlyLabel($key, $capitalize = TRUE) {
  $custom_labels = $this->moduleHandler
    ->invokeAll('duration_field_labels');
  if (!isset($custom_labels['capitalized'])) {
    $custom_labels['capitalized'] = [];
  }
  if (!isset($custom_labels['lowercase'])) {
    $custom_labels['lowercase'] = [];
  }
  if ($capitalize) {
    $values = [
      'full' => $this
        ->t('Full'),
      'short' => $this
        ->t('Short'),
      'space' => $this
        ->t('Spaces'),
      'hyphen' => $this
        ->t('Hyphens'),
      'comma' => $this
        ->t('Commas'),
      'newline' => $this
        ->t('New lines'),
    ] + $custom_labels['capitalized'];
  }
  else {
    $values = [
      'full' => $this
        ->t('full'),
      'short' => $this
        ->t('short'),
      'space' => $this
        ->t('spaces'),
      'hyphen' => $this
        ->t('hyphens'),
      'comma' => $this
        ->t('commas'),
      'newline' => $this
        ->t('new lines'),
    ] + $custom_labels['lowercase'];
  }
  return isset($values[$key]) ? $values[$key] : $key;
}