You are here

function mvf_field_settings in Measured Value Field 6

Implementation of hook_field_settings().

File

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

Code

function mvf_field_settings($op, $field) {
  switch ($op) {
    case 'form':
      $form = array();
      $minmax_fields = array(
        'min' => array(
          '#title' => t('Minimum'),
          '#description' => t('Use this option to define the minimum value that can be accepted for this field. Leave blank for no explicit limitation.'),
        ),
        'max' => array(
          '#title' => t('Maximum'),
          '#description' => t('Use this option to define the maximum value that can be accepted for this field. Leave blank for no explicit limitation.'),
        ),
      );
      $form['value_limits'] = array(
        '#type' => 'fieldset',
        '#title' => t('Measured Values limits'),
        '#collapsed' => FALSE,
        '#collapsible' => FALSE,
        '#description' => t('Choose value limits, that will apply both to "From" and "To" values.'),
      );
      foreach ($minmax_fields as $name => $info) {
        $default_attributes = array(
          'class' => 'formatted-number',
        );
        $default_value = isset($field[$name]) ? parse_formatted_number($field[$name]) : '';
        if (!is_numeric($default_value)) {
          $default_value = '';
        }
        else {

          // Initialize default value with as many decimal digits as necessary.
          $decimals = strpos($default_value, '.') !== FALSE ? drupal_strlen(preg_replace('#^.*\\.(.*)$#', '\\1', $default_value)) : 0;
          $default_value = format_number($default_value, $decimals);
        }
        $form['value_limits'][$name] = array(
          '#type' => 'textfield',
          '#title' => $info['#title'],
          '#size' => 32,
          '#maxlength' => 30,
          '#default_value' => $default_value,
          '#attributes' => $default_attributes,
          '#description' => $info['#description'],
        );
      }
      $form['precision'] = array(
        '#type' => 'select',
        '#title' => t('Precision'),
        '#options' => drupal_map_assoc(range(1, 20)),
        '#default_value' => is_numeric($field['precision']) && (int) $field['precision'] > 0 ? $field['precision'] : 10,
        '#description' => t('The total number of digits to store in the database, including digits to the right of the decimal point.'),
      );
      $form['decimals'] = array(
        '#type' => 'select',
        '#title' => t('Decimals'),
        '#options' => drupal_map_assoc(range(0, 4)),
        '#default_value' => is_numeric($field['decimals']) && (int) $field['decimals'] >= 0 ? $field['decimals'] : 2,
        '#description' => t('The number of digits to the right of the decimal point.'),
      );
      $description = t("Display a matching second value field as a 'To' value. If marked 'Optional' field will be presented but not required. If marked 'Required' the 'To' value will be required if the 'From' value is required or filled in.");
      $description .= '<p class="error">' . t('Changing the %name setting after data has been created could result in the loss of data!', array(
        '%name' => 'To value',
      )) . '</p>';
      $form['input']['tovalue'] = array(
        '#type' => 'radios',
        '#title' => t('"To" Value'),
        '#options' => array(
          '' => t('Disabled'),
          'optional' => t('Optional'),
          'required' => t('Required'),
        ),
        '#description' => $description,
        '#default_value' => isset($field['tovalue']) ? $field['tovalue'] : '',
      );
      formatted_number_add_js();
      return $form;
    case 'save':
      return array(
        'precision',
        'decimals',
        'tovalue',
        'min',
        'max',
      );
    case 'database columns':
      $precision = isset($field['precision']) ? $field['precision'] : 10;
      $decimals = isset($field['decimals']) ? $field['decimals'] : 2;
      $db_columns = array(
        'value' => array(
          'type' => 'numeric',
          'precision' => $precision,
          'scale' => $decimals,
          'not null' => FALSE,
          'sortable' => TRUE,
          'views' => TRUE,
        ),
        'unit' => array(
          'type' => 'varchar',
          'length' => 64,
          'not null' => FALSE,
          'sortable' => TRUE,
          'views' => TRUE,
        ),
      );
      if (!empty($field['tovalue'])) {
        $db_columns['value2'] = $db_columns['value'];

        // We don't want CCK to create additional columns, just the first.
        // We modify them our own way in views data.
        $db_columns['value2']['views'] = FALSE;
      }
      return $db_columns;
    case 'views data':
      $data = content_views_field_views_data($field);
      $db_info = content_database_info($field);
      $table_alias = content_views_tablename($field);

      // Swap in the CCK filter handler with custom one.
      $data[$table_alias][$field['field_name'] . '_value']['filter']['handler'] = 'mvf_filter_handler';

      // Add in another set of fields for the "To" value.
      if (!empty($field['tovalue'])) {
        $data[$table_alias][$field['field_name'] . '_value']['field']['title'] = $data[$table_alias][$field['field_name'] . '_value']['title'];
        $data[$table_alias][$field['field_name'] . '_value']['field']['title short'] = $data[$table_alias][$field['field_name'] . '_value']['title short'];
        $data[$table_alias][$field['field_name'] . '_value2'] = $data[$table_alias][$field['field_name'] . '_value'];
        $data[$table_alias][$field['field_name'] . '_value']['title'] .= ' - ' . t('"From" value');
        $data[$table_alias][$field['field_name'] . '_value']['title short'] .= ' - ' . t('"From" value');
        $data[$table_alias][$field['field_name'] . '_value']['field']['title'] .= ' - ' . t('"From" value');
        $data[$table_alias][$field['field_name'] . '_value']['field']['title short'] .= ' - ' . t('"From" value');
        $data[$table_alias][$field['field_name'] . '_value2']['title'] .= ' - ' . t('"To" value');
        $data[$table_alias][$field['field_name'] . '_value2']['title short'] .= ' - ' . t('"To" value');
        $data[$table_alias][$field['field_name'] . '_value2']['field']['title'] .= ' - ' . t('"To" value');
        $data[$table_alias][$field['field_name'] . '_value2']['field']['title short'] .= ' - ' . t('"To" value');
        $data[$table_alias][$field['field_name'] . '_value2']['field']['field'] .= '2';
        $data[$table_alias][$field['field_name'] . '_value2']['sort']['field'] .= '2';
      }
      return $data;
  }
}