1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-07-31 14:00:31 +02:00

[ticket/17153] Use twig and avatar helper to render avatars

PHPBB3-17153
This commit is contained in:
Marc Alexander
2023-07-01 09:47:37 +02:00
parent 32e6c637ec
commit 902283c368
5 changed files with 109 additions and 32 deletions

View File

@@ -45,6 +45,8 @@ services:
template.twig.extensions.avatar:
class: phpbb\template\twig\extension\avatar
arguments:
- '@avatar.helper'
tags:
- { name: twig.extension }

View File

@@ -13,16 +13,36 @@
namespace phpbb\template\twig\extension;
use phpbb\avatar\helper;
use phpbb\avatar\manager;
use phpbb\template\twig\environment;
use Twig\Error\Error;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
class avatar extends AbstractExtension
{
/**
* @var helper
*/
private $avatar_helper;
/**
* Constructor for avatar extension
*
* @param helper $avatar_helper
*/
public function __construct(helper $avatar_helper)
{
$this->avatar_helper = $avatar_helper;
}
/**
* Get the name of this extension
*
* @return string
*/
public function getName()
public function getName(): string
{
return 'avatar';
}
@@ -30,13 +50,13 @@ class avatar extends AbstractExtension
/**
* Returns a list of global functions to add to the existing list.
*
* @return \Twig\TwigFunction[] An array of global functions
* @return TwigFunction[] An array of global functions
*/
public function getFunctions(): array
{
return array(
new \Twig\TwigFunction('avatar', array($this, 'get_avatar')),
);
return [
new TwigFunction('avatar', [$this, 'get_avatar'], ['needs_environment' => true]),
];
}
/**
@@ -48,35 +68,30 @@ class avatar extends AbstractExtension
* The mode and row (group_row or user_row) are required.
* The other fields (alt|ignore_config|lazy) are optional.
*
* @uses \phpbb_get_group_avatar()
* @uses \phpbb_get_user_avatar()
*
* @return string The avatar HTML for the specified mode
*/
public function get_avatar()
public function get_avatar(environment $environment, string $mode, array $row, ?string $alt, ?bool $ignore_config, ?bool $lazy): string
{
$args = func_get_args();
$alt = $alt ?? false;
$ignore_config = $ignore_config ?? false;
$lazy = $lazy ?? false;
$row = manager::clean_row($row, $mode);
$avatar = $this->avatar_helper->get_avatar($row, $alt, $ignore_config, $lazy);
$mode = (string) $args[0];
$row = (array) $args[1];
$alt = isset($args[2]) ? (string) $args[2] : false;
$ignore_config = isset($args[3]) ? (bool) $args[3] : false;
$lazy = isset($args[4]) ? (bool) $args[4] : false;
// To prevent having to redefine alt attribute ('USER_AVATAR'|'GROUP_AVATAR'), we check if an alternative has been provided
switch ($mode)
try
{
case 'group':
return $alt ? phpbb_get_group_avatar($row, $alt, $ignore_config, $lazy) : phpbb_get_group_avatar($row);
break;
case 'user':
return $alt ? phpbb_get_user_avatar($row, $alt, $ignore_config, $lazy) : phpbb_get_user_avatar($row);
break;
default:
return '';
break;
return $environment->render('macros/avatar.twig', [
'SRC' => $avatar['lazy'] ? $this->avatar_helper->get_no_avatar_source() : $avatar['src'],
'DATA_SRC' => $avatar['lazy'] ? $avatar['src'] : '',
'WIDTH' => $avatar['width'],
'HEIGHT' => $avatar['height'],
'TITLE' => $avatar['title'],
'LAZY' => $avatar['lazy'],
]);
}
catch (Error $e)
{
return '';
}
}
}

View File

@@ -0,0 +1 @@
{% if SRC %}<img class="avatar" src="{{ SRC }}"{% if DATA_SRC %} data-src="{{ DATA_SRC }}"{% endif %}{% if WIDTH %} width="{{ WIDTH }}"{% endif %}{% if HEIGHT %} height="{{ HEIGHT }}"{% endif %} alt="{{ lang(TITLE) }}">{% endif %}