diff --git a/core/modules/taxonomy/taxonomy.install b/core/modules/taxonomy/taxonomy.install index 54a84d5ac2..7529e67265 100644 --- a/core/modules/taxonomy/taxonomy.install +++ b/core/modules/taxonomy/taxonomy.install @@ -186,3 +186,45 @@ function taxonomy_update_8601() { return t('The publishing status field has been added to taxonomy terms.'); } + +/** + * Add unpublished nodes to the taxonomy index table. + */ +function taxonomy_update_8602(&$sandbox) { + if (!isset($sandbox['total'])) { + // Initialize state for future calls. + $sandbox['last'] = 0; + $sandbox['count'] = 0; + + $query = db_select('node_field_data', 'n') + ->condition('n.status', NODE_NOT_PUBLISHED); + $sandbox['total'] = $query->countQuery()->execute()->fetchField(); + } + + if ($sandbox['total']) { + // Operate on every unpublished node, in batches. + $batch_size = 100; + $query = db_select('node_field_data', 'n'); + $query + ->fields('n', array('nid')) + ->condition('n.nid', $sandbox['last'], '>') + ->condition('n.status', NODE_NOT_PUBLISHED) + ->orderBy('n.nid', 'ASC') + ->range(0, $batch_size); + $records = $query->execute(); + // Build the taxonomy index for each node. + foreach ($records as $record) { + $node = node_load($record->nid); + taxonomy_build_node_index($node); + $sandbox['last'] = $record->nid; + } + $sandbox['count'] += $batch_size; + } + // Finish after all the unpublished nodes have been processed. + if ($sandbox['count'] < $sandbox['total']) { + $sandbox['#finished'] = FALSE; + } + else { + $sandbox['#finished'] = TRUE; + } +} diff --git a/core/modules/taxonomy/taxonomy.module b/core/modules/taxonomy/taxonomy.module index 179487d3f0..ee2446ec7a 100644 --- a/core/modules/taxonomy/taxonomy.module +++ b/core/modules/taxonomy/taxonomy.module @@ -525,10 +525,11 @@ function taxonomy_build_node_index($node) { return; } - $status = $node->isPublished(); - $sticky = (int) $node->isSticky(); - // We only maintain the taxonomy index for published nodes. - if ($status && $node->isDefaultRevision()) { + // We only maintain the taxonomy index for the default node revision. + if ($node->isDefaultRevision()) { + $status = (int) $node->isPublished(); + $sticky = (int) $node->isSticky(); + // Collect a unique list of all the term IDs from all node fields. $tid_all = []; $entity_reference_class = 'Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem'; @@ -550,7 +551,7 @@ function taxonomy_build_node_index($node) { if (!empty($tid_all)) { foreach ($tid_all as $tid) { db_merge('taxonomy_index') - ->key(['nid' => $node->id(), 'tid' => $tid, 'status' => $node->isPublished()]) + ->key(['nid' => $node->id(), 'tid' => $tid, 'status' => $status]) ->fields(['sticky' => $sticky, 'created' => $node->getCreatedTime()]) ->execute(); }