Index: modules/field/modules/options/options.module =================================================================== RCS file: /cvs/drupal/drupal/modules/field/modules/options/options.module,v retrieving revision 1.7 diff -u -p -r1.7 options.module --- modules/field/modules/options/options.module 28 May 2009 16:44:06 -0000 1.7 +++ modules/field/modules/options/options.module 14 Jun 2009 07:59:48 -0000 @@ -44,7 +44,7 @@ function options_field_widget_info() { return array( 'options_select' => array( 'label' => t('Select list'), - 'field types' => array('list', 'list_boolean', 'list_text', 'list_number'), + 'field types' => array('list', 'list_boolean', 'list_text', 'list_number', 'term'), 'behaviors' => array( 'multiple values' => FIELD_BEHAVIOR_CUSTOM, 'default value' => FIELD_BEHAVIOR_DEFAULT, @@ -52,7 +52,7 @@ function options_field_widget_info() { ), 'options_buttons' => array( 'label' => t('Check boxes/radio buttons'), - 'field types' => array('list', 'list_boolean', 'list_text', 'list_number'), + 'field types' => array('list', 'list_boolean', 'list_text', 'list_number', 'term'), 'behaviors' => array( 'multiple values' => FIELD_BEHAVIOR_CUSTOM, 'default value' => FIELD_BEHAVIOR_DEFAULT, Index: modules/taxonomy/term.info =================================================================== RCS file: modules/taxonomy/term.info diff -N modules/taxonomy/term.info --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ modules/taxonomy/term.info 14 Jun 2009 07:59:51 -0000 @@ -0,0 +1,6 @@ +; $Id$ +name = Term +description = Defines taxonomy term field types. Use with Options to create selection lists. +package = Core - fields +core = 7.x +files[]=term.module Index: modules/taxonomy/term.module =================================================================== RCS file: modules/taxonomy/term.module diff -N modules/taxonomy/term.module --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ modules/taxonomy/term.module 14 Jun 2009 07:59:52 -0000 @@ -0,0 +1,157 @@ + array( + 'arguments' => array('element' => NULL), + ), + 'field_formatter_term_plain' => array( + 'arguments' => array('element' => NULL), + ), + ); +} + +/** + * Implement hook_field_info(). + */ +function term_field_info() { + return array( + 'term' => array( + 'label' => t('Term'), + 'description' => t('This field stores keys from key/value terms of allowed numbers where the stored numeric key has significance and must be preserved, i.e. \'Lifetime in days\': 1|1 day, 7|1 week, 31|1 month.'), + 'default_widget' => 'options_select', + 'default_formatter' => 'term_default', + 'settings' => array('vid' => 0), + ), + ); +} + +/** + * Implement hook_field_schema(). + */ +function term_field_schema($field) { + $columns = array( + 'value' => array( + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => FALSE, + ), + ); + return array( + 'columns' => $columns, + 'indexes' => array( + 'value' => array('value'), + ), + ); +} + +/** + * Implement hook_field_validate(). + * + * Possible error codes: + * - 'term_illegal_value': The value is not part of the list of allowed values. + */ +function term_field_validate($obj_type, $object, $field, $instance, $items, &$errors) { + $allowed_values = term_allowed_values($field); + foreach ($items as $delta => $item) { + if (!empty($item['value'])) { + if (count($allowed_values) && !array_key_exists($item['value'], $allowed_values)) { + $errors[$field['field_name']][$delta][] = array( + 'error' => 'term_illegal_value', + 'message' => t('%name: illegal value.', array('%name' => t($instance['label']))), + ); + } + } + } +} + +/** + * Implement hook_field_is_empty(). + */ +function term_field_is_empty($item, $field) { + if (empty($item['value']) && (string)$item['value'] !== '0') { + return TRUE; + } + return FALSE; +} + +/** + * Implement hook_field_formatter_info(). + */ +function term_field_formatter_info() { + return array( + 'term_default' => array( + 'label' => t('Default'), + 'field types' => array('term'), + 'behaviors' => array( + 'multiple values' => FIELD_BEHAVIOR_DEFAULT, + ), + ), + 'term_plain' => array( + 'label' => t('Plain text'), + 'field types' => array('term'), + 'behaviors' => array( + 'multiple values' => FIELD_BEHAVIOR_DEFAULT, + ), + ), + ); +} + +/** + * Theme function for 'default' term field formatter. + */ +function theme_field_formatter_term_default($element) { + $field = field_info_field($element['#field_name']); + if (($allowed_values = term_allowed_values($field)) && isset($allowed_values[$element['#item']['value']])) { + $term = taxonomy_term_load($element['#item']['value']); + return l($term->name, taxonomy_term_path($term)); + } + // If no match was found in allowed values, fall back to the key. + return 'invalid - '. $element['#item']['value']; +} + +/** + * Theme function for 'default' term field formatter. + */ +function theme_field_formatter_term_plain($element) { + $field = field_info_field($element['#field_name']); + if (($allowed_values = term_allowed_values($field)) && isset($allowed_values[$element['#item']['value']])) { + $term = taxonomy_term_load($element['#item']['value']); + return $term->name; + } + // If no match was found in allowed values, fall back to the key. + return 'invalid - '. $element['#item']['value']; +} + +/** + * Create an array of the allowed values for this field. + * + * Call the allowed_values_function to retrieve the allowed + * values array. + * + * This function should imitate the features of _taxonomy_term_select + * + * TODO deal with excluded tids? + * TODO support scope limiting to a particular subtree of the vocabulary + * + */ +function term_allowed_values($field) { + $tree = taxonomy_get_tree($field['settings']['vid']); + $options = array(); + + if ($tree) { + foreach ($tree as $term) { + $options[$term->tid] = str_repeat('-', $term->depth) . $term->name; + } + } + return $options; +}