You are here

public function IngredientUnitUtility::createUnitSelectOptions in Recipe 8.2

Returns options for a unit select form element.

@todo The blank option should be created as the #empty_option in the individual select elements. Unfortunately, the default_unit element in the field settings form uses a #process function to set its options which seems to overwrite the #empty_option. Even setting the #empty_option during the #process step doesn't seem to work. This seems like a possible core bug and will probably have to be fixed there before we can complete this task.

Parameters

array $units: An array of units.

Return value

array An array of unit key/value pairs for use as select form element options.

File

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

Class

IngredientUnitUtility
Provides the ingredient.unit service.

Namespace

Drupal\ingredient\Utility

Code

public function createUnitSelectOptions(array $units = []) {

  // Put in a blank so non-matching units will not validate and save.
  $options = [
    '' => '',
  ];
  foreach ($units as $unit_key => $unit) {
    $text = $unit['name'];
    if (!empty($unit['abbreviation'])) {
      $text .= ' (' . $unit['abbreviation'] . ')';
    }
    $options[$unit_key] = $text;
  }
  return $options;
}