From 3c33fb1690eeea7f49fdecf589436982b4ed4603 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Thu, 26 Aug 2021 22:21:49 -0700 Subject: [PATCH] dev/translation#70 - Cache separate ContactType lists per-locale Notes: * If you grep to see how `contactTypes` cache is used, it appears that they never access this row individually outside this function. * There are coarse-grained `clear()` invocations all `contactTypes`-related data. These should hit the old+new keys the same way. * I made the cache-lookup a little more micro-optimal. :shrug: To verify that this fixes the bug, I enabled multilingual with fr_CA+es_MX and manually translated the labels for "Individual" contacts. Then used the following command: ``` cv ev -U admin 'function go($l){ CRM_Core_I18n::singleton()->setLocale($l); return \Civi\Test\Invasive::call(["CRM_Contact_BAO_ContactType","getAllContactTypes"])["Individual"]; } return [go("fr_CA"),go("es_MX")];' ``` Before the patch, it returns the en_US labels ("Individual"..."Individual"...). After the patch, it returns the fr_CA and es_MX labels that I had created ("Particulier"..."Persona"...). --- CRM/Contact/BAO/ContactType.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/CRM/Contact/BAO/ContactType.php b/CRM/Contact/BAO/ContactType.php index 18b62ab86e..790fe70b6a 100644 --- a/CRM/Contact/BAO/ContactType.php +++ b/CRM/Contact/BAO/ContactType.php @@ -863,7 +863,10 @@ WHERE ($subtypeClause)"; * @throws \API_Exception */ protected static function getAllContactTypes() { - if (!Civi::cache('contactTypes')->has('all')) { + $cache = Civi::cache('contactTypes'); + $cacheKey = 'all_' . $GLOBALS['tsLocale']; + $contactTypes = $cache->get($cacheKey); + if ($contactTypes === NULL) { $contactTypes = (array) ContactType::get(FALSE) ->setSelect(['id', 'name', 'label', 'description', 'is_active', 'is_reserved', 'image_URL', 'parent_id', 'parent_id:name', 'parent_id:label']) ->execute()->indexBy('name'); @@ -873,9 +876,8 @@ WHERE ($subtypeClause)"; $contactTypes[$id]['parent_label'] = $contactType['parent_id:label']; unset($contactTypes[$id]['parent_id:name'], $contactTypes[$id]['parent_id:label']); } - Civi::cache('contactTypes')->set('all', $contactTypes); + $cache->set($cacheKey, $contactTypes); } - $contactTypes = Civi::cache('contactTypes')->get('all'); return $contactTypes; } -- 2.25.1