1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-04-12 03:42:05 +02:00

[ticket/15538] Allow array of icons: icon_name => boolean

PHPBB3-15538
This commit is contained in:
mrgoldy 2019-11-30 15:55:21 +01:00
parent 23a2c0c9c7
commit 811fbbeb92
2 changed files with 92 additions and 2 deletions

View File

@ -68,6 +68,7 @@ class icon extends \Twig\Extension\AbstractExtension
public function icon(environment $environment, $type, $icon, $title = '', $hidden = false, $classes = '', array $attributes = [])
{
$type = strtolower($type);
$icon = is_array($icon) ? $this->get_first_icon($icon) : $icon;
if (empty($icon))
{
@ -142,8 +143,15 @@ class icon extends \Twig\Extension\AbstractExtension
// If no PNG or SVG icon was found, display a default 404 SVG icon.
if ($not_found)
{
$file = $environment->load('svg/404.svg');
$source = $this->prepare_svg($file, $view_box);
try
{
$file = $environment->load('svg/404.svg');
$source = $this->prepare_svg($file, $view_box);
}
catch (\Twig\Error\Error $e)
{
return '';
}
$type = 'svg';
$icon = '404';
@ -238,6 +246,44 @@ class icon extends \Twig\Extension\AbstractExtension
return $string;
}
/**
* Finds the first icon that has a "true" value and returns it.
*
* This allows sending an array to the Icon() function,
* where the keys are the icon names and the values are their checks.
*
* {{ Icon('font', {
* 'bullhorn': topicrow.S_POST_GLOBAL or topicrow.S_POST_ANNOUNCE,
* 'star': topicrow.S_POST_STICKY,
* 'lock': topicrow.S_TOPIC_LOCKED,
* 'fire': topicrow.S_TOPIC_HOT,
* 'file': true,
* }, 'MY_TITLE', true) }}
*
* @param array $icons Array of icons and their booleans
* @return string The first 'true' icon
*/
protected function get_first_icon(array $icons)
{
foreach ($icons as $icon => $boolean)
{
// In case the key is not a string,
// this icon does not have a check
// so instantly return it
if (!is_string($icon))
{
return $boolean;
}
if ($boolean)
{
return $icon;
}
}
return '';
}
/**
* Implode an associated array of attributes to a string for usage in a template.
*

View File

@ -311,6 +311,50 @@ class phpbb_template_extension_test extends phpbb_template_template_test_case
'<i class="o-icon o-icon-font fa-pencil a-class another-class" title="Pencil icon" aria-hidden="true" data-attr-1="true" data-attr-2="two"></i>
<span class="sr-only">Pencil icon</span>'
],
/** Font: icons array */
[
[
'type' => 'font',
'icon' => [
'bullhorn' => false,
'star' => false,
'lock' => true,
'fire' => false,
'file' => true,
],
'title' => 'ICON_TOPIC',
'hidden' => false,
'classes' => '',
'attributes' => [],
],
[
'ICON_TOPIC' => 'Topic icon',
],
'<i class="o-icon o-icon-font fa-lock"></i>
<span>Topic icon</span>',
],
/** Font: icons array with no key for the default */
[
[
'type' => 'font',
'icon' => [
'bullhorn' => false,
'star' => false,
'lock' => false,
'fire' => false,
'file',
],
'title' => 'ICON_TOPIC',
'hidden' => false,
'classes' => '',
'attributes' => [],
],
[
'ICON_TOPIC' => 'Topic icon',
],
'<i class="o-icon o-icon-font fa-file"></i>
<span>Topic icon</span>',
],
/** Iconify: default */
[
[