From 502492c2bcad65c4a1e77f9a2b9595844a39dcb4 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Thu, 16 Jan 2020 01:21:20 -0800 Subject: [PATCH] crmResURL, crmResPath - Allow full path expressions Before ------ In Smarty, there is no way to request values from `Civi::paths()`. After ----- In Smarty, you can request values from `Civi::paths()` using these notations: ``` {crmResURL expr="[civicrm.root]/foo"} {crmResPath expr="[civicrm.root]/foo"} ``` --- CRM/Core/Smarty/plugins/function.crmResPath.php | 7 +++++++ CRM/Core/Smarty/plugins/function.crmResURL.php | 7 +++++++ tests/phpunit/CRM/Core/ResourcesTest.php | 3 +++ 3 files changed, 17 insertions(+) diff --git a/CRM/Core/Smarty/plugins/function.crmResPath.php b/CRM/Core/Smarty/plugins/function.crmResPath.php index dd91bae335..01b47198a2 100644 --- a/CRM/Core/Smarty/plugins/function.crmResPath.php +++ b/CRM/Core/Smarty/plugins/function.crmResPath.php @@ -21,14 +21,21 @@ * Determine the path of a resource file * * @param array $params + * Identify the resource by either 'ext'+'file' or 'expr'. + * * Array with keys: * - ext: string, extension name. see CRM_Core_Resources::getPath * - file: string, relative file path. see CRM_Core_Resources::getPath + * - expr: string, a dynamic path expression. See: \Civi\Core\Paths::getPath() * @param CRM_Core_Smarty $smarty * * @return string */ function smarty_function_crmResPath($params, &$smarty) { + if (!empty($params['expr'])) { + return Civi::paths()->getPath($params['expr']); + } + $res = CRM_Core_Resources::singleton(); if (!array_key_exists('ext', $params)) { $params['ext'] = 'civicrm'; diff --git a/CRM/Core/Smarty/plugins/function.crmResURL.php b/CRM/Core/Smarty/plugins/function.crmResURL.php index 800c56997d..e73e67ac33 100644 --- a/CRM/Core/Smarty/plugins/function.crmResURL.php +++ b/CRM/Core/Smarty/plugins/function.crmResURL.php @@ -21,14 +21,21 @@ * Determine the URL of a resource file * * @param array $params + * Identify the resource by either 'ext'+'file' or 'expr'. + * * Array with keys: * - ext: string, extension name. see CRM_Core_Resources::getUrl * - file: string, relative file path. see CRM_Core_Resources::getUrl + * - expr: string, a dynamic path expression. See: \Civi\Core\Paths::getUrl() * @param CRM_Core_Smarty $smarty * * @return string */ function smarty_function_crmResURL($params, &$smarty) { + if (!empty($params['expr'])) { + return Civi::paths()->getUrl($params['expr'], 'absolute'); + } + $res = CRM_Core_Resources::singleton(); if (!array_key_exists('file', $params)) { $params['file'] = NULL; diff --git a/tests/phpunit/CRM/Core/ResourcesTest.php b/tests/phpunit/CRM/Core/ResourcesTest.php index face0400d7..bfebbb69a9 100644 --- a/tests/phpunit/CRM/Core/ResourcesTest.php +++ b/tests/phpunit/CRM/Core/ResourcesTest.php @@ -306,6 +306,9 @@ class CRM_Core_ResourcesTest extends CiviUnitTestCase { $actual = $smarty->fetch('string:{crmResURL ext=com.example.ext}'); $this->assertEquals('http://ext-dir/com.example.ext/', $actual); + + $actual = $smarty->fetch('string:{crmResURL expr="[civicrm.root]/foo"}'); + $this->assertEquals(Civi::paths()->getUrl('[civicrm.root]/foo'), $actual); } public function testGlob() { -- 2.25.1