diff --git a/phpBB/phpbb/template/twig/extension/icon.php b/phpBB/phpbb/template/twig/extension/icon.php index 5b9bb854a2..f96ed94821 100644 --- a/phpBB/phpbb/template/twig/extension/icon.php +++ b/phpBB/phpbb/template/twig/extension/icon.php @@ -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. * diff --git a/tests/template/extension_test.php b/tests/template/extension_test.php index 25624bc273..5d73aed91e 100644 --- a/tests/template/extension_test.php +++ b/tests/template/extension_test.php @@ -311,6 +311,50 @@ class phpbb_template_extension_test extends phpbb_template_template_test_case ' Pencil icon' ], + /** 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', + ], + ' + Topic icon', + ], + /** 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', + ], + ' + Topic icon', + ], /** Iconify: default */ [ [