[ticket/15538] Revert back to Icon function

PHPBB3-15538
This commit is contained in:
mrgoldy 2019-10-04 12:58:39 +02:00 committed by Marc Alexander
parent 341266cc71
commit ba5e73bfea
No known key found for this signature in database
GPG key ID: 50E0D2423696F995
9 changed files with 17 additions and 435 deletions

View file

@ -61,18 +61,6 @@ services:
tags: tags:
- { name: twig.extension } - { name: twig.extension }
template.twig.extensions.implode:
class: phpbb\template\twig\extension\implode
tags:
- { name: twig.extension }
template.twig.extensions.macro:
class: phpbb\template\twig\extension\macro
arguments:
- '@template.twig.environment'
tags:
- { name: twig.extension }
template.twig.extensions.routing: template.twig.extensions.routing:
class: phpbb\template\twig\extension\routing class: phpbb\template\twig\extension\routing
arguments: arguments:

View file

@ -1,150 +0,0 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\template\twig\extension;
use phpbb\template\twig\environment;
class icon extends \Twig\Extension\AbstractExtension
{
/** @var \phpbb\user */
protected $user;
/**
* Constructor.
*
* @param \phpbb\user $user User object
*/
public function __construct(\phpbb\user $user)
{
$this->user = $user;
}
/**
* Returns the name of this extension.
*
* @return string The extension name
*/
public function getName()
{
return 'icon';
}
/**
* Returns a list of filters to add to the existing list.
*
* @return \Twig\TwigFilter[] Array of twig filters
*/
public function getFilters()
{
return [
new \Twig\TwigFilter('png_path', [$this, 'png_path'], ['needs_environment' => true]),
];
}
/**
* Returns a list of functions to add to the existing list.
*
* @return \Twig\TwigFunction[] Array of twig functions
*/
public function getFunctions()
{
return [
new \Twig\TwigFunction('Svg_clean', [$this, 'svg_clean'], ['needs_environment' => true]),
];
}
/**
* Create a path to a PNG template icon.
*
* @param environment $environment Twig environment object
* @param string $icon The icon name
* @return string
*/
public function png_path(environment $environment, $icon)
{
$board_url = defined('PHPBB_USE_BOARD_URL_PATH') && PHPBB_USE_BOARD_URL_PATH;
$web_path = $board_url ? generate_board_url() . '/' : $environment->get_web_root_path();
$style_path = $this->user->style['style_path'];
return "{$web_path}styles/{$style_path}/theme/icons/png/{$icon}.png";
}
/**
* Load and clean an SVG template icon.
*
* @param environment $environment Twig environment object
* @param string $icon The icon name
* @return string
*/
public function svg_clean(environment $environment, $icon)
{
try
{
$file = $environment->load('icons/svg/' . $icon . '.svg');
}
catch (\Twig\Error\Error $e)
{
return '';
}
$src = $file->getSourceContext();
$svg = $src->getCode();
$doc = new \DOMDocument();
$doc->preserveWhiteSpace = false;
$doc->strictErrorChecking = false;
if (!$doc->loadXML($svg))
{
return '';
}
foreach ($doc->childNodes as $child)
{
if ($child->nodeType === XML_DOCUMENT_TYPE_NODE)
{
$child->parentNode->removeChild($child);
}
}
$xpath = new \DOMXPath($doc);
foreach ($xpath->query('//svg | //title') as $element)
{
if ($element->nodeName === 'svg')
{
$children = [];
/** @var \DOMNode $node */
foreach ($element->childNodes as $node)
{
$children[] = $node;
}
/** @var \DOMNode $child */
foreach ($children as $child)
{
$element->parentNode->insertBefore($child, $element);
}
}
$element->parentNode->removeChild($element);
}
$string = $doc->saveXML($doc->documentElement, LIBXML_NOEMPTYTAG);
$string = preg_replace('/\s+/', ' ', $string);
return $string;
}
}

View file

@ -1,181 +0,0 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\template\twig\extension;
class implode extends \Twig\Extension\AbstractExtension
{
/**
* Returns the name of this extension.
*
* @return string The extension name
*/
public function getName()
{
return 'implode';
}
/**
* Returns a list of functions to add to the existing list.
*
* @return \Twig\TwigFunction[] Array of twig functions
*/
public function getFunctions()
{
return [
new \Twig\TwigFunction('Implode_attributes', [$this, 'implode_attributes']),
new \Twig\TwigFunction('Implode_classes', [$this, 'implode_classes']),
];
}
/**
* Implode an array of attributes to a string.
*
* This string will be prepended by a space for ease-of-use.
*
* Examples would be:
* Implode_attributes('checked', {'data-ajax': 'true'})
* Implode_attributes(['checked', {'data-ajax': 'true'}])
*
* @param mixed $arguments Attributes to implode
* @return string The attributes string
*/
public function implode_attributes(...$arguments)
{
$string = '';
$attributes = [];
foreach ($arguments as $argument)
{
if (is_string($argument))
{
$attributes[] = $argument;
}
else if (is_array($argument))
{
foreach ($argument as $key => $value)
{
if (is_integer($key) && is_string($value))
{
$attributes[] = $value;
}
else if (is_array($value))
{
foreach ($value as $k => $v)
{
if (is_integer($k) && is_string($v))
{
$attributes[] = $v;
}
else
{
$attributes[$k] = $v;
}
}
}
else
{
$attributes[$key] = $value;
}
}
}
}
foreach ($attributes as $attribute => $value)
{
if (is_string($attribute))
{
$value = is_bool($value) ? ($value ? 'true' : 'false') : $value;
$string .= ' ' . $attribute . '="' . $value . '"';
}
else
{
$string .= ' ' . $value;
}
}
return $string;
}
/**
* Implode an array or classes to a string.
*
* This string will be prepended with a space for ease-of-use.
*
* Conditions can be added to the classes, which will determine if the classes is added to the string.
* @see https://twig.symfony.com/doc/2.x/functions/html_classes.html
*
* An example would be:
* Implode_classes('a-class', 'another-class', {
* 'reported-class': S_POST_REPORTED,
* 'hidden-class': S_POST_HIDDEN,
* })
*
* This function differs from the html_classes function linked above,
* in that it allows another depth level, so it also supports a single argument.
*
* An example would be:
* Implode_classes(['a-class', 'another-class', {
* 'reported-class': S_POST_REPORTED,
* 'hidden-class': S_POST_HIDDEN,
* }])
*
* @param mixed $arguments The classes to implode
* @return string The classes string prepended with a space
*/
public function implode_classes(...$arguments)
{
$classes = [];
foreach ($arguments as $argument)
{
if (is_string($argument))
{
$classes[] = $argument;
}
else if (is_array($argument))
{
foreach ($argument as $key => $value)
{
if (is_integer($key) && is_string($value))
{
$classes[] = $value;
}
else if (is_string($key))
{
if ($value)
{
$classes[] = $key;
}
}
else if (is_array($value))
{
foreach ($value as $class => $condition)
{
if ($condition)
{
$classes[] = $class;
}
}
}
}
}
}
$string = implode(' ', array_unique($classes));
return $string ? ' ' . $string : $string;
}
}

View file

@ -1,55 +0,0 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\template\twig\extension;
use phpbb\template\twig\environment;
class macro extends \Twig\Extension\AbstractExtension implements \Twig\Extension\GlobalsInterface
{
/** @var environment */
protected $twig;
/**
* Constructor.
*
* @param environment $twig Twig environment object
*/
public function __construct(environment $twig)
{
$this->twig = $twig;
}
/**
* Returns the name of this extension.
*
* @return string The extension name
*/
public function getName()
{
return 'macros';
}
/**
* Returns a list of global variables to add to the existing list.
*
* @throws \Twig\Error\Error
* @return array An array of global variables
*/
public function getGlobals()
{
return [
'macros' => $this->twig->loadTemplate('macros/macros.twig'),
];
}
}

View file

@ -1,34 +0,0 @@
{# Wrapper function #}
{% macro Icon(type, icon, classes = '', title = '', hidden = false, attributes = {}) %}
{% set type = type|capitalize %}
{% if type in ['Fa', 'Png', 'Svg'] %}
{{ attribute(_self, type, [icon, classes, title, hidden, attributes]) }}
{% endif %}
{% endmacro Icon %}
{# FA icons #}
{% macro Fa(icon, classes = '', title = '', hidden = false, attributes = {}) %}
<i class="o-icon font fa-{{ icon ~ Implode_classes(classes) }}"{% if hidden %}{% if title %} title="{{ lang(title) }}"{% endif %} aria-hidden="true"{% endif %}{{ Implode_attributes(attributes) }}></i>
{% if title %}<span{% if hidden %} class="sr-only"{% endif %}>{{ lang(title) }}</span>{% endif %}
{% endmacro Fa %}
{# PNG icons #}
{% macro Png(icon, classes = '', title = '', hidden = false, attributes = {}) %}
<img class="o-icon png png-{{ icon ~ Implode_classes(classes) }}" src="{{ icon|png_path }}" alt="{{ lang(title) }}"{{ _self.attributes(attributes) }}
{% endmacro Png %}
{# SVG icons #}
{% macro Svg(icon, classes = '', title = '', hidden = false, attributes = {}) %}
{% set title_id = title ? title|lower|replace({' ': '_'}) ~ '-' ~ random() %}
<svg class="o-icon svg svg-{{ icon ~ Implode_classes(classes) }}" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"{% if title %}{% if hidden %} aria-hidden="true"{% endif %} aria-labelledby="{{ title_id }}"{% endif %} role="img"{{ Implode_attributes(attributes) }}>
{% if title %}
<title id="{{ title_id }}">{{ lang(title) }}</title>
{% endif %}
{{ Svg_clean(icon) }}
</svg>
{% endmacro Svg %}
{% from _self import Icon as Icon %}

View file

@ -0,0 +1,4 @@
{% spaceless %}
<i class="o-icon font fa-{{ ICON ~ (CLASSES ? ' ' ~ CLASSES) }}"{% if S_HIDDEN %}{% if TITLE %} title="{{ lang(TITLE) }}"{% endif %} aria-hidden="true"{% endif %}{{ ATTRIBUTES }}></i>
{% if TITLE %}<span{% if S_HIDDEN %} class="sr-only"{% endif %}>{{ lang(TITLE) }}</span>{% endif %}
{% endspaceless %}

View file

@ -0,0 +1,3 @@
{% spaceless %}
<img class="o-icon png png-{{ ICON ~ (CLASSES ? ' ' ~ CLASSES) }}" src="{{ SOURCE }}" alt="{{ lang(TITLE) }}"{{ ATTRIBUTES }} />
{% endspaceless %}

View file

@ -0,0 +1,9 @@
{% spaceless %}
{% set TITLE_ID = TITLE ? TITLE|lower|replace({' ': '_'}) ~ '-' ~ random() %}
<svg class="o-icon svg svg-{{ ICON ~ (CLASSES ? ' ' ~ CLASSES) }}" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"{% if TITLE %}{% if S_HIDDEN %} aria-hidden="true"{% endif %} aria-labelledby="{{ TITLE_ID }}"{% endif %} role="img"{{ ATTRIBUTES }}>
{% if TITLE %}<title id="{{ TITLE_ID }}">{{ lang(TITLE) }}</title>{% endif %}
{{ SOURCE }}
</svg>
{% endspaceless %}

View file

@ -11,8 +11,6 @@
<!-- ENDIF --> <!-- ENDIF -->
<!-- EVENT index_body_markforums_after --> <!-- EVENT index_body_markforums_after -->
Test: {{ macros.Icon('fa', 'phone') }}
<!-- INCLUDE forumlist_body.html --> <!-- INCLUDE forumlist_body.html -->
<!-- EVENT index_body_forumlist_body_after --> <!-- EVENT index_body_forumlist_body_after -->