diff --git a/entityreference/plugins/behavior/EntityReferenceBehavior_TaxonomyIndex.class.php b/entityreference/plugins/behavior/EntityReferenceBehavior_TaxonomyIndex.class.php index 43ac693..872b885 100644 --- a/entityreference/plugins/behavior/EntityReferenceBehavior_TaxonomyIndex.class.php +++ b/entityreference/plugins/behavior/EntityReferenceBehavior_TaxonomyIndex.class.php @@ -44,7 +44,7 @@ class EntityReferenceBehavior_TaxonomyIndex extends EntityReference_BehaviorHand return; } - $this->buildNodeIndex($entity); + $this->buildNodeIndex($entity, $field); } /** @@ -57,7 +57,7 @@ class EntityReferenceBehavior_TaxonomyIndex extends EntityReference_BehaviorHand return; } - $this->buildNodeIndex($entity); + $this->buildNodeIndex($entity, $field); } /** @@ -66,12 +66,16 @@ class EntityReferenceBehavior_TaxonomyIndex extends EntityReference_BehaviorHand * The index lists all terms that are related to a given node entity, and is * therefore maintained at the entity level. * + * Called once for each taxonomy term entityreference field on the node. + * * @param $node * The node object. + * @param $field + * The field object * * @see taxonomy_build_node_index() */ - protected function buildNodeIndex($node) { + protected function buildNodeIndex($node, $field) { // We maintain a denormalized table of term/node relationships, containing // only data for current, published nodes. $status = NULL; @@ -89,74 +93,54 @@ class EntityReferenceBehavior_TaxonomyIndex extends EntityReference_BehaviorHand } // 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 (!empty($field['settings']['target_type']) && $field['settings']['target_type'] == 'taxonomy_term' && $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['target_id']] = $item['target_id']; - } + // If a field value is not set in the node object when node_save() is + // called, the old value from $node->original is used. + $field_name = $field['field_name']; + if (isset($node->{$field_name})) { + $items = $node->{$field_name}; + } + elseif (isset($node->original->{$field_name})) { + $items = $node->original->{$field_name}; + } + + // Insert only if items exist + if (!empty($items) && $field['storage']['type'] == 'field_sql_storage') { + + // Collect a unique list of all the term IDs from this field. + $tids = array(); + foreach (field_available_languages('node', $field) as $langcode) { + if (!empty($items[$langcode])) { + foreach ($items[$langcode] as $item) { + $tids[$item['target_id']] = $item['target_id']; } } } - + // Re-calculate the terms added in taxonomy_build_node_index() so // we can optimize database queries. - $original_tid_all = array(); - 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) { - $original_tid_all[$item['tid']] = $item['tid']; - } - } + $query = db_select('taxonomy_index', 't'); + $query->condition('nid', $node->nid); + $query->addField('t', 'tid'); + $existing_tids = $query->execute()->fetchCol(); + + // Insert index entries for field terms, that were not + // already inserted in taxonomy_build_node_index(). + $tids = array_diff($tids, $existing_tids); + + // Insert index entries for field terms. + if (!empty($tids)) { + $query = db_insert('taxonomy_index')->fields(array('nid', 'tid', 'sticky', 'created')); + foreach ($tids as $tid) { + $query->values(array( + 'nid' => $node->nid, + 'tid' => $tid, + 'sticky' => $sticky, + 'created' => $node->created, + )); } + $query->execute(); } } - - // Insert index entries for all the node's terms, that were not - // already inserted in taxonomy_build_node_index(). - $tid_all = array_diff($tid_all, $original_tid_all); - - // 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) { - $query->values(array( - 'nid' => $node->nid, - 'tid' => $tid, - 'sticky' => $sticky, - 'created' => $node->created, - )); - } - $query->execute(); - } } }