From 80e57ef22e8cf48649289745f53a3cfbc758ba4a Mon Sep 17 00:00:00 2001 From: Claudiu Cristea Date: Fri, 25 Jan 2013 11:41:42 +0200 Subject: [PATCH] Issue #461262 by claudiu.cristea | ShutterFreak: Fixed Content profile can't handle translated profiles. --- content_profile.module | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 42 insertions(+), 2 deletions(-) diff --git a/content_profile.module b/content_profile.module index fb1026d..542a0d5 100644 --- a/content_profile.module +++ b/content_profile.module @@ -476,8 +476,7 @@ function content_profile_load($type, $uid, $lang = '', $reset = NULL) { if (!isset($cache[$type][$uid][$lang]) || $reset) { $cache[$type][$uid][$lang] = FALSE; - $params = array('type' => $type, 'uid' => $uid); - if ($node = node_load($lang ? $params + array('language' => $lang) : $params, NULL, $reset)) { + if ($node = content_profile_load_translated($type, $uid, $lang, $reset)) { $cache[$type][$uid][$lang] = $node->nid; } return $node; @@ -674,3 +673,44 @@ function content_profile_pageroute_info() { ) ); } + +/** + * Get the right translation node for a specific user. + * + * If there's an existing translated node in the current language returns it + * otherwise search for the language neutral version. + * + * @param $node_type + * String with the content-type machine redable name. + * @param $uid + * Integer with user ID. + * @param $lang + * Optional string with the language code. If omitted, the current language is + * used. + * @param $reset + * Optional. If set, the cache is reset. + * + * @return + * Returns a node object or FALSE on failure. + */ +function content_profile_load_translated($node_type, $uid, $lang = NULL, $reset = NULL) { + if (empty($lang)) { + global $language; + $lang = $language->language; + } + + // Retrieve nodes with in the next order: + // - First check for a translated node. + // - If not found, try to get a language neutral node. + // ...pickup the first best choice. + $query = "SELECT nid FROM {node} WHERE type = '%s' AND uid = %d AND language IN ('%s', '') ORDER BY language DESC"; + $nid = db_result(db_query_range($query, $node_type, $uid, $lang, 0, 1)); + + // Fallback to the default language translation. + if (!$nid) { + $lang = language_default('language'); + $nid = db_result(db_query("SELECT nid FROM {node} WHERE type = '%s' AND uid = %d AND language = '%s'", $node_type, $uid, $lang)); + } + + return $nid ? node_load($nid, NULL, $reset) : FALSE; +} -- 1.7.5.4