diff --git a/core/modules/taxonomy/taxonomy.module b/core/modules/taxonomy/taxonomy.module index 8ac2f99..7962f68 100644 --- a/core/modules/taxonomy/taxonomy.module +++ b/core/modules/taxonomy/taxonomy.module @@ -1730,17 +1730,17 @@ function taxonomy_field_presave($entity_type, $entity, $field, $instance, $langc /** * Implements hook_node_insert(). * - * We are using node hooks instead of field hooks, because we are maintaining a - * term to node relationship. For details, see http://drupal.org/node/1050466. + * This maintains the taxonomy index. The index lists all terms that are + * related to a given node entity, and is therefore maintained at the entity + * level. */ function taxonomy_node_insert($node) { // We maintain a denormalized table of term/node relationships, containing // only data for current, published nodes. $status = NULL; if (variable_get('taxonomy_maintain_index_table', TRUE)) { - // People are allowed to invoke node_save() without status and sticky, and - // in that case the original values are in $node->original. For instance, - // see NodeRevisionsTestCase(). + // If a node property is not set in the node object when node_save() is + // called, the old value from $node->original is used. if (!empty($node->original)) { $status = (int)(!empty($node->status) || (!isset($node->status) && !empty($node->original->status))); $sticky = (int)(!empty($node->sticky) || (!isset($node->sticky) && !empty($node->original->sticky))); @@ -1750,14 +1750,16 @@ function taxonomy_node_insert($node) { $sticky = (int)(!empty($node->sticky)); } } + // We only maintain the taxonomy index for unpublished nodes. if ($status) { - // Collect all the terms. + // Collect a unique list of all the term IDs. $tid_all = array(); foreach (field_info_instances('node', $node->type) as $instance) { $field_name = $instance['field_name']; $field = field_info_field($field_name); if ($field['module'] == 'taxonomy' && $field['storage']['type'] == 'field_sql_storage') { - // Same as status and sticky, the fields can be in $node->original. + // If a field value is not set in the node object when node_save() is + // called, the old value from $node->original is used. if (isset($node->{$field_name})) { $items = $node->{$field_name}; } @@ -1776,7 +1778,7 @@ function taxonomy_node_insert($node) { } } } - // Insert index for all the terms. + // Insert index entries for all the node's terms. if (!empty($tid_all)) { $query = db_insert('taxonomy_index')->fields(array('nid', 'tid', 'sticky', 'created')); foreach ($tid_all as $tid) { @@ -1796,8 +1798,7 @@ function taxonomy_node_insert($node) { * Implements hook_node_update(). */ function taxonomy_node_update($node) { - // We don't maintain index for unpublished nodes, so we always rebuild the - // index to avoid too much checking. + // Always rebuild the node's taxonomy index entries on node save. taxonomy_node_delete($node); taxonomy_node_insert($node); } diff --git a/core/modules/taxonomy/taxonomy.test b/core/modules/taxonomy/taxonomy.test index 577a6cd..d874e4f 100644 --- a/core/modules/taxonomy/taxonomy.test +++ b/core/modules/taxonomy/taxonomy.test @@ -829,26 +829,29 @@ class TaxonomyTermTestCase extends TaxonomyWebTestCase { } /** - * Tests the hook implementations that maintain the index of term tid to node - * nid works as expected. + * Tests the hook implementations that maintain the taxonomy index. */ class TaxonomyTermIndexTestCase extends TaxonomyWebTestCase { public static function getInfo() { return array( 'name' => 'Taxonomy term index', - 'description' => 'Tests the hook implementations that maintain the index of term tid to node nid works as expected.', + 'description' => 'Tests the hook implementations that maintain the taxonomy index.', 'group' => 'Taxonomy', ); } function setUp() { parent::setUp('taxonomy'); + + // Create an administrative user. $this->admin_user = $this->drupalCreateUser(array('administer taxonomy', 'bypass node access')); $this->drupalLogin($this->admin_user); - $this->vocabulary = $this->createVocabulary(); - $this->field_name_1 = drupal_strtolower($this->randomName()); + // Create a vocabulary and add two term reference fields to article nodes. + $this->vocabulary = $this->createVocabulary(); + + $this->field_name_1 = drupal_strtolower($this->randomName()); $this->field_1 = array( 'field_name' => $this->field_name_1, 'type' => 'taxonomy_term_reference', @@ -910,7 +913,7 @@ class TaxonomyTermIndexTestCase extends TaxonomyWebTestCase { } /** - * Main test function. + * Tests that the taxonomy index is maintained properly. */ function testTaxonomyIndex() { // Create terms in the vocabulary. @@ -1028,7 +1031,7 @@ class TaxonomyTermIndexTestCase extends TaxonomyWebTestCase { } /** - * Tests for multiple free-tagging fields referencing to a same vocabulary. + * Tests for multiple free-tagging fields using a single vocabulary. */ class TaxonomyTermTaggingTestCase extends TaxonomyWebTestCase { @@ -1042,8 +1045,11 @@ class TaxonomyTermTaggingTestCase extends TaxonomyWebTestCase { function setUp() { parent::setUp('taxonomy'); + // Create an administrative user. $this->admin_user = $this->drupalCreateUser(array('administer taxonomy', 'bypass node access')); $this->drupalLogin($this->admin_user); + + // Create a vocabulary and add two autocomplete fields on article nodes. $this->vocabulary = $this->createVocabulary(); $this->field_name_1 = drupal_strtolower($this->randomName()); @@ -1108,7 +1114,7 @@ class TaxonomyTermTaggingTestCase extends TaxonomyWebTestCase { } /** - * Main test function. + * Tests creation, update, and deletion of terms in autocomplete fields. */ function testTaxonomyTagging() { $term_name = $this->randomName();