From 6b3ce75689f20eca760cc7e11b7ba8f956d572db Mon Sep 17 00:00:00 2001
From: Florent Torregrosa <florent.torregrosa@smile.fr>
Date: Tue, 29 Oct 2019 17:36:56 +0100
Subject: [PATCH] Issue #2889486 by Grimreaper, vacho: "Has taxonomy term"
 contextual filter does not take the language value into account

---
 .../optional/views.view.taxonomy_term.yml     | 40 ++++++++
 .../taxonomy/src/TermStorageSchema.php        | 10 +-
 core/modules/taxonomy/src/TermViewsData.php   | 11 +++
 core/modules/taxonomy/taxonomy.install        | 92 +++++++++++++++++++
 core/modules/taxonomy/taxonomy.module         | 28 ++++--
 .../Functional/Views/TaxonomyTermViewTest.php | 19 ++++
 6 files changed, 189 insertions(+), 11 deletions(-)

diff --git a/core/modules/taxonomy/config/optional/views.view.taxonomy_term.yml b/core/modules/taxonomy/config/optional/views.view.taxonomy_term.yml
index 92da5368f0..bcd2acf0a5 100644
--- a/core/modules/taxonomy/config/optional/views.view.taxonomy_term.yml
+++ b/core/modules/taxonomy/config/optional/views.view.taxonomy_term.yml
@@ -213,6 +213,46 @@ display:
             default_group_multiple: {  }
             group_items: {  }
           plugin_id: boolean
+        langcode_1:
+          id: langcode_1
+          table: taxonomy_index
+          field: langcode
+          relationship: none
+          group_type: group
+          admin_label: ''
+          operator: in
+          value:
+            '***LANGUAGE_language_content***': '***LANGUAGE_language_content***'
+          group: 1
+          exposed: false
+          expose:
+            operator_id: ''
+            label: ''
+            description: ''
+            use_operator: false
+            operator: ''
+            operator_limit_selection: false
+            operator_list: {  }
+            identifier: ''
+            required: false
+            remember: false
+            multiple: false
+            remember_roles:
+              authenticated: authenticated
+            reduce: false
+          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: language
       style:
         type: default
         options:
diff --git a/core/modules/taxonomy/src/TermStorageSchema.php b/core/modules/taxonomy/src/TermStorageSchema.php
index 0aa097bcf7..d8e8da3f15 100644
--- a/core/modules/taxonomy/src/TermStorageSchema.php
+++ b/core/modules/taxonomy/src/TermStorageSchema.php
@@ -60,10 +60,16 @@ protected function getEntitySchema(ContentEntityTypeInterface $entity_type, $res
           'not null' => TRUE,
           'default' => 0,
         ],
+        'langcode' => [
+          'description' => 'The langcode of the node.',
+          'type' => 'varchar_ascii',
+          'length' => 12,
+          'not null' => TRUE,
+        ],
       ],
-      'primary key' => ['nid', 'tid'],
+      'primary key' => ['nid', 'tid', 'langcode'],
       'indexes' => [
-        'term_node' => ['tid', 'status', 'sticky', 'created'],
+        'term_node' => ['tid', 'status', 'sticky', 'created', 'langcode'],
       ],
       'foreign keys' => [
         'tracked_node' => [
diff --git a/core/modules/taxonomy/src/TermViewsData.php b/core/modules/taxonomy/src/TermViewsData.php
index 73e8cbc007..461b3913ad 100644
--- a/core/modules/taxonomy/src/TermViewsData.php
+++ b/core/modules/taxonomy/src/TermViewsData.php
@@ -212,6 +212,17 @@ public function getViewsData() {
       ],
     ];
 
+    $data['taxonomy_index']['langcode'] = [
+      'title' => $this->t('Node language'),
+      'help' => $this->t('The langcode of the node.'),
+      'filter' => [
+        'id' => 'language',
+      ],
+      'sort' => [
+        'id' => 'standard',
+      ],
+    ];
+
     $data['taxonomy_index']['created'] = [
       'title' => $this->t('Post date'),
       'help' => $this->t('The date the content related to a term was posted.'),
diff --git a/core/modules/taxonomy/taxonomy.install b/core/modules/taxonomy/taxonomy.install
index bcf64aca5d..0e07b36a77 100644
--- a/core/modules/taxonomy/taxonomy.install
+++ b/core/modules/taxonomy/taxonomy.install
@@ -8,6 +8,7 @@
 use Drupal\Core\Entity\Sql\SqlEntityStorageInterface;
 use Drupal\Core\Field\BaseFieldDefinition;
 use Drupal\Core\Site\Settings;
+use Drupal\Core\Database\Database;
 
 /**
  * Convert the custom taxonomy term hierarchy storage to a default storage.
@@ -248,3 +249,94 @@ function taxonomy_update_8702(&$sandbox) {
   }
   $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['current'] / $sandbox['max']);
 }
+
+/**
+ * Add 'langcode' field to 'taxonomy_index' entities.
+ *
+ * Content needs to be updated.
+ */
+function taxonomy_update_8703() {
+  if (!Database::getConnection()->schema()->fieldExists('taxonomy_index', 'langcode')) {
+    Database::getConnection()->schema()->addField('taxonomy_index', 'langcode', [
+      'description' => 'The langcode of the node.',
+      'type' => 'varchar_ascii',
+      'length' => 12,
+      'not null' => TRUE,
+    ]);
+
+    Database::getConnection()->schema()->dropPrimaryKey('taxonomy_index');
+    Database::getConnection()->schema()->addPrimaryKey(
+      'taxonomy_index',
+      ['nid', 'tid', 'langcode']
+    );
+
+    Database::getConnection()->schema()->dropIndex('taxonomy_index', 'term_node');
+    Database::getConnection()->schema()->addIndex(
+      'taxonomy_index',
+      'term_node',
+      ['tid', 'status', 'sticky', 'created', 'langcode'],
+      [
+        'description' => 'Maintains denormalized information about node/term relationships.',
+        'fields' => [
+          'nid' => [
+            'description' => 'The {node}.nid this record tracks.',
+            'type' => 'int',
+            'unsigned' => TRUE,
+            'not null' => TRUE,
+            'default' => 0,
+          ],
+          'tid' => [
+            'description' => 'The term ID.',
+            'type' => 'int',
+            'unsigned' => TRUE,
+            'not null' => TRUE,
+            'default' => 0,
+          ],
+          'status' => [
+            'description' => 'Boolean indicating whether the node is published (visible to non-administrators).',
+            'type' => 'int',
+            'not null' => TRUE,
+            'default' => 1,
+          ],
+          'sticky' => [
+            'description' => 'Boolean indicating whether the node is sticky.',
+            'type' => 'int',
+            'not null' => FALSE,
+            'default' => 0,
+            'size' => 'tiny',
+          ],
+          'created' => [
+            'description' => 'The Unix timestamp when the node was created.',
+            'type' => 'int',
+            'not null' => TRUE,
+            'default' => 0,
+          ],
+          'langcode' => [
+            'description' => 'The langcode of the node.',
+            'type' => 'varchar_ascii',
+            'length' => 12,
+            'not null' => TRUE,
+          ],
+        ],
+        'primary key' => ['nid', 'tid', 'langcode'],
+        'indexes' => [
+          'term_node' => ['tid', 'status', 'sticky', 'created', 'langcode'],
+        ],
+        'foreign keys' => [
+          'tracked_node' => [
+            'table' => 'node',
+            'columns' => ['nid' => 'nid'],
+          ],
+          'term' => [
+            'table' => 'taxonomy_term_data',
+            'columns' => ['tid' => 'tid'],
+          ],
+        ],
+      ]
+    );
+  }
+
+  $manager = \Drupal::entityDefinitionUpdateManager();
+  $entity_type = $manager->getEntityType('taxonomy_term');
+  $manager->updateEntityType($entity_type);
+}
diff --git a/core/modules/taxonomy/taxonomy.module b/core/modules/taxonomy/taxonomy.module
index 24abc7dd68..d29a5c9ce7 100644
--- a/core/modules/taxonomy/taxonomy.module
+++ b/core/modules/taxonomy/taxonomy.module
@@ -11,6 +11,7 @@
 use Drupal\Core\Render\Element;
 use Drupal\Core\Routing\RouteMatchInterface;
 use Drupal\Core\Url;
+use Drupal\node\NodeInterface;
 use Drupal\taxonomy\Entity\Term;
 use Drupal\taxonomy\Entity\Vocabulary;
 use Drupal\taxonomy\TermInterface;
@@ -532,7 +533,7 @@ function taxonomy_build_node_index($node) {
         foreach ($node->getTranslationLanguages() as $language) {
           foreach ($node->getTranslation($language->getId())->$field_name as $item) {
             if (!$item->isEmpty()) {
-              $tid_all[$item->target_id] = $item->target_id;
+              $tid_all[$item->target_id][$language->getId()] = $item->target_id;
             }
           }
         }
@@ -541,11 +542,13 @@ function taxonomy_build_node_index($node) {
     // Insert index entries for all the node's terms.
     if (!empty($tid_all)) {
       $connection = \Drupal::database();
-      foreach ($tid_all as $tid) {
-        $connection->merge('taxonomy_index')
-          ->key(['nid' => $node->id(), 'tid' => $tid, 'status' => $node->isPublished()])
-          ->fields(['sticky' => $sticky, 'created' => $node->getCreatedTime()])
-          ->execute();
+      foreach ($tid_all as $tid_info) {
+        foreach ($tid_info as $langcode => $tid) {
+          $connection->merge('taxonomy_index')
+            ->key(['nid' => $node->id(), 'tid' => $tid, 'status' => $node->isPublished(), 'langcode' => $langcode])
+            ->fields(['sticky' => $sticky, 'created' => $node->getCreatedTime()])
+            ->execute();
+        }
       }
     }
   }
@@ -575,12 +578,19 @@ function taxonomy_node_predelete(EntityInterface $node) {
 /**
  * Deletes taxonomy index entries for a given node.
  *
- * @param \Drupal\Core\Entity\EntityInterface $node
+ * @param \Drupal\node\NodeInterface $node
  *   The node entity.
  */
-function taxonomy_delete_node_index(EntityInterface $node) {
+function taxonomy_delete_node_index(NodeInterface $node) {
   if (\Drupal::config('taxonomy.settings')->get('maintain_index_table')) {
-    \Drupal::database()->delete('taxonomy_index')->condition('nid', $node->id())->execute();
+    $languages_translated = $node->getTranslationLanguages();
+    $nid = $node->id();
+    foreach (array_keys($languages_translated) as $langcode) {
+      \Drupal::database()->delete('taxonomy_index')
+        ->condition('nid', $nid)
+        ->condition('langcode', $langcode)
+        ->execute();
+    }
   }
 }
 
diff --git a/core/modules/taxonomy/tests/src/Functional/Views/TaxonomyTermViewTest.php b/core/modules/taxonomy/tests/src/Functional/Views/TaxonomyTermViewTest.php
index 42bfc4989c..dab6aa7aba 100644
--- a/core/modules/taxonomy/tests/src/Functional/Views/TaxonomyTermViewTest.php
+++ b/core/modules/taxonomy/tests/src/Functional/Views/TaxonomyTermViewTest.php
@@ -107,15 +107,34 @@ public function testTaxonomyTermView() {
 
     $this->drupalPostForm('node/' . $node->id() . '/translations/add/en/ur', $edit, t('Save (this translation)'));
 
+    // Create a second term that will not reference the term in one of its
+    // language.
+    $edit = [];
+    $edit['title[0][value]'] = $original_title_2 = $this->randomMachineName();
+    $edit['body[0][value]'] = $this->randomMachineName();
+    $edit["{$this->fieldName1}[]"] = $term->id();
+    $this->drupalPostForm('node/add/article', $edit, t('Save'));
+    $node_2 = $this->drupalGetNodeByTitle($edit['title[0][value]']);
+
+    $edit['title[0][value]'] = $translated_title_2 = $this->randomMachineName();
+    $edit["{$this->fieldName1}[]"] = '_none';
+    $this->drupalPostForm('node/' . $node_2->id() . '/translations/add/en/ur', $edit, t('Save (this translation)'));
+
     $this->drupalGet('taxonomy/term/' . $term->id());
     $this->assertText($term->label());
     $this->assertText($original_title);
     $this->assertNoText($translated_title);
+    $this->assertText($original_title_2);
+    $this->assertNoText($translated_title_2);
 
     $this->drupalGet('ur/taxonomy/term/' . $term->id());
     $this->assertText($term->label());
     $this->assertNoText($original_title);
     $this->assertText($translated_title);
+    // As node 2 do not reference the term, it should not appear on the
+    // translated term page.
+    $this->assertNoText($original_title_2);
+    $this->assertNoText($translated_title_2);
 
     // Uninstall language module and ensure that the language is not part of the
     // query anymore.
-- 
2.17.1

