diff --git a/taxonomy_vocab_relate.module b/taxonomy_vocab_relate.module index 3af71b7..c91bc58 100644 --- a/taxonomy_vocab_relate.module +++ b/taxonomy_vocab_relate.module @@ -36,6 +36,22 @@ function taxonomy_vocab_relate_form_alter(&$form, $form_state, $form_id) { $form['advanced']['relations_'. $rvid] = _taxonomy_term_select(t('Related terms in ') . $vocab_name, 'relations', array_keys(taxonomy_get_related($form['tid']['#value'])), $rvid, NULL, 1, '<'. t('none') .'>', array($edit['tid'])); $form['advanced']['relations_'. $rvid]['#weight'] = -14; } + + // Find any terms from other vocabularies that are related to this one, and cache + // them so that they can be re-saved in taxonomy_vocab_relate_term_submit() + // This needs to be done because taxonomy_save_term() will delete relationships + // for the term whether it is stored as tid1 or tid2. + // Note: this only retrieves relationships from terms in different vocabularies. + if (empty($form['#opposing_related_terms'])) { + $result = db_query("SELECT tr.tid1, td.vid + FROM {term_relation} tr + LEFT JOIN {term_data} td on td.tid = tr.tid1 + WHERE tr.tid2=%d AND td.vid != %d", $form['tid']['#value'], $vid); + $form['#opposing_related_terms'] = array(); + while ($row = db_fetch_object($result)) { + $form['#opposing_related_terms'][] = $row->tid1; + } + } } } @@ -79,6 +95,17 @@ function taxonomy_vocab_relate_term_submit(&$form, $form_state) { } } } + + // Re-save relationships where another term has a relationship to this one + // The #opposing_related_terms array is set above in taxonomy_vocab_relate_form_alter() + // And again, this is necessary because taxonomy_save_term() will delete the + // relationship regardless of which side the current term is on, and only re-save + // relationships 'from' the term being saved + if (!empty($form['#opposing_related_terms']) && is_array($form['#opposing_related_terms'])) { + foreach ($form['#opposing_related_terms'] as $tid1) { + db_query('INSERT INTO {term_relation} (tid1, tid2) VALUES (%d, %d)', $tid1, $form_state['values']['tid']); + } + } } /**