optionwidgets.module

Tracking 5.x-1.x branch
  1. drupal
    1. 5 contributions/cck/optionwidgets.module
    2. 6 contributions/cck/modules/optionwidgets/optionwidgets.module

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

Functions & methods

NameDescription
optionwidgets_helpImplementation of hook_help().
optionwidgets_widgetImplementation of hook_widget().
optionwidgets_widget_infoImplementation of hook_widget_info().
theme_optionwidgets_noneTheme the label for the empty value for options that are not required. The default theme will display N/A for a radio list and blank for a select.
_optionwidgets_options

File

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