protected function EntityReferenceBehavior_TaxonomyIndex::buildNodeIndex in Entity reference 7
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.
Parameters
$node: The node object.
See also
2 calls to EntityReferenceBehavior_TaxonomyIndex::buildNodeIndex()
- EntityReferenceBehavior_TaxonomyIndex::entityPostInsert in plugins/
behavior/ EntityReferenceBehavior_TaxonomyIndex.class.php - Overrides EntityReference_BehaviorHandler_Abstract::entityPostInsert().
- EntityReferenceBehavior_TaxonomyIndex::entityPostUpdate in plugins/
behavior/ EntityReferenceBehavior_TaxonomyIndex.class.php - Overrides EntityReference_BehaviorHandler_Abstract::entityPostUpdate().
File
- plugins/
behavior/ EntityReferenceBehavior_TaxonomyIndex.class.php, line 74 - CTools plugin class for the taxonomy-index behavior.
Class
- EntityReferenceBehavior_TaxonomyIndex
- Extends an entityreference field to maintain its references to taxonomy terms in the {taxonomy_index} table.
Code
protected function buildNodeIndex($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));
}
}
// 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'];
}
}
}
}
// 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'];
}
}
}
}
}
// 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, preventing duplicates.
if (!empty($tid_all)) {
foreach ($tid_all as $tid) {
$row = array(
'nid' => $node->nid,
'tid' => $tid,
'sticky' => $sticky,
'created' => $node->created,
);
$query = db_merge('taxonomy_index')
->key($row)
->fields($row);
$query
->execute();
}
}
}
}