Skip to content

Instantly share code, notes, and snippets.

@makara
Created July 14, 2011 09:08
Show Gist options
  • Save makara/1082140 to your computer and use it in GitHub Desktop.
Save makara/1082140 to your computer and use it in GitHub Desktop.
RW v2
diff --git modules/taxonomy/taxonomy.module modules/taxonomy/taxonomy.module
index dc2847d..64f9083 100644
--- modules/taxonomy/taxonomy.module
+++ modules/taxonomy/taxonomy.module
@@ -1689,16 +1689,7 @@ function taxonomy_field_insert($entity_type, $entity, $field, $instance, $langco
// We maintain a denormalized table of term/node relationships, containing
// only data for current, published nodes.
if (variable_get('taxonomy_maintain_index_table', TRUE) && $field['storage']['type'] == 'field_sql_storage' && $entity_type == 'node' && $entity->status) {
- $query = db_insert('taxonomy_index')->fields(array('nid', 'tid', 'sticky', 'created', ));
- foreach ($items as $item) {
- $query->values(array(
- 'nid' => $entity->nid,
- 'tid' => $item['tid'],
- 'sticky' => $entity->sticky,
- 'created' => $entity->created,
- ));
- }
- $query->execute();
+ _taxonomy_field_save_index($entity, $items);
}
}
@@ -1718,21 +1709,45 @@ function taxonomy_field_update($entity_type, $entity, $field, $instance, $langco
}
// Only save data to the table if the node is published.
if ($entity->status) {
- $query = db_insert('taxonomy_index')->fields(array('nid', 'tid', 'sticky', 'created'));
- foreach ($items as $item) {
- $query->values(array(
- 'nid' => $entity->nid,
- 'tid' => $item['tid'],
- 'sticky' => $entity->sticky,
- 'created' => $entity->created,
- ));
- }
- $query->execute();
+ _taxonomy_field_save_index($entity, $items);
}
}
}
/**
+ * Save index.
+ */
+function _taxonomy_field_save_index($entity, $items) {
+ if (empty($entity->nid) || empty($items)) {
+ return;
+ }
+ $tids = array();
+ foreach ($items as $item) {
+ $tids[$item['tid']] = $item['tid'];
+ }
+ if (empty($tids)) {
+ return;
+ }
+ // Avoid duplicates.
+ $indexed_tids = db_query("SELECT tid FROM {taxonomy_index} WHERE nid = :nid", array(':nid' => $entity->nid))->fetchCol();
+ $tids = array_diff($tids, $indexed_tids);
+ if (empty($tids)) {
+ return;
+ }
+ // Insert.
+ $query = db_insert('taxonomy_index')->fields(array('nid', 'tid', 'sticky', 'created', ));
+ foreach ($tids as $tid) {
+ $query->values(array(
+ 'nid' => $entity->nid,
+ 'tid' => $tid,
+ 'sticky' => $entity->sticky,
+ 'created' => $entity->created,
+ ));
+ }
+ $query->execute();
+}
+
+/**
* Implements hook_node_delete().
*/
function taxonomy_node_delete($node) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment