CCK: Defines selection, check box and radio button widgets for text and numeric fields. Note: Requires content.module, text.module and number.module.'); } } /** * Implementation of hook_widget_info(). */ function optionwidgets_widget_info() { return array( 'options_select' => array( 'label' => 'Select list', 'field types' => array('text', 'number_integer', 'number_decimal'), ), 'options_buttons' => array( 'label' => 'Check boxes/radio buttons', 'field types' => array('text', 'number_integer', 'number_decimal'), ), ); } /** * Implementation of hook_widget(). */ function optionwidgets_widget($op, &$node, $field, &$node_field) { switch ($op) { case 'prepare form values': $options = _optionwidgets_options($field); $node_field_transposed = content_transpose_array_rows_cols($node_field); $values = (isset($node_field_transposed['value']) && is_array($node_field_transposed['value'])) ? $node_field_transposed['value'] : array(); $keys = array(); foreach ($values as $value) { $key = array_search($value, $options); if ($key) { $keys[] = $key; } } if ($field['multiple']) { $node_field['default keys'] = $keys; } else { $node_field['default key'] = reset($keys); } break; case 'form': $options = _optionwidgets_options($field); $form = array(); $form[$field['field_name']] = array('#tree' => TRUE); switch ($field['widget']['type']) { case 'options_select': if (!$field['required']) $options = array('' => '') + $options; if ($field['multiple']) { $form[$field['field_name']]['keys'] = array( '#type' => 'select', '#title' => t($field['widget']['label']), '#default_value' => $node_field['default keys'], '#multiple' => TRUE, '#options' => $options, '#required' => $field['required'], '#description' => $field['widget']['description'], ); } else { $form[$field['field_name']]['key'] = array( '#type' => 'select', '#title' => t($field['widget']['label']), '#default_value' => $node_field['default key'], '#multiple' => FALSE, '#options' => $options, '#required' => $field['required'], '#description' => $field['widget']['description'], ); } break; case 'options_buttons': if ($field['multiple']) { $form[$field['field_name']]['keys'] = array( '#type' => 'checkboxes', '#title' => t($field['widget']['label']), '#default_value' => $node_field['default keys'], '#options' => $options, '#required' => $field['required'], '#description' => $field['widget']['description'], ); } else { $form[$field['field_name']]['key'] = array( '#type' => 'radios', '#title' => t($field['widget']['label']), '#default_value' => $node_field['default key'], '#options' => $options, '#required' => $field['required'], '#description' => $field['widget']['description'], ); } break; } return $form; case 'process form values': $options = _optionwidgets_options($field); if ($field['multiple']) { $keys = $node_field['keys']; } else { $keys = array($node_field['key']); } $values = array(); foreach ($keys as $key) { if (isset($options[$key])) { $values[] = $options[$key]; } } $node_field = content_transpose_array_rows_cols(array('value' => $values)); // Remove the widget's data representation so it isn't saved. unset($node_field['keys']); unset($node_field['key']); break; } } function _optionwidgets_options($field) { $allowed_values = explode("\n", $field['allowed_values']); $allowed_values = array_map('trim', $allowed_values); $allowed_values = array_filter($allowed_values, 'strlen'); // Create an array indexed starting at 1; checkboxes cannot have 0 keys. $return_array = array(); $i = 1; foreach ($allowed_values as $value) { $return_array[$i] = $value; $i++; } return (drupal_map_assoc($return_array)); }