From 87599ecf1098948f0acc028df2dc8a772f53f61e Mon Sep 17 00:00:00 2001
From: Alexander Benjamin <alx_benjamin@yahoo.com>
Date: Wed, 5 Aug 2015 09:18:47 +0200
Subject: [PATCH] - Issue #2467293: merging 2 patches into one

---
 core/modules/taxonomy/src/Entity/Vocabulary.php    | 36 +++++++++++++---------
 .../taxonomy/src/Tests/TermEntityReferenceTest.php | 14 +++++++++
 2 files changed, 35 insertions(+), 15 deletions(-)

diff --git a/core/modules/taxonomy/src/Entity/Vocabulary.php b/core/modules/taxonomy/src/Entity/Vocabulary.php
index 2855c65..78a2b24 100644
--- a/core/modules/taxonomy/src/Entity/Vocabulary.php
+++ b/core/modules/taxonomy/src/Entity/Vocabulary.php
@@ -135,29 +135,35 @@ public function postSave(EntityStorageInterface $storage, $update = TRUE) {
       $field_map = \Drupal::entityManager()->getFieldMapByFieldType('entity_reference');
       foreach ($field_map as $entity_type => $field_storages) {
         foreach ($field_storages as $field_storage => $info) {
-          $field_ids[] = $entity_type . '.' . $field_storage;
+          foreach ($info['bundles'] as $bundle) {
+            $field_ids[] = $entity_type . '.' . $bundle . '.' . $field_storage;
+          }
         }
       }
 
-      $field_storages = \Drupal::entityManager()->getStorage('field_storage_config')->loadMultiple($field_ids);
-      $taxonomy_fields = array_filter($field_storages, function ($field_storage) {
-        return $field_storage->getType() == 'entity_reference' && $field_storage->getSetting('target_type') == 'taxonomy_term';
+      $fields = \Drupal::entityManager()->getStorage('field_config')->loadMultiple($field_ids);
+      $taxonomy_fields = array_filter($fields, function ($field) {
+        return $field->getFieldStorageDefinition()->getSetting('target_type') == 'taxonomy_term';
       });
 
-      foreach ($taxonomy_fields as $field_storage) {
-        $update_storage = FALSE;
-
-        $allowed_values = $field_storage->getSetting('allowed_values');
-        foreach ($allowed_values as &$value) {
-          if ($value['vocabulary'] == $this->getOriginalId()) {
-            $value['vocabulary'] = $this->id();
-            $update_storage = TRUE;
+      foreach ($taxonomy_fields as $field) {
+        /** @var \Drupal\field\FieldConfigInterface $field */
+        $update_field = FALSE;
+
+        $settings = $field->getSettings();
+        $target_bundles = [];
+        foreach ($settings['handler_settings']['target_bundles'] as $value) {
+          if ($value == $this->getOriginalId()) {
+            // Replace allowed bundle with new value.
+            $value= $this->id();
+            $update_field = TRUE;
           }
+          $target_bundles[$value] = $value;
         }
-        $field_storage->setSetting('allowed_values', $allowed_values);
 
-        if ($update_storage) {
-          $field_storage->save();
+        if ($update_field) {
+          $settings['handler_settings']['target_bundles'] = $target_bundles;
+          $field->set('settings', $settings)->save();
         }
       }
     }
diff --git a/core/modules/taxonomy/src/Tests/TermEntityReferenceTest.php b/core/modules/taxonomy/src/Tests/TermEntityReferenceTest.php
index fc452ff..9fba9a0 100644
--- a/core/modules/taxonomy/src/Tests/TermEntityReferenceTest.php
+++ b/core/modules/taxonomy/src/Tests/TermEntityReferenceTest.php
@@ -7,6 +7,7 @@
 
 namespace Drupal\taxonomy\Tests;
 
+use Drupal\Component\Utility\Unicode;
 use Drupal\field\Entity\FieldStorageConfig;
 use Drupal\field\Entity\FieldConfig;
 
@@ -79,5 +80,18 @@ function testSelectionTestVocabularyRestriction() {
     );
 
     $this->assertIdentical($result, $expected_result, 'Terms selection restricted to a single vocabulary.');
+
+    // Check vocabulary rename.
+    // @see \Drupal\taxonomy\Tests\VocabularyCrudTest::testTaxonomyVocabularyChangeMachineName()
+    $old_name = $vocabulary->id();
+    $new_name = Unicode::strtolower($this->randomMachineName());
+    // Change the machine name.
+    $vocabulary->set('vid', $new_name);
+    $vocabulary->save();
+    $field = entity_load('field_config', $field->id(), TRUE);
+    $target_bundles = $field->getSetting('handler_settings')['target_bundles'];
+    $this->assertFalse(isset($target_bundles[$old_name]), 'Field not references old vocabulary');
+    $this->assertTrue(isset($target_bundles[$new_name]), 'Field references new vocabulary');
   }
+
 }
-- 
1.9.1

