You are here

function revisioning_update_taxonomy_index in Revisioning 7

Same name and namespace in other branches
  1. 8 revisioning.taxonomy.inc \revisioning_update_taxonomy_index()

Updates the {taxonomy_index} table.

Assures that when content with terms is shown in a View, the correct terms (those belonging to the current, rather than latest revision) are displayed, regardless of whether the content is published or not.

Revisioning's weight has been set higher than Taxonomy's so this is called after taxonomy_node_insert() or taxonomy_node_update(). Called when creating, updating or (un)publishing a node.

Parameters

object $node: the node object

bool $show_unpublished_content_terms: whether to show unpublished content terms

5 calls to revisioning_update_taxonomy_index()
revisioning_admin_configure_form_submit in ./revisioning.admin.inc
Execute the revisioning_admin_configure_form.
revisioning_install in ./revisioning.install
Implements hook_install().
revisioning_node_insert in ./revisioning.module
Implements hook_node_insert().
revisioning_node_update in ./revisioning.module
Implements hook_node_update().
revisioning_uninstall in ./revisioning.install
Implements hook_uninstall().

File

./revisioning.taxonomy.inc, line 25
Code required only when the Taxonomy module is enabled.

Code

function revisioning_update_taxonomy_index($node, $show_unpublished_content_terms = TRUE) {
  if (module_exists('taxonomy') && variable_get('taxonomy_maintain_index_table', TRUE)) {

    // First, delete all term id's associated with this node.
    db_delete('taxonomy_index')
      ->condition('nid', $node->nid)
      ->execute();

    // Use of taxonomy_delete_node_index($node); requires core 7.12 or later
    // Then add terms if node is published OR terms are requested explicitly.
    if ($node->status || $show_unpublished_content_terms) {
      $vid = empty($node->revision_moderation) || empty($node->current_revision_id) ? $node->vid : $node->current_revision_id;

      // Find all the terms attached to this node revision.
      $tids = revisioning_get_tids($vid);
      if (!empty($tids)) {

        // Core, via taxonomy_node_update(), only does this when node is
        // published, but then we can't see the terms of unpublished content
        // in Views!
        $query = db_insert('taxonomy_index')
          ->fields(array(
          'nid',
          'tid',
          'sticky',
          'created',
        ));
        foreach ($tids as $tid) {
          $query
            ->values(array(
            'nid' => $node->nid,
            'tid' => $tid,
            // See [#1417658].
            'sticky' => empty($node->sticky) ? 0 : 1,
            'created' => $node->created,
          ));
        }
        $query
          ->execute();
      }
    }
  }
}