From 9de5aa1b82d4db922a01f834de5488e204cad0f5 Mon Sep 17 00:00:00 2001 From: Andrew Hunt Date: Wed, 13 May 2020 18:34:07 -0400 Subject: [PATCH] icon helpers - allow specification of HTML attributes --- CRM/Core/Page.php | 30 ++++++++++++++++++++++---- CRM/Core/Smarty/plugins/block.icon.php | 7 +++++- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/CRM/Core/Page.php b/CRM/Core/Page.php index 5e7844367c..f83b170f93 100644 --- a/CRM/Core/Page.php +++ b/CRM/Core/Page.php @@ -431,23 +431,45 @@ class CRM_Core_Page { * @param bool $condition * Whether to display anything at all. This helps simplify code when a * checkmark should appear if something is true. + * @param array $attribs + * Attributes to set or override on the icon element. Any standard + * attribute can be unset by setting the value to an empty string. * * @return string * The whole bit to drop in. */ - public static function crmIcon($icon, $text = NULL, $condition = TRUE) { + public static function crmIcon($icon, $text = NULL, $condition = TRUE, $attribs = []) { if (!$condition) { return ''; } + + // Add icon classes to any that might exist in $attribs + $classes = array_key_exists('class', $attribs) ? explode(' ', $attribs['class']) : []; + $classes[] = 'crm-i'; + $classes[] = $icon; + $attribs['class'] = implode(' ', array_unique($classes)); + + $standardAttribs = ['aria-hidden' => 'true']; if ($text === NULL || $text === '') { $title = $sr = ''; } else { - $text = htmlspecialchars($text); - $title = " title=\"$text\""; + $standardAttribs['title'] = $text; $sr = "$text"; } - return "$sr"; + + // Assemble attribs + $attribString = ''; + // Strip out title if $attribs specifies a blank title + $attribs = array_merge($standardAttribs, $attribs); + foreach ($attribs as $attrib => $val) { + if (strlen($val)) { + $val = htmlspecialchars($val); + $attribString .= " $attrib=\"$val\""; + } + } + + return "$sr"; } } diff --git a/CRM/Core/Smarty/plugins/block.icon.php b/CRM/Core/Smarty/plugins/block.icon.php index 0cdad70186..8fb0213b49 100644 --- a/CRM/Core/Smarty/plugins/block.icon.php +++ b/CRM/Core/Smarty/plugins/block.icon.php @@ -25,6 +25,7 @@ * @param $params * - condition: if present and falsey, return empty * - icon: the icon class to display instead of fa-check + * - anything else is passed along as attributes for the icon * * @param $text * The translated text to include in the icon's title and screen-reader text. @@ -36,5 +37,9 @@ function smarty_block_icon($params, $text, &$smarty) { $condition = array_key_exists('condition', $params) ? $params['condition'] : 1; $icon = $params['icon'] ?? 'fa-check'; - return CRM_Core_Page::crmIcon($icon, $text, $condition); + $dontPass = [ + 'condition' => 1, + 'icon' => 1, + ]; + return CRM_Core_Page::crmIcon($icon, $text, $condition, array_diff_key($params, $dontPass)); } -- 2.25.1