From 79a18c21d066b39f244726e5a6052176a763259b Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Tue, 20 Jan 2015 21:32:05 -0500 Subject: [PATCH] CRM-15789 - Move htmlAttributes helper to utils class ---------------------------------------- * CRM-15789: Add icons to submit buttons https://issues.civicrm.org/jira/browse/CRM-15789 --- CRM/Core/Smarty/plugins/block.crmButton.php | 8 +++----- .../Smarty/plugins/function.crmAttributes.php | 9 ++------- CRM/Utils/String.php | 15 +++++++++++++++ 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/CRM/Core/Smarty/plugins/block.crmButton.php b/CRM/Core/Smarty/plugins/block.crmButton.php index 55cb32ed6e..6bc7f906ca 100644 --- a/CRM/Core/Smarty/plugins/block.crmButton.php +++ b/CRM/Core/Smarty/plugins/block.crmButton.php @@ -33,8 +33,6 @@ * */ -require_once 'CRM/Core/Smarty/plugins/function.crmAttributes.php'; - /** * Generate the html for a button-style link * @@ -49,7 +47,7 @@ require_once 'CRM/Core/Smarty/plugins/function.crmAttributes.php'; * The generated html. */ function smarty_block_crmButton($params, $text, &$smarty) { - // Generate url (pass 'html' param as false to avoid double-encode by crmAttributes) + // Generate url (pass 'html' param as false to avoid double-encode by htmlAttributes) $params['href'] = CRM_Utils_System::crmURL($params + array('h' => FALSE)); // Always add class 'button' - fixme probably should be crm-button $params['class'] = 'button ' . CRM_Utils_Array::value('class', $params, ''); @@ -57,6 +55,6 @@ function smarty_block_crmButton($params, $text, &$smarty) { $icon = CRM_Utils_Array::value('icon', $params, 'pencil'); // All other params are treated as html attributes CRM_Utils_Array::remove($params, 'icon', 'p', 'q', 'a', 'f', 'h', 'fb', 'fe'); - $attributes = smarty_function_crmAttributes(array('a' => $params), CRM_Core_DAO::$_nullObject); - return " $text"; + $attributes = CRM_Utils_String::htmlAttributes($params); + return " $text"; } diff --git a/CRM/Core/Smarty/plugins/function.crmAttributes.php b/CRM/Core/Smarty/plugins/function.crmAttributes.php index 32ff894c3e..64bcf71333 100644 --- a/CRM/Core/Smarty/plugins/function.crmAttributes.php +++ b/CRM/Core/Smarty/plugins/function.crmAttributes.php @@ -23,7 +23,7 @@ | GNU Affero General Public License or the licensing of CiviCRM, | | see the CiviCRM license FAQ at http://civicrm.org/licensing | +--------------------------------------------------------------------+ - */ +*/ /** * @@ -41,13 +41,8 @@ * @param CRM_Core_Smarty $smarty * * @return string - * @throws Exception */ function smarty_function_crmAttributes($params, &$smarty) { - $output = ''; $attributes = isset($params['a']) ? $params['a'] : array(); - foreach ($attributes as $name => $vals) { - $output .= " $name=\"" . htmlspecialchars(implode(' ', (array) $vals)) . '"'; - } - return $output; + return CRM_Utils_String::htmlAttributes($attributes); } diff --git a/CRM/Utils/String.php b/CRM/Utils/String.php index a3606c2558..ecf80f8935 100644 --- a/CRM/Utils/String.php +++ b/CRM/Utils/String.php @@ -777,4 +777,19 @@ class CRM_Utils_String { return str_replace('&', '&', $htmlUrl); } + /** + * Formats a string of attributes for insertion in an html tag + * + * @param array $attributes + * + * @return string + */ + public static function htmlAttributes($attributes) { + $output = ''; + foreach ($attributes as $name => $vals) { + $output .= " $name=\"" . htmlspecialchars(implode(' ', (array) $vals)) . '"'; + } + return ltrim($output); + } + } -- 2.25.1