You are here

public function IngredientUnitUtility::getConfiguredUnits in Recipe 8.2

Returns an array of units from configuration.

Parameters

string[] $sets_to_get: An array of set id strings.

Return value

string[] An array of units.

File

modules/ingredient/src/Utility/IngredientUnitUtility.php, line 38

Class

IngredientUnitUtility
Provides the ingredient.unit service.

Namespace

Drupal\ingredient\Utility

Code

public function getConfiguredUnits(array $sets_to_get = []) {

  // The field settings will set disabled set values to 0 in configuration.
  // Filter out any disabled values.  This prevents custom unit sets with an
  // ID of '0' and another solution may have to be found eventually.
  $sets_to_get = array_filter($sets_to_get);
  $unit_sets = $this->ingredientUnitConfig
    ->get('unit_sets');
  $units = [];
  foreach ($unit_sets as $set_id => $set) {

    // Verify that the set contains an array of units.
    if (empty($set['units']) || !is_array($set['units'])) {
      continue;
    }

    // Skip the set if it's not in the $sets_to_get.
    if (!empty($sets_to_get) && is_array($sets_to_get) && !in_array($set_id, $sets_to_get)) {
      continue;
    }
    $units = array_merge($units, $set['units']);
  }
  return $units;
}