Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move l10n.js to coreResourcesList #13612

Merged
merged 1 commit into from
Feb 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 25 additions & 13 deletions CRM/Core/Resources.php
Original file line number Diff line number Diff line change
Expand Up @@ -591,24 +591,18 @@ public function addCoreResources($region = 'html-header') {
if (is_array($item)) {
$this->addSetting($item);
}
elseif (substr($item, -2) == 'js') {
elseif (strpos($item, '.css')) {
$this->isFullyFormedUrl($item) ? $this->addStyleUrl($item, -100, $region) : $this->addStyleFile('civicrm', $item, -100, $region);
}
elseif ($this->isFullyFormedUrl($item)) {
$this->addScriptUrl($item, $jsWeight++, $region);
}
else {
// Don't bother looking for ts() calls in packages, there aren't any
$translate = (substr($item, 0, 3) == 'js/');
$this->addScriptFile('civicrm', $item, $jsWeight++, $region, $translate);
}
else {
$this->addStyleFile('civicrm', $item, -100, $region);
}
}

$tsLocale = CRM_Core_I18n::getLocale();
// Dynamic localization script
$args = [
'r' => $this->getCacheCode(),
'cid' => CRM_Core_Session::getLoggedInContactID(),
];
$this->addScriptUrl(CRM_Utils_System::url('civicrm/ajax/l10n-js/' . $tsLocale, $args, FALSE, NULL, FALSE), $jsWeight++, $region);

// Add global settings
$settings = array(
'config' => array(
Expand Down Expand Up @@ -733,6 +727,13 @@ public function coreResourceList($region) {
"js/crm.ajax.js",
"js/wysiwyg/crm.wysiwyg.js",
);

// Dynamic localization script
$items[] = $this->addCacheCode(
CRM_Utils_System::url('civicrm/ajax/l10n-js/' . CRM_Core_I18n::getLocale(),
['cid' => CRM_Core_Session::getLoggedInContactID()], FALSE, NULL, FALSE)
);

// add wysiwyg editor
$editor = Civi::settings()->get('editor_id');
if ($editor == "CKEditor") {
Expand Down Expand Up @@ -940,4 +941,15 @@ public function addCacheCode($url) {
return $url . $operator . 'r=' . $this->cacheCode;
}

/**
* Checks if the given URL is fully-formed
*
* @param string $url
*
* @return bool
*/
public static function isFullyFormedUrl($url) {
return (substr($url, 0, 4) === 'http') || (substr($url, 0, 1) === '/');
}

}
24 changes: 24 additions & 0 deletions tests/phpunit/CRM/Core/ResourcesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,4 +397,28 @@ public function urlForCacheCodeProvider() {
);
}

/**
* return array
*/
public function urlsToCheckIfFullyFormed() {
return [
['civicrm/test/page', FALSE],
['#', FALSE],
['', FALSE],
['/civicrm/test/page', TRUE],
['http://test.com/civicrm/test/page', TRUE],
['https://test.com/civicrm/test/page', TRUE],
];
}

/**
* @param string $url
* @param string $expected
*
* @dataProvider urlsToCheckIfFullyFormed
*/
public function testIsFullyFormedUrl($url, $expected) {
$this->assertEquals($expected, CRM_Core_Resources::isFullyFormedUrl($url));
}

}