From 583a9b3e8769457e129cc7644ebb8c38a1618c00 Mon Sep 17 00:00:00 2001
From: florenttorregrosa <florenttorregrosa@2388214.no-reply.drupal.org>
Date: Sun, 25 Jun 2017 19:28:38 +0200
Subject: [PATCH] Issue #2889486 by Grimreaper: "Has taxonomy term" contextual
 filter does not take the language value into account

---
 core/modules/taxonomy/src/TermStorageSchema.php | 10 ++++++++--
 core/modules/taxonomy/src/TermViewsData.php     | 11 +++++++++++
 core/modules/taxonomy/taxonomy.install          | 26 +++++++++++++++++++++++++
 core/modules/taxonomy/taxonomy.module           | 14 +++++++------
 4 files changed, 53 insertions(+), 8 deletions(-)
 create mode 100644 core/modules/taxonomy/taxonomy.install

diff --git a/core/modules/taxonomy/src/TermStorageSchema.php b/core/modules/taxonomy/src/TermStorageSchema.php
index 5bcb088..75e9b87 100644
--- a/core/modules/taxonomy/src/TermStorageSchema.php
+++ b/core/modules/taxonomy/src/TermStorageSchema.php
@@ -88,10 +88,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 5e98bfe..f41dcba 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
new file mode 100644
index 0000000..a165c1e
--- /dev/null
+++ b/core/modules/taxonomy/taxonomy.install
@@ -0,0 +1,26 @@
+<?php
+
+/**
+ * @file
+ * Install, update and uninstall functions for the taxonomy module.
+ */
+
+use Drupal\Core\Database\Database;
+
+/**
+ * Add 'langcode' field to 'taxonomy_index' entities.
+ *
+ * Content needs to be updated.
+ */
+function taxonomy_update_8300() {
+  Database::getConnection()->schema()->addField('taxonomy_index', 'langcode', [
+    'description' => 'The langcode of the node.',
+    'type' => 'varchar_ascii',
+    'length' => 12,
+    'not null' => TRUE,
+  ]);
+
+  $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 4a93989..bde249b 100644
--- a/core/modules/taxonomy/taxonomy.module
+++ b/core/modules/taxonomy/taxonomy.module
@@ -520,7 +520,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;
             }
           }
         }
@@ -528,11 +528,13 @@ function taxonomy_build_node_index($node) {
     }
     // Insert index entries for all the node's terms.
     if (!empty($tid_all)) {
-      foreach ($tid_all as $tid) {
-        db_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) {
+          db_merge('taxonomy_index')
+            ->key(['nid' => $node->id(), 'tid' => $tid, 'status' => $node->isPublished(), 'langcode' => $langcode])
+            ->fields(['sticky' => $sticky, 'created' => $node->getCreatedTime()])
+            ->execute();
+        }
       }
     }
   }
-- 
2.1.4

