You are here

function number_field_settings in Content Construction Kit (CCK) 5

Same name and namespace in other branches
  1. 6.3 modules/number/number.module \number_field_settings()
  2. 6 modules/number/number.module \number_field_settings()
  3. 6.2 modules/number/number.module \number_field_settings()

Implementation of hook_field_settings().

File

./number.module, line 21
Defines numeric field types.

Code

function number_field_settings($op, $field) {
  switch ($op) {
    case 'form':
      $form = array();
      foreach (number_field_formatter_info() as $format => $opt) {
        if (in_array($field['type'], $opt['field types'])) {
          $options[$format] = $opt['label'];
        }
      }
      $form['min'] = array(
        '#type' => 'textfield',
        '#title' => t('Minimum'),
        '#default_value' => isset($field['min']) ? $field['min'] : '',
      );
      $form['max'] = array(
        '#type' => 'textfield',
        '#title' => t('Maximum'),
        '#default_value' => isset($field['max']) ? $field['max'] : '',
      );
      $form['append']['prefix'] = array(
        '#type' => 'textfield',
        '#title' => t('Prefix'),
        '#size' => 60,
        '#default_value' => isset($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' => isset($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'] = array(
        '#type' => 'textarea',
        '#title' => t('Allowed values list'),
        '#default_value' => isset($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['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['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['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 'validate':
      if ($field['min'] && !is_numeric($field['min'])) {
        form_set_error('min', t('"Minimum" must be a number.'));
      }
      if ($field['max'] && !is_numeric($field['max'])) {
        form_set_error('max', t('"Maximum" must be a number.'));
      }
      break;
    case 'save':
      return array(
        'prefix',
        'suffix',
        'append_position',
        'min',
        'max',
        'allowed_values',
        'allowed_values_php',
      );
    case 'database columns':
      if ($field['type'] == 'number_integer') {
        return array(
          'value' => array(
            'type' => 'int',
            'not null' => FALSE,
            'default' => NULL,
            'sortable' => TRUE,
          ),
        );
      }
      if ($field['type'] == 'number_decimal') {
        return array(
          'value' => array(
            'type' => 'float',
            'not null' => FALSE,
            'default' => NULL,
            'sortable' => TRUE,
          ),
        );
      }
    case 'filters':
      $allowed_values = number_allowed_values($field);
      if (count($allowed_values)) {
        return array(
          'default' => array(
            'list' => $allowed_values,
            'list-type' => 'list',
            'operator' => 'views_handler_operator_andor',
            'value-type' => 'array',
          ),
        );
      }
      else {
        return array(
          'default' => array(
            'operator' => 'views_handler_operator_gtlt',
          ),
        );
      }
      break;
  }
}