From 9ae8e2c86a00fb5fb31cfbee23a6b451efca44a2 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Wed, 19 Nov 2014 11:40:15 +0000
Subject: [PATCH] Issue #2348007 by jibran, olli, marcus7777: Taxonomy term
 view needs status filter

---
 .../install/views.view.taxonomy_term.yml      | 40 ++++++++++++++++++-
 .../taxonomy/src/TermStorageSchema.php        |  8 +++-
 core/modules/taxonomy/src/TermViewsData.php   |  9 +++++
 core/modules/taxonomy/src/Tests/RssTest.php   | 23 +++++++++++
 core/modules/taxonomy/taxonomy.module         |  3 +-
 5 files changed, 79 insertions(+), 4 deletions(-)

diff --git a/core/modules/taxonomy/config/install/views.view.taxonomy_term.yml b/core/modules/taxonomy/config/install/views.view.taxonomy_term.yml
index 289c4195cacd..deac0f83db1c 100644
--- a/core/modules/taxonomy/config/install/views.view.taxonomy_term.yml
+++ b/core/modules/taxonomy/config/install/views.view.taxonomy_term.yml
@@ -95,9 +95,9 @@ display:
           relationship: none
           group_type: group
           admin_label: ''
-          default_action: ignore
+          default_action: 'not found'
           exception:
-            value: all
+            value: ''
             title_enable: false
             title: All
           title_enable: true
@@ -168,6 +168,42 @@ display:
             default_group_multiple: {  }
             group_items: {  }
           plugin_id: language
+        status:
+          id: status
+          table: taxonomy_index
+          field: status
+          relationship: none
+          group_type: group
+          admin_label: ''
+          operator: '='
+          value: true
+          group: 1
+          exposed: false
+          expose:
+            operator_id: ''
+            label: ''
+            description: ''
+            use_operator: false
+            operator: ''
+            identifier: ''
+            required: false
+            remember: false
+            multiple: false
+            remember_roles:
+              authenticated: authenticated
+          is_grouped: false
+          group_info:
+            label: ''
+            description: ''
+            identifier: ''
+            optional: true
+            widget: select
+            multiple: false
+            remember: false
+            default_group: All
+            default_group_multiple: {  }
+            group_items: {  }
+          plugin_id: boolean
       style:
         type: default
         options:
diff --git a/core/modules/taxonomy/src/TermStorageSchema.php b/core/modules/taxonomy/src/TermStorageSchema.php
index 301bf4688627..99232888c0b0 100644
--- a/core/modules/taxonomy/src/TermStorageSchema.php
+++ b/core/modules/taxonomy/src/TermStorageSchema.php
@@ -74,6 +74,12 @@ protected function getEntitySchema(ContentEntityTypeInterface $entity_type, $res
           'not null' => TRUE,
           'default' => 0,
         ),
+        'status' => array(
+          'description' => 'Boolean indicating whether the node is published (visible to non-administrators).',
+          'type' => 'int',
+          'not null' => TRUE,
+          'default' => 1,
+        ),
         'sticky' => array(
           'description' => 'Boolean indicating whether the node is sticky.',
           'type' => 'int',
@@ -90,7 +96,7 @@ protected function getEntitySchema(ContentEntityTypeInterface $entity_type, $res
       ),
       'primary key' => array('nid', 'tid'),
       'indexes' => array(
-        'term_node' => array('tid', 'sticky', 'created'),
+        'term_node' => array('tid', 'status', 'sticky', 'created'),
       ),
       'foreign keys' => array(
         'tracked_node' => array(
diff --git a/core/modules/taxonomy/src/TermViewsData.php b/core/modules/taxonomy/src/TermViewsData.php
index 2e3a83187b14..4b4e578cc3fa 100644
--- a/core/modules/taxonomy/src/TermViewsData.php
+++ b/core/modules/taxonomy/src/TermViewsData.php
@@ -214,6 +214,15 @@ public function getViewsData() {
       ),
     );
 
+    $data['taxonomy_index']['status'] = [
+      'title' => t('Publish status'),
+      'help' => t('Whether or not the content related to a term is published.'),
+      'filter' => [
+        'id' => 'boolean',
+        'label' => t('Published status'),
+        'type' => 'yes-no',
+      ],
+    ];
 
     $data['taxonomy_index']['sticky'] = [
       'title' => t('Sticky status'),
diff --git a/core/modules/taxonomy/src/Tests/RssTest.php b/core/modules/taxonomy/src/Tests/RssTest.php
index acfc4ae431a2..8b9a286176b8 100644
--- a/core/modules/taxonomy/src/Tests/RssTest.php
+++ b/core/modules/taxonomy/src/Tests/RssTest.php
@@ -8,6 +8,7 @@
 namespace Drupal\taxonomy\Tests;
 
 use Drupal\Core\Field\FieldStorageDefinitionInterface;
+use Drupal\views\Views;
 
 /**
  * Ensure that data added as terms appears in RSS feeds if "RSS Category" format
@@ -106,5 +107,27 @@ function testTaxonomyRss() {
     // Test that the feed page exists for the term.
     $this->drupalGet("taxonomy/term/{$term1->id()}/feed");
     $this->assertRaw('<rss version="2.0"', "Feed page is RSS.");
+
+    // Check that the "Exception value" is disabled by default.
+    $this->drupalGet('taxonomy/term/all/feed');
+    $this->assertResponse(404);
+    // Set the exception value to 'all'.
+    $view = Views::getView('taxonomy_term');
+    $arguments = $view->getDisplay()->getOption('arguments');
+    $arguments['tid']['exception']['value'] = 'all';
+    $view->getDisplay()->overrideOption('arguments', $arguments);
+    $view->storage->save();
+    // Check the article is shown in the feed.
+    $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
+    $raw_xml = format_xml_elements([[
+      'key' => 'title',
+      'value' => $node->label(),
+    ]]);
+    $this->drupalGet('taxonomy/term/all/feed');
+    $this->assertRaw($raw_xml);
+    // Unpublish the article and check that it is not shown in the feed.
+    $node->setPublished(FALSE)->save();
+    $this->drupalGet('taxonomy/term/all/feed');
+    $this->assertNoRaw($raw_xml);
   }
 }
diff --git a/core/modules/taxonomy/taxonomy.module b/core/modules/taxonomy/taxonomy.module
index 161a8e265bd4..4c8ee292d93e 100644
--- a/core/modules/taxonomy/taxonomy.module
+++ b/core/modules/taxonomy/taxonomy.module
@@ -162,6 +162,7 @@ function taxonomy_select_nodes($tid, $pager = TRUE, $limit = FALSE, $order = arr
   $query->addTag('node_access');
   $query->addMetaData('base_table', 'taxonomy_index');
   $query->condition('tid', $tid);
+  $query->condition('status', NODE_PUBLISHED);
   if ($pager) {
     $count_query = clone $query;
     $count_query->addExpression('COUNT(t.nid)');
@@ -686,7 +687,7 @@ function taxonomy_build_node_index($node) {
     if (!empty($tid_all)) {
       foreach ($tid_all as $tid) {
         db_merge('taxonomy_index')
-          ->key(array('nid' => $node->id(), 'tid' => $tid))
+          ->key(array('nid' => $node->id(), 'tid' => $tid, 'status' => $node->isPublished()))
           ->fields(array('sticky' => $sticky, 'created' => $node->getCreatedTime()))
           ->execute();
       }
-- 
GitLab