optionwidgets.module 6.87 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<?php
// $Id$

/**
 * @file
 * Defines selection, check box and radio button widgets for text and numeric fields.
 */

/**
 * Implementation of hook_help().
 */
function optionwidgets_help($section) {
  switch ($section) {
    case 'admin/modules#description':
15
      return t('<strong>CCK:</strong> Defines selection, check box and radio button widgets for text and numeric fields. <em>Note: Requires content.module, text.module and number.module.</em>');
16 17 18 19 20 21 22
  }
}

/**
 * Implementation of hook_widget_info().
 */
function optionwidgets_widget_info() {
23

24
  $option_types = array('text', 'number_integer', 'number_decimal');
25 26 27
  return array(
    'options_select' => array(
      'label' => 'Select list',
28
      'field types' => $option_types,
29 30 31
    ),
    'options_buttons' => array(
      'label' => 'Check boxes/radio buttons',
32
      'field types' => $option_types,
33
    ),
34 35 36 37
    'options_onoff' => array(
      'label' => 'Single on/off checkbox',
      'field types' => $option_types,
    ),
38 39 40
  );
}

41 42 43 44 45 46 47
/**
 * Implementation of hook_widget_settings().
 */
function optionwidgets_widget_settings($op, $widget) {
  $form = array();
  switch ($op) {
    case 'form':
48 49 50 51 52 53 54
      $form['#prefix'] = t('Create a list of options as a list in <strong>Allowed values</strong> or as an array in Php code at the bottom of this page. These values will be the same for the %field in all content types. ', array('%field' => $widget['label']));
      if ($widget['type'] == 'options_onoff') {
        $form['#prefix'] .= t(' For a single on/off checkbox, define the \'off\' value first, then the \'on\' value in the <strong>Allowed values</strong> section.');
      }
      else {
        $form['#prefix'] .= t(' The Check boxes/radio buttons widget will display checkboxes if the multiple values option is selected for this field, otherwise radios will be displayed.');
      }
55 56 57 58
  }
  return $form;
}

59 60 61
/**
 * Implementation of hook_widget().
 */
62
function optionwidgets_widget($op, &$node, $field, &$items) {
63
  switch ($op) {
64
    case 'prepare form values':
65
      $options = _optionwidgets_options($field);
66 67
      $items_transposed = content_transpose_array_rows_cols($items);
      $values = (isset($items_transposed['value']) && is_array($items_transposed['value'])) ? $items_transposed['value'] : array();
68

69 70
      $keys = array();
      foreach ($values as $value) {
71 72 73
        $key = array_search($value, array_keys($options));
        if (isset($key)) {
          $keys[] = $value;
74 75
        }
      }
76
      if ($field['multiple'] || $field['widget']['type'] == 'options_onoff') {
77
        $items['default keys'] = $keys;
78 79
      }
      else {
80
        $items['default key'] = reset($keys);
81 82 83 84 85
      }
      break;

    case 'form':
      $options = _optionwidgets_options($field);
86

87
      $form = array();
88

89
      $form[$field['field_name']] = array('#tree' => TRUE);
90

91 92
      switch ($field['widget']['type']) {
        case 'options_select':
93
          if (!$field['required']) $options = array('' => theme('optionwidgets_none', $field['widget']['type'], $field['field_name'], $node->type)) + $options;
94 95 96 97
          if ($field['multiple']) {
            $form[$field['field_name']]['keys'] = array(
              '#type' => 'select',
              '#title' => t($field['widget']['label']),
98
              '#default_value' => $items['default keys'],
99
              '#multiple' => TRUE,
100
              '#size' => min(count($options), 6),
101 102
              '#options' => $options,
              '#required' => $field['required'],
103
              '#description' => t($field['widget']['description']),
104 105 106 107 108 109
            );
          }
          else {
            $form[$field['field_name']]['key'] = array(
              '#type' => 'select',
              '#title' => t($field['widget']['label']),
110
              '#default_value' => $items['default key'],
111 112 113
              '#multiple' => FALSE,
              '#options' => $options,
              '#required' => $field['required'],
114
              '#description' => t($field['widget']['description']),
115 116
            );
          }
117 118
          break;

119 120 121 122 123 124 125 126 127
        case 'options_onoff':
          // Display only the 'On' value of $options;
          $vals = array_keys($options);
          $on_value = $vals[1];
          $form[$field['field_name']]['keys'] = array(
            '#type' => 'checkbox',
            '#title' => $options[$on_value],
            '#default_value' => $items['default keys'][0],
            '#return_value' => $on_value,
128
            '#description' => t($field['widget']['description']),
129 130 131 132
            '#required' => FALSE,
          );
          break;

133 134
        case 'options_buttons':
          if ($field['multiple']) {
135
            $form[$field['field_name']]['keys'] = array(
136 137
              '#type' => 'checkboxes',
              '#title' => t($field['widget']['label']),
138
              '#default_value' => $items['default keys'],
139 140
              '#options' => $options,
              '#required' => $field['required'],
141
              '#description' => t($field['widget']['description']),
142 143 144
            );
          }
          else {
145
            if (!$field['required']) $options = array('' => theme('optionwidgets_none', $field['widget']['type'], $field['field_name'], $node->type)) + $options;
146
            $form[$field['field_name']]['key'] = array(
147 148
              '#type' => 'radios',
              '#title' => t($field['widget']['label']),
149
              '#default_value' => $items['default key'],
150 151
              '#options' => $options,
              '#required' => $field['required'],
152
              '#description' => t($field['widget']['description']),
153 154 155 156 157 158
            );
          }
          break;
      }
      return $form;

159 160 161
    case 'process form values':
      $options = _optionwidgets_options($field);

162 163
      if ($field['multiple'] || $field['widget']['type'] == 'options_onoff') {
        $keys = (array) $items['keys'];
164 165
      }
      else {
166
        $keys = array($items['key']);
167
      }
168

169 170 171
      $values = array();
      foreach ($keys as $key) {
        if (isset($options[$key])) {
172
          $values[] = $key;
173 174
        }
      }
175

176 177 178 179 180
      if ($field['widget']['type'] == 'options_onoff' && empty($values)) {
        $keys = array_keys($options);
        $values[] = $keys[0];
      }

181
      $items = content_transpose_array_rows_cols(array('value' => $values));
182

183
      // Remove the widget's data representation so it isn't saved.
184 185
      unset($items['keys']);
      unset($items['key']);
186 187 188 189 190
      break;
  }
}

function _optionwidgets_options($field) {
191

192 193 194 195 196 197 198 199 200 201
  $types = _content_field_types();
  $field_allowed_values = $types[$field['type']]['module'] .'_allowed_values';
  if (function_exists($field_allowed_values)) {
    return $field_allowed_values($field);
  }
  else {
    return array();
  }

}
202

203 204 205 206 207 208 209 210 211 212
/**
 *  Theme the label for the empty value for options that are not required.
 *  The default theme will display N/A for a radio list and blank for a select.
 */
function theme_optionwidgets_none($widget_type, $field_name, $node_type) {
  switch ($widget_type) {
  case 'options_buttons':
    return t('N/A');
  default :
    return '';
213
  }
214
}