You are here

function range_field_settings in Range 6

Implementation of hook_field_settings().

File

./range.module, line 81
Defines numeric fields within a range of possible values and displays them colored accordingly..

Code

function range_field_settings($op, $field) {
  switch ($op) {
    case 'form':
      $form = array();
      $form['min'] = array(
        '#type' => 'textfield',
        '#title' => t('Minimum'),
        '#element_validate' => array(
          '_range_widget_settings_min_validate',
        ),
        '#default_value' => $field['min'],
        '#required' => TRUE,
        '#description' => t('Define the minimum value below which this field will be shown out of range. You can use < to indicate that there is no minimum limit.'),
      );
      $form['max'] = array(
        '#type' => 'textfield',
        '#title' => t('Maximum'),
        '#element_validate' => array(
          '_range_widget_settings_max_validate',
        ),
        '#default_value' => $field['max'],
        '#required' => TRUE,
        '#description' => t('Define the maximum value above which this field will be shown out of range. You can use > to indicate that there is no maximum limit.'),
      );
      if ($field['type'] == 'range_decimal') {
        $form['precision'] = array(
          '#type' => 'select',
          '#options' => drupal_map_assoc(range(10, 32)),
          '#title' => t('Precision'),
          '#description' => t('The total number of digits to store in the database, including those to the right of the decimal.'),
          '#default_value' => is_numeric($field['precision']) ? $field['precision'] : 10,
        );
        $form['scale'] = array(
          '#type' => 'select',
          '#options' => drupal_map_assoc(range(0, 2)),
          '#title' => t('Scale'),
          '#description' => t('The number of digits to the right of the decimal.'),
          '#default_value' => is_numeric($field['scale']) ? $field['scale'] : 2,
        );
        $form['decimal'] = array(
          '#type' => 'select',
          '#options' => array(
            '.' => 'decimal point',
            ',' => 'comma',
            ' ' => 'space',
          ),
          '#title' => t('Decimal marker'),
          '#description' => t('The character users will input to mark the decimal point in forms.'),
          '#default_value' => !empty($field['decimal']) ? $field['decimal'] : '.',
        );
      }

      /*
      $form['append']['prefix'] = array(
        '#type' => 'textfield',
        '#title' => t('Prefix'),
        '#size' => 60,
        '#default_value' => !empty($field['prefix']) ? $field['prefix'] : '',
        '#description' => t('Define a string that should be prefixed to the value, like $ or €. Leave blank for none. Separate singular and plural values with a pipe (pound|pounds).'),
      );
      */
      $form['append']['suffix'] = array(
        '#type' => 'textfield',
        '#title' => t('Suffix'),
        '#size' => 60,
        '#default_value' => !empty($field['suffix']) ? $field['suffix'] : '',
        '#description' => t('Define a string that should suffixed to the value, like m², m/s², kb/s. Leave blank for none. Separate singular and plural values with a pipe (pound|pounds).'),
      );
      $form['allowed_values_fieldset'] = array(
        '#type' => 'fieldset',
        '#title' => t('Allowed values'),
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
      );
      $form['allowed_values_fieldset']['allowed_values'] = array(
        '#type' => 'textarea',
        '#title' => t('Allowed values list'),
        '#default_value' => !empty($field['allowed_values']) ? $field['allowed_values'] : '',
        '#required' => FALSE,
        '#rows' => 10,
        '#description' => t('The possible values this field can contain. Enter one value per line, in the format key|label. The key is the value that will be stored in the database, and it must match the field storage type (%type). The label is optional, and the key will be used as the label if no label is specified.<br />Allowed HTML tags: @tags', array(
          '%type' => $field['type'],
          '@tags' => _content_filter_xss_display_allowed_tags(),
        )),
      );
      $form['allowed_values_fieldset']['advanced_options'] = array(
        '#type' => 'fieldset',
        '#title' => t('PHP code'),
        '#collapsible' => TRUE,
        '#collapsed' => empty($field['allowed_values_php']),
      );
      if (user_access('Use PHP input for field settings (dangerous - grant with care)')) {
        $form['allowed_values_fieldset']['advanced_options']['allowed_values_php'] = array(
          '#type' => 'textarea',
          '#title' => t('Code'),
          '#default_value' => !empty($field['allowed_values_php']) ? $field['allowed_values_php'] : '',
          '#rows' => 6,
          '#description' => t('Advanced usage only: PHP code that returns a keyed array of allowed values. Should not include &lt;?php ?&gt; delimiters. If this field is filled out, the array returned by this code will override the allowed values list above.'),
        );
      }
      else {
        $form['allowed_values_fieldset']['advanced_options']['markup_allowed_values_php'] = array(
          '#type' => 'item',
          '#title' => t('Code'),
          '#value' => !empty($field['allowed_values_php']) ? '<code>' . check_plain($field['allowed_values_php']) . '</code>' : t('&lt;none&gt;'),
          '#description' => empty($field['allowed_values_php']) ? t("You're not allowed to input PHP code.") : t('This PHP code was set by an administrator and will override the allowed values list above.'),
        );
      }
      return $form;
    case 'save':
      $values = array(
        'prefix',
        'suffix',
        'min',
        'max',
        'allowed_values',
        'allowed_values_php',
      );
      if ($field['type'] == 'range_decimal') {
        $values = array_merge($values, array(
          'precision',
          'scale',
          'decimal',
        ));
      }
      return $values;
    case 'database columns':
      if ($field['type'] == 'range_integer') {
        return array(
          'value' => array(
            'type' => 'int',
            'not null' => FALSE,
            'sortable' => TRUE,
          ),
        );
      }
      if ($field['type'] == 'range_float') {
        return array(
          'value' => array(
            'type' => 'float',
            'not null' => FALSE,
            'sortable' => TRUE,
          ),
        );
      }
      if ($field['type'] == 'range_decimal') {
        $precision = isset($field['precision']) ? $field['precision'] : 10;
        $scale = isset($field['scale']) ? $field['scale'] : 2;
        return array(
          'value' => array(
            'type' => 'numeric',
            'precision' => $precision,
            'scale' => $scale,
            'not null' => FALSE,
            'sortable' => TRUE,
          ),
        );
      }
    case 'views data':
      $allowed_values = content_allowed_values($field);
      if (count($allowed_values)) {
        $data = content_views_field_views_data($field);
        $db_info = content_database_info($field);
        $table_alias = content_views_tablename($field);

        // Swap the filter handler to the 'in' operator.
        $data[$table_alias][$field['field_name'] . '_value']['filter']['handler'] = 'content_handler_filter_many_to_one';
        $data[$table_alias][$field['field_name'] . '_value']['filter']['numeric'] = TRUE;
        $data[$table_alias][$field['field_name'] . '_value']['argument']['handler'] = 'content_handler_argument_many_to_one';
        $data[$table_alias][$field['field_name'] . '_value']['argument']['numeric'] = TRUE;
        return $data;
      }
      break;
  }
}