You are here

public function GranularityService::convertGranularityArrayToGranularityString in Duration Field 3.0.x

Same name and namespace in other branches
  1. 8.2 src/Service/GranularityService.php \Drupal\duration_field\Service\GranularityService::convertGranularityArrayToGranularityString()

Converts a granularity array to a granularity string.

Parameters

array $granularityArray: An array containing the following keys. The value of each key will be evaulated to TRUE or FALSE.

  • y (years)
  • m (months)
  • d (days)
  • h (hours)
  • i (minutes)
  • s (seconds)

TRUE values mean the element should be included as part of the granularity.

Return value

string The array converted to a granularity string.

Overrides GranularityServiceInterface::convertGranularityArrayToGranularityString

File

src/Service/GranularityService.php, line 13

Class

GranularityService

Namespace

Drupal\duration_field\Service

Code

public function convertGranularityArrayToGranularityString(array $granularityArray) {
  $granularities = [];

  // Loop through each of the submitted values.
  foreach (array_keys($granularityArray) as $granularity) {

    // Check if the submitted value evaluates to TRUE.
    if ($granularityArray[$granularity]) {

      // Add the granularity to the granularities to be rendered.
      $granularities[] = $granularity;
    }
  }

  // Build and return the granularity string.
  return implode(':', $granularities);
}