options.module

You are here

  1. 7 modules/field/modules/options/options.module
  2. 8 core/modules/options/options.module

Defines selection, check box and radio button widgets for text and numeric fields.

Functions

Namesort descending Description
options_array_flatten Flattens an array of allowed values.
options_array_transpose Manipulates a 2D array to reverse rows and columns.
options_field_widget_error Implements hook_field_widget_error().
options_field_widget_form Implements hook_field_widget_form().
options_field_widget_info Implements hook_field_widget_info().
options_field_widget_settings_form Implements hook_field_widget_settings_form().
options_field_widget_validate Form element validation handler for options element.
options_help Implements hook_help().
options_theme Implements hook_theme().
theme_options_none Returns HTML for the label for the empty value for options that are not required.
_options_form_to_storage Transforms submitted form values into field storage format.
_options_get_options Collects the options for a field.
_options_prepare_options Sanitizes the options.
_options_properties Describes the preparation steps required by each widget.
_options_storage_to_form Transforms stored field values into the format the widgets need.

File

modules/field/modules/options/options.module
View source
  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. 'empty_option' => FALSE,
  169. 'optgroups' => FALSE,
  170. );
  171. $properties = array();
  172. switch ($type) {
  173. case 'select':
  174. $properties = array(
  175. // Select boxes do not support any HTML tag.
  176. 'strip_tags' => TRUE,
  177. 'optgroups' => TRUE,
  178. );
  179. if ($multiple) {
  180. // Multiple select: add a 'none' option for non-required fields.
  181. if (!$required) {
  182. $properties['empty_option'] = 'option_none';
  183. }
  184. }
  185. else {
  186. // Single select: add a 'none' option for non-required fields,
  187. // and a 'select a value' option for required fields that do not come
  188. // with a value selected.
  189. if (!$required) {
  190. $properties['empty_option'] = 'option_none';
  191. }
  192. elseif (!$has_value) {
  193. $properties['empty_option'] = 'option_select';
  194. }
  195. }
  196. break;
  197. case 'buttons':
  198. $properties = array(
  199. 'filter_xss' => TRUE,
  200. );
  201. // Add a 'none' option for non-required radio buttons.
  202. if (!$required && !$multiple) {
  203. $properties['empty_option'] = 'option_none';
  204. }
  205. break;
  206. case 'onoff':
  207. $properties = array(
  208. 'filter_xss' => TRUE,
  209. );
  210. break;
  211. }
  212. return $properties + $base;
  213. }
  214. /**
  215. * Collects the options for a field.
  216. */
  217. function _options_get_options($field, $instance, $properties, $entity_type, $entity) {
  218. // Get the list of options.
  219. $options = (array) module_invoke($field['module'], 'options_list', $field, $instance, $entity_type, $entity);
  220. // Sanitize the options.
  221. _options_prepare_options($options, $properties);
  222. if (!$properties['optgroups']) {
  223. $options = options_array_flatten($options);
  224. }
  225. if ($properties['empty_option']) {
  226. $label = theme('options_none', array('instance' => $instance, 'option' => $properties['empty_option']));
  227. $options = array('_none' => $label) + $options;
  228. }
  229. return $options;
  230. }
  231. /**
  232. * Sanitizes the options.
  233. *
  234. * The function is recursive to support optgroups.
  235. */
  236. function _options_prepare_options(&$options, $properties) {
  237. foreach ($options as $value => $label) {
  238. // Recurse for optgroups.
  239. if (is_array($label)) {
  240. _options_prepare_options($options[$value], $properties);
  241. }
  242. else {
  243. if ($properties['strip_tags']) {
  244. $options[$value] = strip_tags($label);
  245. }
  246. if ($properties['filter_xss']) {
  247. $options[$value] = field_filter_xss($label);
  248. }
  249. }
  250. }
  251. }
  252. /**
  253. * Transforms stored field values into the format the widgets need.
  254. */
  255. function _options_storage_to_form($items, $options, $column, $properties) {
  256. $items_transposed = options_array_transpose($items);
  257. $values = (isset($items_transposed[$column]) && is_array($items_transposed[$column])) ? $items_transposed[$column] : array();
  258. // Discard values that are not in the current list of options. Flatten the
  259. // array if needed.
  260. if ($properties['optgroups']) {
  261. $options = options_array_flatten($options);
  262. }
  263. $values = array_values(array_intersect($values, array_keys($options)));
  264. return $values;
  265. }
  266. /**
  267. * Transforms submitted form values into field storage format.
  268. */
  269. function _options_form_to_storage($element) {
  270. $values = array_values((array) $element['#value']);
  271. $properties = $element['#properties'];
  272. // On/off checkbox: transform '0 / 1' into the 'on / off' values.
  273. if ($element['#type'] == 'checkbox') {
  274. $values = array($values[0] ? $element['#on_value'] : $element['#off_value']);
  275. }
  276. // Filter out the 'none' option. Use a strict comparison, because
  277. // 0 == 'any string'.
  278. if ($properties['empty_option']) {
  279. $index = array_search('_none', $values, TRUE);
  280. if ($index !== FALSE) {
  281. unset($values[$index]);
  282. }
  283. }
  284. // Make sure we populate at least an empty value.
  285. if (empty($values)) {
  286. $values = array(NULL);
  287. }
  288. $result = options_array_transpose(array($element['#value_key'] => $values));
  289. return $result;
  290. }
  291. /**
  292. * Manipulates a 2D array to reverse rows and columns.
  293. *
  294. * The default data storage for fields is delta first, column names second.
  295. * This is sometimes inconvenient for field modules, so this function can be
  296. * used to present the data in an alternate format.
  297. *
  298. * @param $array
  299. * The array to be transposed. It must be at least two-dimensional, and
  300. * the subarrays must all have the same keys or behavior is undefined.
  301. * @return
  302. * The transposed array.
  303. */
  304. function options_array_transpose($array) {
  305. $result = array();
  306. if (is_array($array)) {
  307. foreach ($array as $key1 => $value1) {
  308. if (is_array($value1)) {
  309. foreach ($value1 as $key2 => $value2) {
  310. if (!isset($result[$key2])) {
  311. $result[$key2] = array();
  312. }
  313. $result[$key2][$key1] = $value2;
  314. }
  315. }
  316. }
  317. }
  318. return $result;
  319. }
  320. /**
  321. * Flattens an array of allowed values.
  322. *
  323. * @param $array
  324. * A single or multidimensional array.
  325. * @return
  326. * A flattened array.
  327. */
  328. function options_array_flatten($array) {
  329. $result = array();
  330. if (is_array($array)) {
  331. foreach ($array as $key => $value) {
  332. if (is_array($value)) {
  333. $result += options_array_flatten($value);
  334. }
  335. else {
  336. $result[$key] = $value;
  337. }
  338. }
  339. }
  340. return $result;
  341. }
  342. /**
  343. * Implements hook_field_widget_error().
  344. */
  345. function options_field_widget_error($element, $error, $form, &$form_state) {
  346. form_error($element, $error['message']);
  347. }
  348. /**
  349. * Returns HTML for the label for the empty value for options that are not required.
  350. *
  351. * The default theme will display N/A for a radio list and '- None -' for a select.
  352. *
  353. * @param $variables
  354. * An associative array containing:
  355. * - instance: An array representing the widget requesting the options.
  356. *
  357. * @ingroup themeable
  358. */
  359. function theme_options_none($variables) {
  360. $instance = $variables['instance'];
  361. $option = $variables['option'];
  362. $output = '';
  363. switch ($instance['widget']['type']) {
  364. case 'options_buttons':
  365. $output = t('N/A');
  366. break;
  367. case 'options_select':
  368. $output = ($option == 'option_none' ? t('- None -') : t('- Select a value -'));
  369. break;
  370. }
  371. return $output;
  372. }