Skip to content
Snippets Groups Projects
Unverified Commit 169af09f authored by bgm's avatar bgm Committed by GitHub
Browse files

Merge pull request #12152 from JMAConsulting/dev_drupal_19

drupal#19 Drupal8: Implement set UF locale/language (affects mailing tokens)
parents e9dd006b 32040f62
Branches
Tags
No related merge requests found
......@@ -509,7 +509,7 @@ VALUES (%1, %2, %3, %4, %5, %6, %7)
$config = CRM_Core_Config::singleton();
}
if (property_exists($mailing, 'language') && $mailing->language && $mailing->language != 'en_US') {
if (property_exists($mailing, 'language') && $mailing->language && $mailing->language != CRM_Core_I18n::getLocale()) {
$swapLang = CRM_Utils_AutoClean::swap('global://dbLocale?getter', 'call://i18n/setLocale', $mailing->language);
}
......
......@@ -300,19 +300,21 @@ class CRM_Utils_System_Drupal8 extends CRM_Utils_System_DrupalBase {
) {
$query = html_entity_decode($query);
$config = CRM_Core_Config::singleton();
$base = $absolute ? $config->userFrameworkBaseURL : 'internal:/';
$url = \Drupal\civicrm\CivicrmHelper::parseURL("{$path}?{$query}");
// Not all links that CiviCRM generates are Drupal routes, so we use the weaker ::fromUri method.
try {
$url = \Drupal\Core\Url::fromUri("base:{$url['path']}", [
$url = \Drupal\Core\Url::fromUri("{$base}{$url['path']}", array(
'query' => $url['query'],
'fragment' => $fragment,
'absolute' => $absolute,
])->toString();
))->toString();
}
catch (Exception $e) {
// @Todo: log to watchdog
$url = '';
\Drupal::logger('civicrm')->error($e->getMessage());
}
// Special case: CiviCRM passes us "*path*?*query*" as a skeleton, but asterisks
......@@ -673,4 +675,88 @@ class CRM_Utils_System_Drupal8 extends CRM_Utils_System_DrupalBase {
$e->list[] = 'js/crm.drupal8.js';
}
/**
* @inheritDoc
*/
public function setUFLocale($civicrm_language) {
$langcode = substr(str_replace('_', '', $civicrm_language), 0, 2);
$languageManager = \Drupal::languageManager();
$languages = $languageManager->getLanguages();
if (isset($languages[$langcode])) {
$languageManager->setConfigOverrideLanguage($languages[$langcode]);
// Config must be re-initialized to reset the base URL
// otherwise links will have the wrong language prefix/domain.
$config = CRM_Core_Config::singleton();
$config->free();
return TRUE;
}
return FALSE;
}
/**
* @inheritDoc
*/
public function languageNegotiationURL($url, $addLanguagePart = TRUE, $removeLanguagePart = FALSE) {
if (empty($url)) {
return $url;
}
// Drupal might not be bootstrapped if being called by the REST API.
if (!class_exists('Drupal')) {
return NULL;
}
$language = $this->getCurrentLanguage();
if (\Drupal::service('module_handler')->moduleExists('language')) {
$config = \Drupal::config('language.negotiation')->get('url');
//does user configuration allow language
//support from the URL (Path prefix or domain)
$enabledLanguageMethods = \Drupal::config('language.types')->get('negotiation.language_interface.enabled') ?: [];
if (array_key_exists(\Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl::METHOD_ID, $enabledLanguageMethods)) {
$urlType = $config['source'];
//url prefix
if ($urlType == \Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl::CONFIG_PATH_PREFIX) {
if (!empty($language)) {
if ($addLanguagePart && !empty($config['prefixes'][$language])) {
$url .= $config['prefixes'][$language] . '/';
}
if ($removeLanguagePart) {
$url = str_replace("/" . $config['prefixes'][$language] . "/", '/', $url);
}
}
}
//domain
if ($urlType == \Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl::CONFIG_DOMAIN) {
if (isset($language->domain) && $language->domain) {
if ($addLanguagePart) {
$url = (CRM_Utils_System::isSSL() ? 'https' : 'http') . '://' . $config['domains'][$language] . base_path();
}
if ($removeLanguagePart && defined('CIVICRM_UF_BASEURL')) {
$url = str_replace('\\', '/', $url);
$parseUrl = parse_url($url);
//kinda hackish but not sure how to do it right
//hope http_build_url() will help at some point.
if (is_array($parseUrl) && !empty($parseUrl)) {
$urlParts = explode('/', $url);
$hostKey = array_search($parseUrl['host'], $urlParts);
$ufUrlParts = parse_url(CIVICRM_UF_BASEURL);
$urlParts[$hostKey] = $ufUrlParts['host'];
$url = implode('/', $urlParts);
}
}
}
}
}
}
return $url;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment