diff --git a/modules/taxonomy/taxonomy.module b/modules/taxonomy/taxonomy.module index df5ce1bb5072ee9aaf396520ded2fa177d968194..bee1627fffae3b750d899a540edc5ed5bcddcf25 100644 --- a/modules/taxonomy/taxonomy.module +++ b/modules/taxonomy/taxonomy.module @@ -472,50 +472,52 @@ function taxonomy_term_save($term) { field_attach_presave('taxonomy_term', $term); - if (!empty($term->tid) && $term->name) { - $status = drupal_write_record('taxonomy_term_data', $term, 'tid'); - field_attach_update('taxonomy_term', $term); - module_invoke_all('taxonomy_term_update', $term); - entity_invoke('update', 'taxonomy_term', $term); - } - else { + if (empty($term->tid)) { $status = drupal_write_record('taxonomy_term_data', $term); field_attach_insert('taxonomy_term', $term); module_invoke_all('taxonomy_term_insert', $term); entity_invoke('insert', 'taxonomy_term', $term); + if (!isset($term->parent)) { + $term->parent = array(0); + } } - - db_delete('taxonomy_term_hierarchy') - ->condition('tid', $term->tid) - ->execute(); - - if (!isset($term->parent) || empty($term->parent)) { - $term->parent = array(0); - } - if (!is_array($term->parent)) { - $term->parent = array($term->parent); + else { + $status = drupal_write_record('taxonomy_term_data', $term, 'tid'); + field_attach_update('taxonomy_term', $term); + module_invoke_all('taxonomy_term_update', $term); + entity_invoke('update', 'taxonomy_term', $term); + if (isset($term->parent)) { + db_delete('taxonomy_term_hierarchy') + ->condition('tid', $term->tid) + ->execute(); + } } - $query = db_insert('taxonomy_term_hierarchy') - ->fields(array('tid', 'parent')); - if (is_array($term->parent)) { - foreach ($term->parent as $parent) { - if (is_array($parent)) { - foreach ($parent as $tid) { + if (isset($term->parent)) { + if (!is_array($term->parent)) { + $term->parent = array($term->parent); + } + $query = db_insert('taxonomy_term_hierarchy') + ->fields(array('tid', 'parent')); + if (is_array($term->parent)) { + foreach ($term->parent as $parent) { + if (is_array($parent)) { + foreach ($parent as $tid) { + $query->values(array( + 'tid' => $term->tid, + 'parent' => $tid + )); + } + } + else { $query->values(array( 'tid' => $term->tid, - 'parent' => $tid + 'parent' => $parent )); } } - else { - $query->values(array( - 'tid' => $term->tid, - 'parent' => $parent - )); - } } + $query->execute(); } - $query->execute(); cache_clear_all(); taxonomy_terms_static_reset(); diff --git a/modules/taxonomy/taxonomy.test b/modules/taxonomy/taxonomy.test index b27509b1fe5dfba8b80c7fabf28350a53e1e93da..4078d15f3e8d404241815c977b0068279aeb12ac 100644 --- a/modules/taxonomy/taxonomy.test +++ b/modules/taxonomy/taxonomy.test @@ -374,6 +374,12 @@ class TaxonomyTermTestCase extends TaxonomyWebTestCase { $this->assertTrue(isset($children[$term2->tid]), t('Child found correctly.')); $this->assertTrue(isset($parents[$term1->tid]), t('Parent found correctly.')); + // Load and save a term, confirming that parents are still set. + $term = taxonomy_term_load($term2->tid); + taxonomy_term_save($term); + $parents = taxonomy_get_parents($term2->tid); + $this->assertTrue(isset($parents[$term1->tid]), t('Parent found correctly.')); + // Create a third term and save this as a parent of term2. $term3 = $this->createTerm($this->vocabulary); $term2->parent = array($term1->tid, $term3->tid);