multiple_selects.module 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * Implements hook_theme().
  4. */
  5. function multiple_selects_theme() {
  6. return array(
  7. 'multiple_selects_none' => array(
  8. 'variables' => array('instance' => NULL, 'option' => NULL),
  9. ),
  10. );
  11. }
  12. /**
  13. * Returns HTML for the label for the empty value for options that are not required.
  14. *
  15. * The default theme will display N/A for a radio list and '- None -' for a select.
  16. *
  17. * @param $variables
  18. * An associative array containing:
  19. * - instance: An array representing the widget requesting the options.
  20. *
  21. * @ingroup themeable
  22. */
  23. function theme_multiple_selects_none($variables) {
  24. $instance = $variables['instance'];
  25. $option = $variables['option'];
  26. $output = ($option == 'option_none' ? t('- None -') : t('- Select a value -'));
  27. return $output;
  28. }
  29. /**
  30. * Implements hook_field_widget_info().
  31. *
  32. * Field type modules willing to use those widgets should:
  33. * - Use hook_field_widget_info_alter() to append their field own types to the
  34. * list of types supported by the widgets,
  35. */
  36. function multiple_selects_field_widget_info() {
  37. return array(
  38. 'multiple_selects' => array(
  39. 'label' => t('Multiple Selects list'),
  40. 'field types' => array('taxonomy_term_reference', 'list_integer', 'list_float', 'list_text'),
  41. ),
  42. );
  43. }
  44. /**
  45. * Implements hook_field_widget_form().
  46. */
  47. function multiple_selects_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  48. // Abstract over the actual field columns, to allow different field types to
  49. // reuse those widgets.
  50. $value_key = key($field['columns']);
  51. $type = "select";
  52. $multiple = 1;
  53. $required = $element['#required'];
  54. $has_value = isset($items[0][$value_key]);
  55. $properties = _options_properties($type, $multiple, $required, $has_value);
  56. // Prepare the list of options.
  57. $options = _multiple_selects_get_options($field, $instance, $properties);
  58. $widget = $element;
  59. $widget += array(
  60. '#type' => 'select',
  61. '#default_value' => isset($items[$delta][$value_key]) ? $items[$delta][$value_key] : NULL,
  62. '#options' => $options,
  63. '#element_validate' => array('multiple_selects_widget_validate'),
  64. '#value_key' => $value_key,
  65. '#properties' => $properties,
  66. );
  67. $element[$value_key] = $widget;
  68. return $element;
  69. }
  70. /**
  71. * Form element validation handler for options element.
  72. */
  73. function multiple_selects_widget_validate($element, &$form_state) {
  74. $value_key = $element['#value_key'];
  75. $properties = $element['#properties'];
  76. $value = $element['#value'];
  77. if ($element['#required'] && $element['#value'] == '_none') {
  78. form_error($element, t('!name field is required.', array('!name' => $element['#title'])));
  79. }
  80. if ($properties['empty_option']) {
  81. if ($value == '_none') {
  82. form_set_value($element, NULL, $form_state);
  83. }
  84. }
  85. }
  86. /**
  87. * Implements hook_field_widget_error().
  88. */
  89. function multiple_selects_field_widget_error($element, $error, $form, &$form_state) {
  90. form_error($element, $error['message']);
  91. }
  92. /**
  93. * Collects the options for a field.
  94. */
  95. function _multiple_selects_get_options($field, $instance, $properties) {
  96. // Get the list of options.
  97. $options = (array) module_invoke($field['module'], 'options_list', $field, $instance);
  98. // Sanitize the options.
  99. _options_prepare_options($options, $properties);
  100. if (!$properties['optgroups']) {
  101. $options = options_array_flatten($options);
  102. }
  103. if ($properties['empty_option']) {
  104. $label = theme('multiple_selects_none', array('instance' => $instance, 'option' => $properties['empty_option']));
  105. $options = array('_none' => $label) + $options;
  106. }
  107. return $options;
  108. }