From 63113028c920ddedcbef0249e33f079d0ff34961 Mon Sep 17 00:00:00 2001
From: Jennifer Hodgdon <yahgrp@poplarware.com>
Date: Mon, 10 Jun 2013 08:38:20 -0700
Subject: [PATCH] Issue #1831540 by orb, Berdir, andypost, podarok, Gaelan,
 znerol, cam8001: Fix up examples in taxonomy.api.php hooks

---
 core/modules/taxonomy/taxonomy.api.php | 91 +++++++++++++-------------
 1 file changed, 45 insertions(+), 46 deletions(-)

diff --git a/core/modules/taxonomy/taxonomy.api.php b/core/modules/taxonomy/taxonomy.api.php
index 24fcc0170de1..0371ce62a6b8 100644
--- a/core/modules/taxonomy/taxonomy.api.php
+++ b/core/modules/taxonomy/taxonomy.api.php
@@ -22,8 +22,8 @@
  *   The vocabulary object.
  */
 function hook_taxonomy_vocabulary_create(\Drupal\taxonomy\Plugin\Core\Entity\Vocabulary $vocabulary) {
-  if (!isset($vocabulary->synonyms)) {
-    $vocabulary->synonyms = FALSE;
+  if (!isset($vocabulary->foo)) {
+    $vocabulary->foo = NULL;
   }
 }
 
@@ -37,12 +37,15 @@ function hook_taxonomy_vocabulary_create(\Drupal\taxonomy\Plugin\Core\Entity\Voc
  *   An array of taxonomy vocabulary entities.
  */
 function hook_taxonomy_vocabulary_load(array $vocabularies) {
-  foreach ($vocabularies as $vocabulary) {
-    $vocabulary->synonyms = variable_get('taxonomy_' . $vocabulary->id() . '_synonyms', FALSE);
+  $result = db_select('mytable', 'm')
+    ->fields('m', array('vid', 'foo'))
+    ->condition('m.vid', array_keys($vocabularies), 'IN')
+    ->execute();
+  foreach ($result as $record) {
+    $vocabularies[$record->vid]->foo = $record->foo;
   }
 }
 
-
 /**
  * Act on taxonomy vocabularies before they are saved.
  *
@@ -66,8 +69,8 @@ function hook_taxonomy_vocabulary_presave(Drupal\taxonomy\Plugin\Core\Entity\Voc
  *   A taxonomy vocabulary entity.
  */
 function hook_taxonomy_vocabulary_insert(Drupal\taxonomy\Plugin\Core\Entity\Vocabulary $vocabulary) {
-  if ($vocabulary->synonyms) {
-    variable_set('taxonomy_' . $vocabulary->id() . '_synonyms', TRUE);
+  if ($vocabulary->id() == 'my_vocabulary') {
+    $vocabulary->weight = 100;
   }
 }
 
@@ -80,10 +83,12 @@ function hook_taxonomy_vocabulary_insert(Drupal\taxonomy\Plugin\Core\Entity\Voca
  *   A taxonomy vocabulary entity.
  */
 function hook_taxonomy_vocabulary_update(Drupal\taxonomy\Plugin\Core\Entity\Vocabulary $vocabulary) {
-  $status = $vocabulary->synonyms ? TRUE : FALSE;
-  if ($vocabulary->synonyms) {
-    variable_set('taxonomy_' . $vocabulary->id() . '_synonyms', $status);
-  }
+  db_insert('mytable')
+    ->fields(array(
+      'vid' => $vocabulary->id(),
+      'foo' => $vocabulary->foo,
+    ))
+    ->execute();
 }
 
 /**
@@ -98,9 +103,9 @@ function hook_taxonomy_vocabulary_update(Drupal\taxonomy\Plugin\Core\Entity\Voca
  * @see hook_taxonomy_vocabulary_delete()
  */
 function hook_taxonomy_vocabulary_predelete(Drupal\taxonomy\Plugin\Core\Entity\Vocabulary $vocabulary) {
-  if (variable_get('taxonomy_' . $vocabulary->id() . '_synonyms', FALSE)) {
-    variable_del('taxonomy_' . $vocabulary->id() . '_synonyms');
-  }
+  db_delete('mytable_index')
+    ->condition('vid', $vocabulary->id())
+    ->execute();
 }
 
 /**
@@ -115,9 +120,9 @@ function hook_taxonomy_vocabulary_predelete(Drupal\taxonomy\Plugin\Core\Entity\V
  * @see hook_taxonomy_vocabulary_predelete()
  */
 function hook_taxonomy_vocabulary_delete(Drupal\taxonomy\Plugin\Core\Entity\Vocabulary $vocabulary) {
-  if (variable_get('taxonomy_' . $vocabulary->id() . '_synonyms', FALSE)) {
-    variable_del('taxonomy_' . $vocabulary->id() . '_synonyms');
-  }
+  db_delete('mytable')
+    ->condition('vid', $vocabulary->id())
+    ->execute();
 }
 
 /**
@@ -152,9 +157,12 @@ function hook_taxonomy_term_create(\Drupal\taxonomy\Plugin\Core\Entity\Term $ter
  *   An array of taxonomy term entities, indexed by tid.
  */
 function hook_taxonomy_term_load(array $terms) {
-  $result = db_query('SELECT tid, foo FROM {mytable} WHERE tid IN (:tids)', array(':tids' => array_keys($terms)));
+  $result = db_select('mytable', 'm')
+    ->fields('m', array('tid', 'foo'))
+    ->condition('m.tid', array_keys($terms), 'IN')
+    ->execute();
   foreach ($result as $record) {
-    $terms[$record->id()]->foo = $record->foo;
+    $terms[$record->tid]->foo = $record->foo;
   }
 }
 
@@ -181,18 +189,12 @@ function hook_taxonomy_term_presave(Drupal\taxonomy\Term $term) {
  *   A taxonomy term entity.
  */
 function hook_taxonomy_term_insert(Drupal\taxonomy\Term $term) {
-  if (!empty($term->synonyms)) {
-    foreach (explode ("\n", str_replace("\r", '', $term->synonyms)) as $synonym) {
-      if ($synonym) {
-        db_insert('taxonomy_term_synonym')
-        ->fields(array(
-          'tid' => $term->id(),
-          'name' => rtrim($synonym),
-        ))
-        ->execute();
-      }
-    }
-  }
+  db_insert('mytable')
+    ->fields(array(
+      'tid' => $term->id(),
+      'foo' => $term->foo,
+    ))
+    ->execute();
 }
 
 /**
@@ -204,19 +206,12 @@ function hook_taxonomy_term_insert(Drupal\taxonomy\Term $term) {
  *   A taxonomy term entity.
  */
 function hook_taxonomy_term_update(Drupal\taxonomy\Term $term) {
-  hook_taxonomy_term_delete($term);
-  if (!empty($term->synonyms)) {
-    foreach (explode ("\n", str_replace("\r", '', $term->synonyms)) as $synonym) {
-      if ($synonym) {
-        db_insert('taxonomy_term_synonym')
-        ->fields(array(
-          'tid' => $term->id(),
-          'name' => rtrim($synonym),
-        ))
-        ->execute();
-      }
-    }
-  }
+  db_insert('mytable')
+    ->fields(array(
+      'tid' => $term->id(),
+      'foo' => $term->foo,
+    ))
+    ->execute();
 }
 
 /**
@@ -230,7 +225,9 @@ function hook_taxonomy_term_update(Drupal\taxonomy\Term $term) {
  *   The taxonomy term entity that is about to be deleted.
  */
 function hook_taxonomy_term_predelete(Drupal\taxonomy\Term $term) {
-  db_delete('term_synoynm')->condition('tid', $term->id())->execute();
+  db_delete('mytable_index')
+    ->condition('tid', $term->id())
+    ->execute();
 }
 
 /**
@@ -243,7 +240,9 @@ function hook_taxonomy_term_predelete(Drupal\taxonomy\Term $term) {
  *   The taxonomy term entity that has been deleted.
  */
 function hook_taxonomy_term_delete(Drupal\taxonomy\Term $term) {
-  db_delete('term_synoynm')->condition('tid', $term->id())->execute();
+  db_delete('mytable')
+    ->condition('tid', $term->id())
+    ->execute();
 }
 
 /**
-- 
GitLab