options_element.module

  1. cis7 sites/all/modules/ulmus/options_element/options_element.module
  2. cle7 sites/all/modules/ulmus/options_element/options_element.module
  3. ecd7 sites/all/modules/ulmus/options_element/options_element.module
  4. elmsmedia7 sites/all/modules/ulmus/options_element/options_element.module
  5. harmony7 sites/all/modules/ulmus/options_element/options_element.module
  6. icor7 sites/all/modules/ulmus/options_element/options_element.module
  7. meedjum_blog7 sites/all/modules/ulmus/options_element/options_element.module
  8. mooc7 sites/all/modules/ulmus/options_element/options_element.module

Defines an "options" form element type for entering select list options.

Functions

Namesort descending Description
form_options_expand Expand the "options" form element type.
form_options_from_text Create an array representation of text option values.
form_options_to_text Create a textual representation of options from an array.
form_options_validate Validate the "options" form element type.
form_type_options_value This function adjusts the value of the element from a text value to an array.
options_element_element_info Implements hook_element_info().
options_element_field_instance_settings_validate Form #validate callback for field_ui_field_edit_form().
options_element_field_settings_after_build Element #after_build callback for the options list in Field UI.
options_element_field_settings_validate Form #validate callback for Field UI forms that use an options element.
options_element_form_field_ui_field_edit_form_alter Implements hook_form_FORM_ID_alter().
options_element_form_field_ui_field_settings_form_alter Implements hook_form_FORM_ID_alter().
options_element_theme Implementation of hook_theme().
_options_element_add_allowed_values_element Add options_element widget to form.
_options_element_supported_field_types Returns supported field types.

File

sites/all/modules/ulmus/options_element/options_element.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Defines an "options" form element type for entering select list options.
  5. */
  6. /**
  7. * Implements hook_element_info().
  8. *
  9. * Defines the #type = 'options' form element type.
  10. *
  11. * The 'options' form element type is useful when collecting a series of
  12. * values in a list. The values within the list may optionally have unique
  13. * keys, such as that in an array structure. In addition, a default choice
  14. * (or several default choices) may be selected by the user.
  15. *
  16. * @code
  17. * $element['options'] = array(
  18. * '#type' => 'options',
  19. * '#limit' => 20,
  20. * '#optgroups' => FALSE,
  21. * '#multiple' => FALSE,
  22. * '#options' => array(
  23. * 'foo' => 'foo',
  24. * 'bar' => 'bar',
  25. * 'baz' => 'baz',
  26. * ),
  27. * '#default_value' => 'foo'
  28. * '#key_type' => 'associative',
  29. * );
  30. * @endcode
  31. *
  32. * Properties for the 'options' element include:
  33. * - limit: The maximum number of options that can be added to a list. Defaults
  34. * to 100.
  35. * - optgroups: If nesting of options is supported, up to one level. This is
  36. * used when building a select HTML element that uses optgroups. Defaults to
  37. * FALSE.
  38. * - multiple: Affects the number of default values that may be selected.
  39. * - default_value: The key(s) for the options that are currently selected. If
  40. * #multiple is TRUE then, the default value is an array, otherwise it is a
  41. * string.
  42. * - options: An array of options currently within the list.
  43. * - key_type: The method by which keys are determined for each value in the
  44. * option list. Available options include:
  45. * - mixed: Each value is not given any ID automatically, but any manually
  46. * specified keys will be retained. This most emulates the existing
  47. * conventions within Drupal, where keys are optional but allowed.
  48. * - numeric: Each value is automatically given a unique numeric ID. This can
  49. * be useful when wanting duplicate values in a list and not have to bother
  50. * the end-user for keys.
  51. * - associative: Keys are automatically mapped from the user-entered values.
  52. * This is equivalent to making key|value pairs, but both the key and value
  53. * are the same. Each key must be unique.
  54. * - custom: Keys are manually entered by the end user. A second set of
  55. * textfields are presented to let the user provide keys as well as values.
  56. * - none: No keys are specified at all. This effectively creates numeric keys
  57. * but unlike numeric keys, the keys are renumbered if the options in the
  58. * list are rearranged.
  59. * - key_type_toggle: If specified, a checkbox will be added that allows the
  60. * user to toggle between the current key type and the "custom" key type,
  61. * letting them customize the keys as desired. This option has no effect with
  62. * the "none" key type.
  63. * - key_type_toggled: Determine if the toggle checkbox is set or not by
  64. * default.
  65. * - default_value_allowed: Indicates whether the end user should be able to
  66. * modify the default value when editing the options list. Defaults to TRUE.
  67. * - default_value_pattern: If allowing dynamic default value keys, such as a
  68. * token, specify a regular expression pattern that will also be allowed as
  69. * a default value. Include pattern delimiters. Defaults to an empty string.
  70. *
  71. * @code
  72. * $element['options'] = array(
  73. * '#type' => 'options',
  74. * '#key_type' => 'associative',
  75. * '#key_type_toggle' => t('Custom keys'),
  76. * '#key_type_toggled' => TRUE,
  77. * );
  78. * @endcode
  79. */
  80. function options_element_element_info() {
  81. $type = array();
  82. $type['options'] = array(
  83. '#input' => TRUE,
  84. '#process' => array('form_options_expand'),
  85. '#limit' => NULL,
  86. '#optgroups' => TRUE,
  87. '#multiple' => FALSE,
  88. '#options' => array(),
  89. '#options_readonly' => FALSE,
  90. '#key_type' => 'mixed',
  91. '#key_type_toggle' => NULL,
  92. '#key_type_toggled' => FALSE,
  93. '#default_value_allowed' => TRUE,
  94. '#default_value_pattern' => '',
  95. '#element_validate' => array('form_options_validate'),
  96. '#theme_wrappers' => array('form_element'),
  97. );
  98. return $type;
  99. }
  100. /**
  101. * Implementation of hook_theme().
  102. */
  103. function options_element_theme() {
  104. return array(
  105. 'options' => array(
  106. 'render element' => 'element',
  107. 'file' => 'options_element.inc',
  108. ),
  109. );
  110. }
  111. /**
  112. * Expand the "options" form element type.
  113. *
  114. * The "options" type is simply an enhanced textarea that makes it easier to
  115. * create key|value pairs and put items into optgroups.
  116. */
  117. function form_options_expand($element) {
  118. module_load_include('inc', 'options_element');
  119. return _form_options_expand($element);
  120. }
  121. /**
  122. * Validate the "options" form element type.
  123. */
  124. function form_options_validate($element, &$form_state) {
  125. module_load_include('inc', 'options_element');
  126. _form_options_validate($element, $form_state);
  127. }
  128. /**
  129. * This function adjusts the value of the element from a text value to an array.
  130. */
  131. function form_type_options_value(&$element, $edit = FALSE) {
  132. module_load_include('inc', 'options_element');
  133. return _form_type_options_value($element, $edit);
  134. }
  135. /**
  136. * Create a textual representation of options from an array.
  137. *
  138. * @param $options
  139. * An array of options used in a select list.
  140. * @param $key_type
  141. * How key/value pairs should be interpreted. Available options:
  142. * - mixed
  143. * - numeric
  144. * - associative
  145. * - custom
  146. * - none
  147. */
  148. function form_options_to_text($options, $key_type) {
  149. module_load_include('inc', 'options_element');
  150. return _form_options_to_text($options, $key_type);
  151. }
  152. /**
  153. * Create an array representation of text option values.
  154. *
  155. * If the Key of the option is within < >, treat as an optgroup
  156. *
  157. * <Group 1>
  158. * creates an optgroup with the label "Group 1"
  159. *
  160. * <>
  161. * Exits the current group, allowing items to be inserted at the root element.
  162. */
  163. function form_options_from_text($text, $key_type, $flat = FALSE, &$duplicates = array()) {
  164. module_load_include('inc', 'options_element');
  165. return _form_options_from_text($text, $key_type, $flat, $duplicates);
  166. }
  167. /**
  168. * Implements hook_form_FORM_ID_alter().
  169. */
  170. function options_element_form_field_ui_field_settings_form_alter(&$form, &$form_state) {
  171. $type = $form['field']['type']['#value'];
  172. if (in_array($type, _options_element_supported_field_types())) {
  173. // Get default value.
  174. $options = form_options_from_text($form['field']['settings']['allowed_values']['#default_value'], 'mixed');
  175. // Toggled by default if options already set.
  176. $key_type_toggled = !empty($options);
  177. $element_overwrite = array(
  178. '#options' => $options,
  179. '#multiple' => FALSE,
  180. '#key_type_toggled' => $key_type_toggled,
  181. '#default_value_allowed' => FALSE,
  182. );
  183. _options_element_add_allowed_values_element($form, $element_overwrite);
  184. }
  185. }
  186. /**
  187. * Implements hook_form_FORM_ID_alter().
  188. */
  189. function options_element_form_field_ui_field_edit_form_alter(&$form, &$form_state) {
  190. $field = $form['#field'];
  191. if (in_array($field['type'], _options_element_supported_field_types())) {
  192. // Use options_element default widget instead of field widget.
  193. $form['instance']['default_value_widget']['#access'] = FALSE;
  194. // Language is hardcoded to 'und' in field_ui_default_value_widget().
  195. $default_field_value = $form['instance']['default_value_widget'][$field['field_name']][LANGUAGE_NONE]['#default_value'];
  196. // Overwrite 'allowed_values' element.
  197. $element_overwrite = array(
  198. '#options' => $field['settings']['allowed_values'],
  199. '#multiple' => !($form['field']['cardinality']['#default_value'] == 1),
  200. '#default_value' => $default_field_value,
  201. );
  202. _options_element_add_allowed_values_element($form, $element_overwrite);
  203. // Add proper default value inside validate callback.
  204. $form['#validate'][] = 'options_element_field_instance_settings_validate';
  205. }
  206. }
  207. /**
  208. * Element #after_build callback for the options list in Field UI.
  209. */
  210. function options_element_field_settings_after_build($element, &$form_state) {
  211. // Add original data for validating.
  212. $element['options_field'] = array_merge($element['options_field'], $element['#original']);
  213. // Standard validate.
  214. $element['options_field']['#element_validate'][] = 'list_allowed_values_setting_validate';
  215. return $element;
  216. }
  217. /**
  218. * Form #validate callback for Field UI forms that use an options element.
  219. */
  220. function options_element_field_settings_validate($form, &$form_state) {
  221. if (!form_get_errors()) {
  222. // Set allowed values.
  223. form_set_value(
  224. $form['field']['settings']['allowed_values'],
  225. $form['field']['settings']['allowed_values']['#value']['options'],
  226. $form_state
  227. );
  228. }
  229. }
  230. /**
  231. * Form #validate callback for field_ui_field_edit_form().
  232. */
  233. function options_element_field_instance_settings_validate($form, &$form_state) {
  234. $field = $form['#field'];
  235. if (!form_get_errors()) {
  236. // Set default values.
  237. $default_field_value_key = $form['instance']['default_value_widget'][$field['field_name']][LANGUAGE_NONE]['#value_key'];
  238. // Create transposed array for field storage.
  239. $default_values = options_array_transpose(array($default_field_value_key => (array)$form['field']['settings']['allowed_values']['#value']['default_value']));
  240. form_set_value(
  241. $form['instance']['default_value_widget'][$field['field_name']][LANGUAGE_NONE],
  242. $default_values,
  243. $form_state
  244. );
  245. }
  246. }
  247. /**
  248. * Add options_element widget to form.
  249. */
  250. function _options_element_add_allowed_values_element(&$form, $element) {
  251. $default_element = array(
  252. '#type' => 'options',
  253. '#options' => array(),
  254. '#key_type' => 'mixed', // Optimal for now.
  255. '#key_type_toggle' => t('Custom keys'),
  256. '#key_type_toggled' => TRUE,
  257. '#after_build' => array('options_element_field_settings_after_build'),
  258. '#access' => $form['field']['settings']['allowed_values']['#access'],
  259. '#default_value' => '',
  260. // Pass original data for validating.
  261. '#original' => array(
  262. '#title' => $form['field']['settings']['allowed_values']['#title'],
  263. '#field_has_data' => $form['field']['settings']['allowed_values']['#field_has_data'],
  264. '#field' => $form['field']['settings']['allowed_values']['#field'],
  265. '#field_type' => $form['field']['settings']['allowed_values']['#field_type'],
  266. )
  267. );
  268. $element = drupal_array_merge_deep($default_element, $element);
  269. $form['field']['settings']['allowed_values'] = $element;
  270. // Set proper allowed values in $form_state.
  271. $form['#validate'][] = 'options_element_field_settings_validate';
  272. }
  273. /**
  274. * Returns supported field types.
  275. */
  276. function _options_element_supported_field_types() {
  277. return array('list_integer', 'list_float', 'list_text');
  278. }