options.module 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. <?php
  2. /**
  3. * @file
  4. * Defines selection, check box and radio button widgets for text and numeric fields.
  5. */
  6. /**
  7. * Implements hook_help().
  8. */
  9. function options_help($path, $arg) {
  10. switch ($path) {
  11. case 'admin/help#options':
  12. $output = '';
  13. $output .= '<h3>' . t('About') . '</h3>';
  14. $output .= '<p>' . t('The Options module defines checkbox, selection, and other input widgets for the Field module. See the <a href="@field-help">Field module help page</a> for more information about fields.', array('@field-help' => url('admin/help/field'))) . '</p>';
  15. return $output;
  16. }
  17. }
  18. /**
  19. * Implements hook_theme().
  20. */
  21. function options_theme() {
  22. return array(
  23. 'options_none' => array(
  24. 'variables' => array('instance' => NULL, 'option' => NULL),
  25. ),
  26. );
  27. }
  28. /**
  29. * Implements hook_field_widget_info().
  30. *
  31. * Field type modules willing to use those widgets should:
  32. * - Use hook_field_widget_info_alter() to append their field own types to the
  33. * list of types supported by the widgets,
  34. * - Implement hook_options_list() to provide the list of options.
  35. * See list.module.
  36. */
  37. function options_field_widget_info() {
  38. return array(
  39. 'options_select' => array(
  40. 'label' => t('Select list'),
  41. 'field types' => array(),
  42. 'behaviors' => array(
  43. 'multiple values' => FIELD_BEHAVIOR_CUSTOM,
  44. ),
  45. ),
  46. 'options_buttons' => array(
  47. 'label' => t('Check boxes/radio buttons'),
  48. 'field types' => array(),
  49. 'behaviors' => array(
  50. 'multiple values' => FIELD_BEHAVIOR_CUSTOM,
  51. ),
  52. ),
  53. 'options_onoff' => array(
  54. 'label' => t('Single on/off checkbox'),
  55. 'field types' => array(),
  56. 'behaviors' => array(
  57. 'multiple values' => FIELD_BEHAVIOR_CUSTOM,
  58. ),
  59. 'settings' => array('display_label' => 0),
  60. ),
  61. );
  62. }
  63. /**
  64. * Implements hook_field_widget_form().
  65. */
  66. function options_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  67. // Abstract over the actual field columns, to allow different field types to
  68. // reuse those widgets.
  69. $value_key = key($field['columns']);
  70. $type = str_replace('options_', '', $instance['widget']['type']);
  71. $multiple = $field['cardinality'] > 1 || $field['cardinality'] == FIELD_CARDINALITY_UNLIMITED;
  72. $required = $element['#required'];
  73. $has_value = isset($items[0][$value_key]);
  74. $properties = _options_properties($type, $multiple, $required, $has_value);
  75. $entity_type = $element['#entity_type'];
  76. $entity = $element['#entity'];
  77. // Prepare the list of options.
  78. $options = _options_get_options($field, $instance, $properties, $entity_type, $entity);
  79. // Put current field values in shape.
  80. $default_value = _options_storage_to_form($items, $options, $value_key, $properties);
  81. switch ($type) {
  82. case 'select':
  83. $element += array(
  84. '#type' => 'select',
  85. '#default_value' => $default_value,
  86. // Do not display a 'multiple' select box if there is only one option.
  87. '#multiple' => $multiple && count($options) > 1,
  88. '#options' => $options,
  89. );
  90. break;
  91. case 'buttons':
  92. // If required and there is one single option, preselect it.
  93. if ($required && count($options) == 1) {
  94. reset($options);
  95. $default_value = array(key($options));
  96. }
  97. // If this is a single-value field, take the first default value, or
  98. // default to NULL so that the form element is properly recognized as
  99. // not having a default value.
  100. if (!$multiple) {
  101. $default_value = $default_value ? reset($default_value) : NULL;
  102. }
  103. $element += array(
  104. '#type' => $multiple ? 'checkboxes' : 'radios',
  105. // Radio buttons need a scalar value.
  106. '#default_value' => $default_value,
  107. '#options' => $options,
  108. );
  109. break;
  110. case 'onoff':
  111. $keys = array_keys($options);
  112. $off_value = array_shift($keys);
  113. $on_value = array_shift($keys);
  114. $element += array(
  115. '#type' => 'checkbox',
  116. '#default_value' => (isset($default_value[0]) && $default_value[0] == $on_value) ? 1 : 0,
  117. '#on_value' => $on_value,
  118. '#off_value' => $off_value,
  119. );
  120. // Override the title from the incoming $element.
  121. $element['#title'] = isset($options[$on_value]) ? $options[$on_value] : '';
  122. if ($instance['widget']['settings']['display_label']) {
  123. $element['#title'] = $instance['label'];
  124. }
  125. break;
  126. }
  127. $element += array(
  128. '#value_key' => $value_key,
  129. '#element_validate' => array('options_field_widget_validate'),
  130. '#properties' => $properties,
  131. );
  132. return $element;
  133. }
  134. /**
  135. * Implements hook_field_widget_settings_form().
  136. */
  137. function options_field_widget_settings_form($field, $instance) {
  138. $form = array();
  139. if ($instance['widget']['type'] == 'options_onoff') {
  140. $form['display_label'] = array(
  141. '#type' => 'checkbox',
  142. '#title' => t('Use field label instead of the "On value" as label'),
  143. '#default_value' => $instance['widget']['settings']['display_label'],
  144. '#weight' => -1,
  145. );
  146. }
  147. return $form;
  148. }
  149. /**
  150. * Form element validation handler for options element.
  151. */
  152. function options_field_widget_validate($element, &$form_state) {
  153. if ($element['#required'] && $element['#value'] == '_none') {
  154. form_error($element, t('!name field is required.', array('!name' => $element['#title'])));
  155. }
  156. // Transpose selections from field => delta to delta => field, turning
  157. // multiple selected options into multiple parent elements.
  158. $items = _options_form_to_storage($element);
  159. form_set_value($element, $items, $form_state);
  160. }
  161. /**
  162. * Describes the preparation steps required by each widget.
  163. */
  164. function _options_properties($type, $multiple, $required, $has_value) {
  165. $base = array(
  166. 'filter_xss' => FALSE,
  167. 'strip_tags' => FALSE,
  168. 'strip_tags_and_unescape' => FALSE,
  169. 'empty_option' => FALSE,
  170. 'optgroups' => FALSE,
  171. );
  172. $properties = array();
  173. switch ($type) {
  174. case 'select':
  175. $properties = array(
  176. // Select boxes do not support any HTML tag.
  177. 'strip_tags_and_unescape' => TRUE,
  178. 'optgroups' => TRUE,
  179. );
  180. if ($multiple) {
  181. // Multiple select: add a 'none' option for non-required fields.
  182. if (!$required) {
  183. $properties['empty_option'] = 'option_none';
  184. }
  185. }
  186. else {
  187. // Single select: add a 'none' option for non-required fields,
  188. // and a 'select a value' option for required fields that do not come
  189. // with a value selected.
  190. if (!$required) {
  191. $properties['empty_option'] = 'option_none';
  192. }
  193. elseif (!$has_value) {
  194. $properties['empty_option'] = 'option_select';
  195. }
  196. }
  197. break;
  198. case 'buttons':
  199. $properties = array(
  200. 'filter_xss' => TRUE,
  201. );
  202. // Add a 'none' option for non-required radio buttons.
  203. if (!$required && !$multiple) {
  204. $properties['empty_option'] = 'option_none';
  205. }
  206. break;
  207. case 'onoff':
  208. $properties = array(
  209. 'filter_xss' => TRUE,
  210. );
  211. break;
  212. }
  213. return $properties + $base;
  214. }
  215. /**
  216. * Collects the options for a field.
  217. */
  218. function _options_get_options($field, $instance, $properties, $entity_type, $entity) {
  219. // Get the list of options.
  220. $options = (array) module_invoke($field['module'], 'options_list', $field, $instance, $entity_type, $entity);
  221. // Sanitize the options.
  222. _options_prepare_options($options, $properties);
  223. if (!$properties['optgroups']) {
  224. $options = options_array_flatten($options);
  225. }
  226. if ($properties['empty_option']) {
  227. $label = theme('options_none', array('instance' => $instance, 'option' => $properties['empty_option']));
  228. $options = array('_none' => $label) + $options;
  229. }
  230. return $options;
  231. }
  232. /**
  233. * Sanitizes the options.
  234. *
  235. * The function is recursive to support optgroups.
  236. */
  237. function _options_prepare_options(&$options, $properties) {
  238. foreach ($options as $value => $label) {
  239. // Recurse for optgroups.
  240. if (is_array($label)) {
  241. _options_prepare_options($options[$value], $properties);
  242. }
  243. else {
  244. // The 'strip_tags' option is deprecated. Use 'strip_tags_and_unescape'
  245. // when plain text is required (and where the output will be run through
  246. // check_plain() before being inserted back into HTML) or 'filter_xss'
  247. // when HTML is required.
  248. if ($properties['strip_tags']) {
  249. $options[$value] = strip_tags($label);
  250. }
  251. if ($properties['strip_tags_and_unescape']) {
  252. $options[$value] = decode_entities(strip_tags($label));
  253. }
  254. if ($properties['filter_xss']) {
  255. $options[$value] = field_filter_xss($label);
  256. }
  257. }
  258. }
  259. }
  260. /**
  261. * Transforms stored field values into the format the widgets need.
  262. */
  263. function _options_storage_to_form($items, $options, $column, $properties) {
  264. $items_transposed = options_array_transpose($items);
  265. $values = (isset($items_transposed[$column]) && is_array($items_transposed[$column])) ? $items_transposed[$column] : array();
  266. // Discard values that are not in the current list of options. Flatten the
  267. // array if needed.
  268. if ($properties['optgroups']) {
  269. $options = options_array_flatten($options);
  270. }
  271. $values = array_values(array_intersect($values, array_keys($options)));
  272. return $values;
  273. }
  274. /**
  275. * Transforms submitted form values into field storage format.
  276. */
  277. function _options_form_to_storage($element) {
  278. $values = array_values((array) $element['#value']);
  279. $properties = $element['#properties'];
  280. // On/off checkbox: transform '0 / 1' into the 'on / off' values.
  281. if ($element['#type'] == 'checkbox') {
  282. $values = array($values[0] ? $element['#on_value'] : $element['#off_value']);
  283. }
  284. // Filter out the 'none' option. Use a strict comparison, because
  285. // 0 == 'any string'.
  286. if ($properties['empty_option']) {
  287. $index = array_search('_none', $values, TRUE);
  288. if ($index !== FALSE) {
  289. unset($values[$index]);
  290. }
  291. }
  292. // Make sure we populate at least an empty value.
  293. if (empty($values)) {
  294. $values = array(NULL);
  295. }
  296. $result = options_array_transpose(array($element['#value_key'] => $values));
  297. return $result;
  298. }
  299. /**
  300. * Manipulates a 2D array to reverse rows and columns.
  301. *
  302. * The default data storage for fields is delta first, column names second.
  303. * This is sometimes inconvenient for field modules, so this function can be
  304. * used to present the data in an alternate format.
  305. *
  306. * @param $array
  307. * The array to be transposed. It must be at least two-dimensional, and
  308. * the subarrays must all have the same keys or behavior is undefined.
  309. * @return
  310. * The transposed array.
  311. */
  312. function options_array_transpose($array) {
  313. $result = array();
  314. if (is_array($array)) {
  315. foreach ($array as $key1 => $value1) {
  316. if (is_array($value1)) {
  317. foreach ($value1 as $key2 => $value2) {
  318. if (!isset($result[$key2])) {
  319. $result[$key2] = array();
  320. }
  321. $result[$key2][$key1] = $value2;
  322. }
  323. }
  324. }
  325. }
  326. return $result;
  327. }
  328. /**
  329. * Flattens an array of allowed values.
  330. *
  331. * @param $array
  332. * A single or multidimensional array.
  333. * @return
  334. * A flattened array.
  335. */
  336. function options_array_flatten($array) {
  337. $result = array();
  338. if (is_array($array)) {
  339. foreach ($array as $key => $value) {
  340. if (is_array($value)) {
  341. $result += options_array_flatten($value);
  342. }
  343. else {
  344. $result[$key] = $value;
  345. }
  346. }
  347. }
  348. return $result;
  349. }
  350. /**
  351. * Implements hook_field_widget_error().
  352. */
  353. function options_field_widget_error($element, $error, $form, &$form_state) {
  354. form_error($element, $error['message']);
  355. }
  356. /**
  357. * Returns HTML for the label for the empty value for options that are not required.
  358. *
  359. * The default theme will display N/A for a radio list and '- None -' for a select.
  360. *
  361. * @param $variables
  362. * An associative array containing:
  363. * - instance: An array representing the widget requesting the options.
  364. *
  365. * @ingroup themeable
  366. */
  367. function theme_options_none($variables) {
  368. $instance = $variables['instance'];
  369. $option = $variables['option'];
  370. $output = '';
  371. switch ($instance['widget']['type']) {
  372. case 'options_buttons':
  373. $output = t('N/A');
  374. break;
  375. case 'options_select':
  376. $output = ($option == 'option_none' ? t('- None -') : t('- Select a value -'));
  377. break;
  378. }
  379. return $output;
  380. }