You are here

function mvf_widget_settings in Measured Value Field 6

Implementation of hook_widget_settings().

File

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

Code

function mvf_widget_settings($op, $widget) {
  switch ($op) {
    case 'form':
      $form = array();
      $options = array(
        'short' => t('Short name'),
        'full' => t('Full name'),
      );
      $form['unit_select_mode'] = array(
        '#type' => 'radios',
        '#title' => t('Unit selection mode'),
        '#options' => $options,
        '#default_value' => isset($widget['unit_select_mode']) && isset($options[$widget['unit_select_mode']]) ? $widget['unit_select_mode'] : 'short',
        '#required' => TRUE,
        '#description' => t('Choose the format of the label that will be displayed for options of the units select list.'),
      );
      $options_single = module_invoke_all('mvf_get_display_modes_single');
      $options_range = module_invoke_all('mvf_get_display_modes_range');
      $form['unit_display'] = array(
        '#type' => 'fieldset',
        '#title' => t('MVF display mode'),
        '#collapsed' => FALSE,
        '#collapsible' => FALSE,
        '#description' => t('Choose the format that will be used to display this Measured Value field when a node is rendered.'),
      );
      $form['unit_display']['unit_display_mode_single'] = array(
        '#type' => 'select',
        '#title' => t('Single value display mode'),
        '#options' => $options_single,
        '#default_value' => isset($widget['unit_display_mode_single']) && isset($options_single[$widget['unit_display_mode_single']]) ? $widget['unit_display_mode_single'] : 'f|+|u',
        '#required' => TRUE,
        '#description' => t('Single value display mode will be used when "To" value is disabled or enabled but not filled in.'),
      );
      $form['unit_display']['unit_display_mode_range'] = array(
        '#type' => 'select',
        '#title' => t('Range display mode'),
        '#options' => $options_range,
        '#default_value' => isset($widget['unit_display_mode_range']) && isset($options_range[$widget['unit_display_mode_range']]) ? $widget['unit_display_mode_range'] : 'f|-|t|+|u',
        '#required' => TRUE,
        '#description' => t('Range display mode will be used when both "From" and "To" values are filled in.'),
      );
      $options = array(
        'field' => t('Field precision'),
        'unit' => t('Unit precision'),
      );
      $form['decimals_display_mode'] = array(
        '#type' => 'radios',
        '#title' => t('Decimals display mode'),
        '#options' => $options,
        '#default_value' => isset($widget['decimals_display_mode']) && isset($options[$widget['decimals_display_mode']]) ? $widget['decimals_display_mode'] : 'field',
        '#required' => TRUE,
        '#description' => t('Choose the method to select the number of decimals used to display the field. The standard precision for each unit is displayed in the <em>Available units</em> list.'),
      );
      $unit_options = array();
      foreach (units_get_units() as $id => $unit) {
        if (units_unit_is_enabled($id)) {
          $unit_options[$id] = $unit['fullname'] . ' [' . $unit['decimals'] . ']';
        }
      }
      $description = t('Choose the units that you want to enable for this field. Do not select any unit to enable them all.');
      $description .= '<br/>';
      $description .= t('The number between square brakets indicates the standard precision for each unit.');
      $description .= '<br/><br/>';
      $description .= t('<strong>Hint:</strong> visit !page and setup site-wide list of units you want to work with. This will save you from browsing long lists of units in each MVF configuration.', array(
        '!page' => l('Units configuration page', 'admin/content/units'),
      ));
      $form['units'] = array(
        '#type' => 'fieldset',
        '#title' => t('Available units'),
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
        '#description' => $description,
      );
      if (isset($widget['allowed_units']) && is_array($widget['allowed_units'])) {

        // Get filtered array.
        $allowed_units = array_filter($widget['allowed_units']);

        // If not empty, create array for the form element values.
        if (!empty($allowed_units)) {
          $allowed_units = array_keys($allowed_units);
          $allowed_units = array_combine($allowed_units, $allowed_units);
        }
      }
      else {
        $allowed_units = array();
      }
      $form['units']['allowed_units'] = array(
        '#type' => 'checkboxes',
        '#options' => $unit_options,
        '#default_value' => $allowed_units,
        '#checkall' => TRUE,
        '#prefix' => '<div class="mvf-unit-checkboxes">',
        '#suffix' => '</div>',
      );
      drupal_add_css(drupal_get_path('module', 'mvf') . '/mvf.css');
      return $form;
    case 'save':
      return array(
        'unit_select_mode',
        'unit_display_mode_single',
        'unit_display_mode_range',
        'decimals_display_mode',
        'allowed_units',
      );
  }
}