Index: modules/taxonomy/taxonomy.module =================================================================== RCS file: /cvs/drupal/drupal/modules/taxonomy/taxonomy.module,v retrieving revision 1.485 diff -u -p -r1.485 taxonomy.module --- modules/taxonomy/taxonomy.module 8 Jul 2009 07:18:07 -0000 1.485 +++ modules/taxonomy/taxonomy.module 9 Jul 2009 16:41:13 -0000 @@ -62,6 +62,12 @@ function taxonomy_theme() { 'taxonomy_overview_terms' => array( 'arguments' => array('form' => array()), ), + 'field_formatter_term_default' => array( + 'arguments' => array('element' => NULL), + ), + 'field_formatter_term_plain' => array( + 'arguments' => array('element' => NULL), + ), ); } @@ -409,6 +415,7 @@ function taxonomy_term_save($term) { } else { $status = drupal_write_record('taxonomy_term_data', $term); + _taxonomy_clean_field_cache($term); field_attach_insert('taxonomy_term', $term); module_invoke_all('taxonomy_term_update', $term); } @@ -536,6 +543,7 @@ function taxonomy_term_delete($tid) { ->execute(); field_attach_delete('taxonomy_term', $term); + _taxonomy_clean_field_cache($term); module_invoke_all('taxonomy_term_delete', $term); } @@ -1816,6 +1824,223 @@ function taxonomy_hook_info() { } /** + * Implement hook_field_info(). + */ +function taxonomy_field_info() { + return array( + 'term' => array( + 'label' => t('Term'), + 'description' => t('This field represents a taxonomy term reference.'), + 'default_widget' => 'options_select', + 'default_formatter' => 'term_default', + 'settings' => array( + 'allowed_values' => array( + array( + 'vid' => '0', + 'parent' => '0', + ), + ), + ), + ), + ); +} + +/** + * Implement hook_field_widget_info_alter(). + */ +function taxonomy_field_widget_info_alter(&$info) { + $info['options_select']['field types'][] = 'term'; + $info['options_buttons']['field types'][] = 'term'; +} + +/** + * Implement hook_field_schema(). + */ +function taxonomy_field_schema($field) { + return array( + 'columns' => array( + 'value' => array( + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => FALSE, + ), + ), + '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 taxonomy_field_validate($obj_type, $object, $field, $instance, $items, &$errors) { + $allowed_values = taxonomy_allowed_values($field); + foreach ($items as $delta => $item) { + if (!empty($item['value'])) { + if (!isset($allowed_values[$item['value']])) { + $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 taxonomy_field_is_empty($item, $field) { + if (empty($item['value']) && (string) $item['value'] !== '0') { + return TRUE; + } + return FALSE; +} + +/** + * Implement hook_field_formatter_info(). + */ +function taxonomy_field_formatter_info() { + return array( + 'term_default' => array( + 'label' => t('Link'), + '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) { + $term = $element['#item']['term']; + return l($term->name, taxonomy_term_path($term)); +} + +/** + * Theme function for 'plain' term field formatter. + */ +function theme_field_formatter_term_plain($element) { + $term = $element['#item']['term']; + return $term->name; +} + +/** + * Create an array of the allowed values for this field. + * + * Call the field's allowed_values function to retrieve the allowed + * values array. + * + * This function should imitate the features of _taxonomy_term_select + */ +function taxonomy_allowed_values($field) { + $options = array(); + foreach ($field['settings']['allowed_values'] as $tree) { + $terms = taxonomy_get_tree($tree['vid'], $tree['parent']); + if ($terms) { + foreach ($terms as $term) { + $options[$term->tid] = str_repeat('-', $term->depth) . $term->name; + } + } + } + return $options; +} + +/* + * Implement hook_field_load(). + * + * This preloads all taxonomy terms for a given object at once + * and unsets values for invalid terms which do not exist. + */ +function taxonomy_field_load($obj_type, $objects, $field, $instances, &$items, $age) { + $tids = array(); + + foreach ($objects as $id => $object) { + foreach ($items[$id] as $delta => $item) { + $tids[$item['value']] = $item['value']; + } + } + if ($tids) { + $terms = array(); + $query = db_select('taxonomy_term_data', 't'); + $taxonomy_term_data = drupal_schema_fields_sql('taxonomy_term_data'); + $query->fields('t', $taxonomy_term_data); + $query->condition('t.tid', $tids, 'IN'); + $query->addTag('term_access'); + $terms = $query->execute()->fetchAllAssoc('tid'); + foreach ($objects as $id => $object) { + foreach ($items[$id] as $delta => $item) { + if (isset($terms[$item['value']])) { + $items[$id][$delta]['term'] = $terms[$item['value']]; + } + else { + unset($items[$id][$delta]); + } + } + } + } +} + +/** + * Helper function that clears field cache when terms are updated or deleted + * + * TODO figure out how to reverse the order of cacheable object type checking + * and the loading of the objects see #50 + */ +function _taxonomy_clean_field_cache($term) { + $cids = array(); + + // Determine object types that are not cacheable. + $obj_types = array(); + foreach (field_info_fieldable_types() as $obj_type => $info) { + if (!$info['cacheable']) { + $obj_types[] = $obj_type; + } + } + + // Load info for all taxonomy term fields. + $fields = field_read_fields(array('type' => 'term')); + foreach ($fields as $field_name => $field) { + + // Assemble an array of vocabulary IDs that are used in this field. + foreach ($field['settings']['allowed_values'] as $tree) { + $vids[$tree['vid']] = $tree['vid']; + } + + // Check this term's vocabulary against those used for the field's options. + if (in_array($term->vid, $vids)) { + $conditions = array(array('value', $term->tid)); + if ($obj_types) { + $conditions[] = array('type', $obj_types, 'NOT IN'); + } + $results = field_attach_query($field['field_name'], $conditions, FIELD_QUERY_NO_LIMIT); + foreach ($results as $obj_type => $objects) { + foreach (array_keys($objects) as $id) { + $cids[] = "field:$obj_type:$id"; + } + } + } + } + if ($cids) { + cache_clear_all($cids, 'cache_field'); + } +} + +/** * Title callback for term pages. * * @param $term Index: modules/taxonomy/taxonomy.test =================================================================== RCS file: /cvs/drupal/drupal/modules/taxonomy/taxonomy.test,v retrieving revision 1.38 diff -u -p -r1.38 taxonomy.test --- modules/taxonomy/taxonomy.test 27 Jun 2009 19:49:07 -0000 1.38 +++ modules/taxonomy/taxonomy.test 9 Jul 2009 16:41:14 -0000 @@ -727,3 +727,145 @@ class TaxonomyHooksTestCase extends Taxo $this->assertFalse($antonyms, t('The antonyms were deleted from the database.')); } } + +class TaxonomyFieldTestCase extends TaxonomyWebTestCase { + protected $instance; + protected $vocabulary; + + public static function getInfo() { + return array( + 'name' => t('Term Field'), + 'description' => t("Test the creation of term fields."), + 'group' => t('Taxonomy') + ); + } + + function setUp() { + parent::setUp('field_test'); + + $web_user = $this->drupalCreateUser(array('access field_test content', 'administer field_test content', 'administer taxonomy')); + $this->drupalLogin($web_user); + + $this->vocabulary = $this->createVocabulary(); + } + + /** + * Test term field validation. + */ + function testTermFieldValidation() { + // Create a field with settings to validate. + $this->field = array( + 'field_name' => drupal_strtolower($this->randomName()), + 'type' => 'term', + 'settings' => array( + 'allowed_values' => array( + array( + 'vid' => $this->vocabulary->vid, + 'parent' => '0', + ), + ), + ) + ); + field_create_field($this->field); + $this->instance = array( + 'field_name' => $this->field['field_name'], + 'bundle' => FIELD_TEST_BUNDLE, + 'widget' => array( + 'type' => 'options_select', + ), + 'display' => array( + 'full' => array( + 'type' => 'term_default', + ), + ), + ); + field_create_instance($this->instance); + + // Test valid and invalid values with field_attach_validate(). + $entity = field_test_create_stub_entity(0, 0, FIELD_TEST_BUNDLE); + $term = $this->createTerm($this->vocabulary); + $entity->{$this->field['field_name']}[0]['value'] = $term->tid; + field_attach_validate('test_entity', $entity); + try { + $this->assertTrue($entity->{$this->field['field_name']}[0]['value'] == $term->tid, t('Correct term does not cause validation error')); + } + catch (FieldValidationException $e) { + $this->assertTrue($entity->{$this->field['field_name']}[0]['value'] != $term->tid, t('Term from wrong vocabulary does not cause validation error')); + } + + $entity = field_test_create_stub_entity(0, 0, FIELD_TEST_BUNDLE); + $bad_term = $this->createTerm($this->createVocabulary()); + $entity->{$this->field['field_name']}[0]['value'] = $bad_term->tid; + try { + field_attach_validate('test_entity', $entity); + } + catch (FieldValidationException $e) { + $this->assertTrue($this->field['settings']['allowed_values'][0]['vid'] != $bad_term->vid, t('Wrong term causes validation error')); + } + } + + /** + * Test widgets. + */ + function testTermfieldWidgets() { + $this->_testTermfieldWidgets('term', 'options_select'); + } + + /** + * Helper function for testTermfieldWidgets(). + */ + function _testTermfieldWidgets($field_type, $widget_type) { + // Setup a field and instance + $entity_type = 'test_entity'; + $this->field_name = drupal_strtolower($this->randomName()); + $this->field = array( + 'field_name' => $this->field_name, + 'type' => $field_type, + 'settings' => array( + 'allowed_values' => array( + array( + 'vid' => $this->vocabulary->vid, + 'parent' => '0', + ), + ), + ) + ); + field_create_field($this->field); + $this->instance = array( + 'field_name' => $this->field_name, + 'bundle' => FIELD_TEST_BUNDLE, + 'label' => $this->randomName() . '_label', + 'widget' => array( + 'type' => $widget_type, + ) + ); + field_create_instance($this->instance); + + // create a term in the vocabulary + $term = $this->createTerm($this->vocabulary); + + // Display creation form. + $this->drupalGet('test-entity/add/test-bundle'); + $this->assertFieldByName($this->field_name . '[value]', '', t('Widget is displayed')); + + // Submit with some value. + $edit = array( + $this->field_name . '[value]' => array($term->tid), + ); + $this->drupalPost(NULL, $edit, t('Save')); + preg_match('|test-entity/(\d+)/edit|', $this->url, $match); + $id = $match[1]; + $this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)), t('Entity was created')); + + // Display the object. + $entity = field_test_entity_load($id); + $entity->content = field_attach_view($entity_type, $entity); + $this->content = drupal_render($entity->content); + $this->assertText($term->name, t('Term name is displayed')); + } + + // Test formatters. + /** + * + */ +}