You are here

function mvf_get_widget_units in Measured Value Field 6

Build unit options for the given field/widget.

1 call to mvf_get_widget_units()
mvf_widget_process in ./mvf.module
Process an individual MVF element.

File

./mvf.module, line 600
Measured Value Field module.

Code

function mvf_get_widget_units($field) {

  // Currently implemented modes: short, full. See mvf_widget_settings().
  $mode = $field['widget']['unit_select_mode'];

  // Prepare the array of allowed units.
  if (isset($field['widget']['allowed_units']) && is_array($field['widget']['allowed_units'])) {

    // Obtain the list of allowed units. Note that this array is in the form of 'unit_id' => boolean.
    $allowed_units = array_filter($field['widget']['allowed_units']);
  }
  else {

    // Initialize array when the list has not been already set in field settings.
    $allowed_units = array();
  }

  // Depending on mode, unit array is built in the form "id" => "shortname",
  // or "id" => "fullname".
  switch ($mode) {
    case 'full':
      $site_units = units_get_unit_names(1);
      break;

    // Default is 'short' mode.
    default:
      $site_units = units_get_unit_names();
  }

  // Reduce units array to globally enabled units.
  // Units get enabled or disabled at Units module UI ("admin/content/units").
  // Empty list of enabled units returned means "all units are enabled".
  $enabled_list = units_get_enabled_units();
  if (empty($enabled_list)) {
    $site_enabled_units = $site_units;
  }
  else {
    $enabled_list = array_combine($enabled_list, $enabled_list);
    $site_enabled_units = array_intersect_key($site_units, $enabled_list);
  }

  // When no unit has been specified in widget settings we allow them all.
  $allowed_units = empty($allowed_units) ? $site_enabled_units : array_intersect_key($site_enabled_units, $allowed_units);

  // When field is not required, an additional empty unit is pushed on top of the resulting list.
  if (!$field['required']) {
    $allowed_units = array(
      '' => $mode == 'short' ? '---' : t('-- Select unit --'),
    ) + $allowed_units;
  }
  return $allowed_units;
}