| Commit | Line | Data |
|---|---|---|
| 2d3f00e8 | 1 | <?php |
| 2d3f00e8 JC |
2 | |
| 3 | /** | |
| 4 | * @file | |
| 5 | * Defines selection, check box and radio button widgets for text and numeric fields. | |
| 6 | */ | |
| 7 | ||
| 8 | /** | |
| b2b91364 YC |
9 | * Implementation of hook_help(). |
| 10 | */ | |
| 11 | function optionwidgets_help($section) { | |
| 12 | if (preg_match('|^admin/content/types/([^/]*)/fields/([^/]*)$|', $section, $matches)) { | |
| 13 | if (($field = content_fields($matches[2], $matches[1])) && in_array($field['widget']['type'], array_keys(optionwidgets_widget_info()))) { | |
| 660f84c7 | 14 | $output = 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' => $field['widget']['label'])); |
| b2b91364 YC |
15 | if ($field['widget']['type'] == 'options_onoff') { |
| 16 | $output .= '<br/>'. t('For a \'Single on/off checkbox\' widget, define the \'off\' value first, then the \'on\' value in the <strong>Allowed values</strong> section. Note that the checkbox will be labeled with the label of the \'on\' value.'); | |
| 17 | } | |
| 18 | else { | |
| 19 | $output .= '<br/>'. t('The \'Checkboxes / radio buttons\' widget will display checkboxes if the multiple values option is selected for this field, otherwise radios will be displayed.'); | |
| 20 | } | |
| 21 | return $output; | |
| 22 | } | |
| 23 | } | |
| 24 | } | |
| 25 | ||
| 26 | /** | |
| 2d3f00e8 JC |
27 | * Implementation of hook_widget_info(). |
| 28 | */ | |
| 29 | function optionwidgets_widget_info() { | |
| d29789de | 30 | $option_types = array('text', 'number_integer', 'number_decimal'); |
| 2d3f00e8 JC |
31 | return array( |
| 32 | 'options_select' => array( | |
| 283efe1d | 33 | 'label' => t('Select list'), |
| d29789de | 34 | 'field types' => $option_types, |
| 2d3f00e8 JC |
35 | ), |
| 36 | 'options_buttons' => array( | |
| 283efe1d | 37 | 'label' => t('Check boxes/radio buttons'), |
| d29789de | 38 | 'field types' => $option_types, |
| 2d3f00e8 | 39 | ), |
| 6c57e0e0 | 40 | 'options_onoff' => array( |
| 283efe1d | 41 | 'label' => t('Single on/off checkbox'), |
| 6c57e0e0 KS |
42 | 'field types' => $option_types, |
| 43 | ), | |
| 2d3f00e8 JC |
44 | ); |
| 45 | } | |
| 46 | ||
| 47 | /** | |
| 48 | * Implementation of hook_widget(). | |
| 49 | */ | |
| 9b37ca26 | 50 | function optionwidgets_widget($op, &$node, $field, &$items) { |
| 2d3f00e8 | 51 | switch ($op) { |
| aeb7c42f | 52 | case 'prepare form values': |
| 9afe0b84 | 53 | $options = _optionwidgets_options($field, $node); |
| 9b37ca26 KS |
54 | $items_transposed = content_transpose_array_rows_cols($items); |
| 55 | $values = (isset($items_transposed['value']) && is_array($items_transposed['value'])) ? $items_transposed['value'] : array(); | |
| 740bb756 | 56 | |
| 2d3f00e8 JC |
57 | $keys = array(); |
| 58 | foreach ($values as $value) { | |
| d29789de KS |
59 | $key = array_search($value, array_keys($options)); |
| 60 | if (isset($key)) { | |
| 61 | $keys[] = $value; | |
| 2d3f00e8 JC |
62 | } |
| 63 | } | |
| 6c57e0e0 | 64 | if ($field['multiple'] || $field['widget']['type'] == 'options_onoff') { |
| 9b37ca26 | 65 | $items['default keys'] = $keys; |
| aeb7c42f JC |
66 | } |
| 67 | else { | |
| 9b37ca26 | 68 | $items['default key'] = reset($keys); |
| aeb7c42f | 69 | } |
| aeb7c42f JC |
70 | break; |
| 71 | ||
| 72 | case 'form': | |
| 9afe0b84 | 73 | $options = _optionwidgets_options($field, $node); |
| 740bb756 | 74 | |
| aeb7c42f | 75 | $form = array(); |
| 740bb756 | 76 | |
| 2d3f00e8 | 77 | $form[$field['field_name']] = array('#tree' => TRUE); |
| 740bb756 | 78 | |
| 2d3f00e8 JC |
79 | switch ($field['widget']['type']) { |
| 80 | case 'options_select': | |
| d3d4f9c6 JC |
81 | if ($field['multiple']) { |
| 82 | $form[$field['field_name']]['keys'] = array( | |
| 83 | '#type' => 'select', | |
| 84 | '#title' => t($field['widget']['label']), | |
| 9b37ca26 | 85 | '#default_value' => $items['default keys'], |
| d3d4f9c6 | 86 | '#multiple' => TRUE, |
| e262a39d | 87 | '#size' => min(count($options), 6), |
| d3d4f9c6 JC |
88 | '#options' => $options, |
| 89 | '#required' => $field['required'], | |
| bd025616 | 90 | '#description' => content_filter_xss(t($field['widget']['description'])), |
| d3d4f9c6 JC |
91 | ); |
| 92 | } | |
| 93 | else { | |
| 94 | $form[$field['field_name']]['key'] = array( | |
| 95 | '#type' => 'select', | |
| 96 | '#title' => t($field['widget']['label']), | |
| 9b37ca26 | 97 | '#default_value' => $items['default key'], |
| d3d4f9c6 JC |
98 | '#multiple' => FALSE, |
| 99 | '#options' => $options, | |
| 100 | '#required' => $field['required'], | |
| bd025616 | 101 | '#description' => content_filter_xss(t($field['widget']['description'])), |
| d3d4f9c6 JC |
102 | ); |
| 103 | } | |
| 2d3f00e8 JC |
104 | break; |
| 105 | ||
| 6c57e0e0 KS |
106 | case 'options_onoff': |
| 107 | // Display only the 'On' value of $options; | |
| 108 | $vals = array_keys($options); | |
| 109 | $on_value = $vals[1]; | |
| 110 | $form[$field['field_name']]['keys'] = array( | |
| 111 | '#type' => 'checkbox', | |
| 112 | '#title' => $options[$on_value], | |
| 1634ccd0 | 113 | '#default_value' => ($items['default keys'][0] == $on_value) ? 1 : 0, |
| 6c57e0e0 | 114 | '#return_value' => $on_value, |
| bd025616 | 115 | '#description' => content_filter_xss(t($field['widget']['description'])), |
| 6c57e0e0 KS |
116 | '#required' => FALSE, |
| 117 | ); | |
| 118 | break; | |
| 119 | ||
| 2d3f00e8 JC |
120 | case 'options_buttons': |
| 121 | if ($field['multiple']) { | |
| aeb7c42f | 122 | $form[$field['field_name']]['keys'] = array( |
| 2d3f00e8 JC |
123 | '#type' => 'checkboxes', |
| 124 | '#title' => t($field['widget']['label']), | |
| 9b37ca26 | 125 | '#default_value' => $items['default keys'], |
| 2d3f00e8 JC |
126 | '#options' => $options, |
| 127 | '#required' => $field['required'], | |
| bd025616 | 128 | '#description' => content_filter_xss(t($field['widget']['description'])), |
| 2d3f00e8 JC |
129 | ); |
| 130 | } | |
| 131 | else { | |
| aeb7c42f | 132 | $form[$field['field_name']]['key'] = array( |
| 2d3f00e8 JC |
133 | '#type' => 'radios', |
| 134 | '#title' => t($field['widget']['label']), | |
| 9b37ca26 | 135 | '#default_value' => $items['default key'], |
| 2d3f00e8 JC |
136 | '#options' => $options, |
| 137 | '#required' => $field['required'], | |
| bd025616 | 138 | '#description' => content_filter_xss(t($field['widget']['description'])), |
| 2d3f00e8 JC |
139 | ); |
| 140 | } | |
| 141 | break; | |
| 142 | } | |
| 143 | return $form; | |
| 144 | ||
| aeb7c42f | 145 | case 'process form values': |
| 9afe0b84 | 146 | $options = _optionwidgets_options($field, $node); |
| aeb7c42f | 147 | |
| 6c57e0e0 KS |
148 | if ($field['multiple'] || $field['widget']['type'] == 'options_onoff') { |
| 149 | $keys = (array) $items['keys']; | |
| aeb7c42f JC |
150 | } |
| 151 | else { | |
| 9b37ca26 | 152 | $keys = array($items['key']); |
| 2d3f00e8 | 153 | } |
| 740bb756 | 154 | |
| aeb7c42f JC |
155 | $values = array(); |
| 156 | foreach ($keys as $key) { | |
| 157 | if (isset($options[$key])) { | |
| d29789de | 158 | $values[] = $key; |
| aeb7c42f JC |
159 | } |
| 160 | } | |
| 740bb756 | 161 | |
| 6c57e0e0 KS |
162 | if ($field['widget']['type'] == 'options_onoff' && empty($values)) { |
| 163 | $keys = array_keys($options); | |
| 164 | $values[] = $keys[0]; | |
| 165 | } | |
| 166 | ||
| 9b37ca26 | 167 | $items = content_transpose_array_rows_cols(array('value' => $values)); |
| 740bb756 | 168 | |
| 06728d7b | 169 | // Remove the widget's data representation so it isn't saved. |
| 9b37ca26 KS |
170 | unset($items['keys']); |
| 171 | unset($items['key']); | |
| 2d3f00e8 JC |
172 | break; |
| 173 | } | |
| 174 | } | |
| 175 | ||
| 9afe0b84 | 176 | function _optionwidgets_options($field, $node) { |
| d29789de KS |
177 | $types = _content_field_types(); |
| 178 | $field_allowed_values = $types[$field['type']]['module'] .'_allowed_values'; | |
| 179 | if (function_exists($field_allowed_values)) { | |
| 9afe0b84 | 180 | $allowed_values = $field_allowed_values($field); |
| d29789de KS |
181 | } |
| 182 | else { | |
| 9afe0b84 | 183 | $allowed_values = array(); |
| d29789de KS |
184 | } |
| 185 | ||
| 9afe0b84 YC |
186 | if ($field['widget']['type'] == 'options_select' || |
| 187 | ($field['widget']['type'] == 'options_buttons' && !$field['multiple'])) { | |
| 188 | if (!$field['required']) { | |
| 189 | $allowed_values = array('' => theme('optionwidgets_none', $field['widget']['type'], $field['field_name'], $node->type)) + $allowed_values; | |
| 190 | } | |
| 191 | } | |
| 192 | ||
| 193 | return $allowed_values; | |
| d29789de | 194 | } |
| 2d3f00e8 | 195 | |
| d29789de KS |
196 | /** |
| 197 | * Theme the label for the empty value for options that are not required. | |
| 198 | * The default theme will display N/A for a radio list and blank for a select. | |
| 199 | */ | |
| 200 | function theme_optionwidgets_none($widget_type, $field_name, $node_type) { | |
| 201 | switch ($widget_type) { | |
| 202 | case 'options_buttons': | |
| 203 | return t('N/A'); | |
| 204 | default : | |
| 205 | return ''; | |
| 2d3f00e8 | 206 | } |
| 283efe1d | 207 | } |