summaryrefslogtreecommitdiff
path: root/modules/taxonomy/taxonomy.module
diff options
context:
space:
mode:
authorwebchick <webchick@24967.no-reply.drupal.org>2011-12-22 01:11:01 -0800
committerwebchick <webchick@24967.no-reply.drupal.org>2011-12-22 01:11:01 -0800
commit2550674cc80bc67a8efdbc645409ef0300a00266 (patch)
treee10433d5ea980053ef967baa52375c1cc280fb91 /modules/taxonomy/taxonomy.module
parent24c357b5c521ade1d9002c43167695d3f769b8fa (diff)
downloadbrdo-2550674cc80bc67a8efdbc645409ef0300a00266.tar.gz
brdo-2550674cc80bc67a8efdbc645409ef0300a00266.tar.bz2
Issue #1050466 by xjm, makara, rhayun: Fixed The taxonomy index should be maintained in a node hook, not a field hook.
Diffstat (limited to 'modules/taxonomy/taxonomy.module')
-rw-r--r--modules/taxonomy/taxonomy.module116
1 files changed, 81 insertions, 35 deletions
diff --git a/modules/taxonomy/taxonomy.module b/modules/taxonomy/taxonomy.module
index 379de71c7..891875b62 100644
--- a/modules/taxonomy/taxonomy.module
+++ b/modules/taxonomy/taxonomy.module
@@ -1724,48 +1724,75 @@ function taxonomy_field_presave($entity_type, $entity, $field, $instance, $langc
}
/**
- * Implements hook_field_insert().
+ * Implements hook_node_insert().
*/
-function taxonomy_field_insert($entity_type, $entity, $field, $instance, $langcode, &$items) {
- // 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();
- }
+function taxonomy_node_insert($node) {
+ // Add taxonomy index entries for the node.
+ taxonomy_build_node_index($node);
}
/**
- * Implements hook_field_update().
+ * Builds and inserts taxonomy index entries for a given node.
+ *
+ * The index lists all terms that are related to a given node entity, and is
+ * therefore maintained at the entity level.
+ *
+ * @param $node
+ * The node object.
*/
-function taxonomy_field_update($entity_type, $entity, $field, $instance, $langcode, &$items) {
- if (variable_get('taxonomy_maintain_index_table', TRUE) && $field['storage']['type'] == 'field_sql_storage' && $entity_type == 'node') {
- $first_call = &drupal_static(__FUNCTION__, array());
-
- // We don't maintain data for old revisions, so clear all previous values
- // from the table. Since this hook runs once per field, per object, make
- // sure we only wipe values once.
- if (!isset($first_call[$entity->nid])) {
- $first_call[$entity->nid] = FALSE;
- db_delete('taxonomy_index')->condition('nid', $entity->nid)->execute();
+function taxonomy_build_node_index($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)) {
+ // 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)));
+ }
+ else {
+ $status = (int)(!empty($node->status));
+ $sticky = (int)(!empty($node->sticky));
}
- // Only save data to the table if the node is published.
- if ($entity->status) {
+ }
+ // We only maintain the taxonomy index for published nodes.
+ if ($status) {
+ // Collect a unique list of all the term IDs from all node fields.
+ $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') {
+ // 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};
+ }
+ elseif (isset($node->original->{$field_name})) {
+ $items = $node->original->{$field_name};
+ }
+ else {
+ continue;
+ }
+ foreach (field_available_languages('node', $field) as $langcode) {
+ if (!empty($items[$langcode])) {
+ foreach ($items[$langcode] as $item) {
+ $tid_all[$item['tid']] = $item['tid'];
+ }
+ }
+ }
+ }
+ }
+ // 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 ($items as $item) {
+ foreach ($tid_all as $tid) {
$query->values(array(
- 'nid' => $entity->nid,
- 'tid' => $item['tid'],
- 'sticky' => $entity->sticky,
- 'created' => $entity->created,
+ 'nid' => $node->nid,
+ 'tid' => $tid,
+ 'sticky' => $sticky,
+ 'created' => $node->created,
));
}
$query->execute();
@@ -1774,11 +1801,30 @@ function taxonomy_field_update($entity_type, $entity, $field, $instance, $langco
}
/**
+ * Implements hook_node_update().
+ */
+function taxonomy_node_update($node) {
+ // Always rebuild the node's taxonomy index entries on node save.
+ taxonomy_delete_node_index($node);
+ taxonomy_build_node_index($node);
+}
+
+/**
* Implements hook_node_delete().
*/
function taxonomy_node_delete($node) {
+ // Clean up the {taxonomy_index} table when nodes are deleted.
+ taxonomy_delete_node_index($node);
+}
+
+/**
+ * Deletes taxonomy index entries for a given node.
+ *
+ * @param $node
+ * The node object.
+ */
+function taxonomy_delete_node_index($node) {
if (variable_get('taxonomy_maintain_index_table', TRUE)) {
- // Clean up the {taxonomy_index} table when nodes are deleted.
db_delete('taxonomy_index')->condition('nid', $node->nid)->execute();
}
}