mirror of
https://github.com/phpbb/phpbb.git
synced 2025-06-08 04:18:52 +00:00
[ticket/17153] Use twig and avatar helper to render avatars
PHPBB3-17153
This commit is contained in:
parent
32e6c637ec
commit
902283c368
5 changed files with 109 additions and 32 deletions
|
@ -45,6 +45,8 @@ services:
|
||||||
|
|
||||||
template.twig.extensions.avatar:
|
template.twig.extensions.avatar:
|
||||||
class: phpbb\template\twig\extension\avatar
|
class: phpbb\template\twig\extension\avatar
|
||||||
|
arguments:
|
||||||
|
- '@avatar.helper'
|
||||||
tags:
|
tags:
|
||||||
- { name: twig.extension }
|
- { name: twig.extension }
|
||||||
|
|
||||||
|
|
|
@ -13,16 +13,36 @@
|
||||||
|
|
||||||
namespace phpbb\template\twig\extension;
|
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\Extension\AbstractExtension;
|
||||||
|
use Twig\TwigFunction;
|
||||||
|
|
||||||
class avatar extends AbstractExtension
|
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
|
* Get the name of this extension
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getName()
|
public function getName(): string
|
||||||
{
|
{
|
||||||
return 'avatar';
|
return 'avatar';
|
||||||
}
|
}
|
||||||
|
@ -30,13 +50,13 @@ class avatar extends AbstractExtension
|
||||||
/**
|
/**
|
||||||
* Returns a list of global functions to add to the existing list.
|
* 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
|
public function getFunctions(): array
|
||||||
{
|
{
|
||||||
return array(
|
return [
|
||||||
new \Twig\TwigFunction('avatar', array($this, 'get_avatar')),
|
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 mode and row (group_row or user_row) are required.
|
||||||
* The other fields (alt|ignore_config|lazy) are optional.
|
* 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
|
* @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];
|
try
|
||||||
$row = (array) $args[1];
|
{
|
||||||
$alt = isset($args[2]) ? (string) $args[2] : false;
|
return $environment->render('macros/avatar.twig', [
|
||||||
$ignore_config = isset($args[3]) ? (bool) $args[3] : false;
|
'SRC' => $avatar['lazy'] ? $this->avatar_helper->get_no_avatar_source() : $avatar['src'],
|
||||||
$lazy = isset($args[4]) ? (bool) $args[4] : false;
|
'DATA_SRC' => $avatar['lazy'] ? $avatar['src'] : '',
|
||||||
|
'WIDTH' => $avatar['width'],
|
||||||
// To prevent having to redefine alt attribute ('USER_AVATAR'|'GROUP_AVATAR'), we check if an alternative has been provided
|
'HEIGHT' => $avatar['height'],
|
||||||
switch ($mode)
|
'TITLE' => $avatar['title'],
|
||||||
|
'LAZY' => $avatar['lazy'],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
catch (Error $e)
|
||||||
{
|
{
|
||||||
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 '';
|
return '';
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
1
phpBB/styles/all/template/macros/avatar.twig
Normal file
1
phpBB/styles/all/template/macros/avatar.twig
Normal 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 %}
|
|
@ -89,6 +89,14 @@ class phpbb_template_extension_test extends phpbb_template_template_test_case
|
||||||
$enabled_drivers = $class->getProperty('enabled_drivers');
|
$enabled_drivers = $class->getProperty('enabled_drivers');
|
||||||
$enabled_drivers->setAccessible(true);
|
$enabled_drivers->setAccessible(true);
|
||||||
$enabled_drivers->setValue($class, false);
|
$enabled_drivers->setValue($class, false);
|
||||||
|
$avatar_helper = new phpbb\avatar\helper(
|
||||||
|
$config,
|
||||||
|
$phpbb_dispatcher,
|
||||||
|
$lang,
|
||||||
|
$phpbb_container->get('avatar.manager'),
|
||||||
|
$phpbb_path_helper,
|
||||||
|
new \phpbb\user($lang, '\phpbb\datetime')
|
||||||
|
);
|
||||||
|
|
||||||
$this->template_path = $this->test_path . '/templates';
|
$this->template_path = $this->test_path . '/templates';
|
||||||
|
|
||||||
|
@ -122,7 +130,7 @@ class phpbb_template_extension_test extends phpbb_template_template_test_case
|
||||||
$this->user,
|
$this->user,
|
||||||
[
|
[
|
||||||
new \phpbb\template\twig\extension($context, $twig, $this->lang),
|
new \phpbb\template\twig\extension($context, $twig, $this->lang),
|
||||||
new \phpbb\template\twig\extension\avatar(),
|
new \phpbb\template\twig\extension\avatar($avatar_helper),
|
||||||
new \phpbb\template\twig\extension\config($config),
|
new \phpbb\template\twig\extension\config($config),
|
||||||
new \phpbb\template\twig\extension\icon($this->user),
|
new \phpbb\template\twig\extension\icon($this->user),
|
||||||
new \phpbb\template\twig\extension\username(),
|
new \phpbb\template\twig\extension\username(),
|
||||||
|
@ -153,7 +161,7 @@ class phpbb_template_extension_test extends phpbb_template_template_test_case
|
||||||
],
|
],
|
||||||
[],
|
[],
|
||||||
[],
|
[],
|
||||||
'<img class="avatar" src="download/avatar/great_avatar.png" width="90" height="90" alt="foo" />',
|
'<img class="avatar" src="download/avatar/great_avatar.png" width="90" height="90" alt="foo">',
|
||||||
[]
|
[]
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
|
@ -171,7 +179,7 @@ class phpbb_template_extension_test extends phpbb_template_template_test_case
|
||||||
],
|
],
|
||||||
[],
|
[],
|
||||||
[],
|
[],
|
||||||
'<img class="avatar" src="phpBB/styles//theme/images/no_avatar.gif" data-src="download/avatar/great_avatar.png" width="90" height="90" alt="foo" />',
|
'<img class="avatar" src="phpBB/styles//theme/images/no_avatar.gif" data-src="download/avatar/great_avatar.png" width="90" height="90" alt="foo">',
|
||||||
[]
|
[]
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
|
@ -190,6 +198,56 @@ class phpbb_template_extension_test extends phpbb_template_template_test_case
|
||||||
'',
|
'',
|
||||||
[]
|
[]
|
||||||
],
|
],
|
||||||
|
[
|
||||||
|
'avatar_group.html',
|
||||||
|
[
|
||||||
|
'row' => [
|
||||||
|
'group_avatar' => 'great_avatar.png',
|
||||||
|
'group_avatar_type' => 'avatar.driver.upload',
|
||||||
|
'group_avatar_width' => 90,
|
||||||
|
'group_avatar_height' => 90,
|
||||||
|
],
|
||||||
|
'alt' => 'foo'
|
||||||
|
],
|
||||||
|
[],
|
||||||
|
[],
|
||||||
|
'<img class="avatar" src="download/avatar/great_avatar.png" width="90" height="90" alt="foo">',
|
||||||
|
[]
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'avatar_group.html',
|
||||||
|
[
|
||||||
|
'row' => [
|
||||||
|
'group_avatar' => 'great_avatar.png',
|
||||||
|
'group_avatar_type' => 'avatar.driver.upload',
|
||||||
|
'group_avatar_width' => 90,
|
||||||
|
'group_avatar_height' => 90,
|
||||||
|
],
|
||||||
|
'alt' => 'foo',
|
||||||
|
'ignore_config' => true,
|
||||||
|
'lazy' => true,
|
||||||
|
],
|
||||||
|
[],
|
||||||
|
[],
|
||||||
|
'<img class="avatar" src="phpBB/styles//theme/images/no_avatar.gif" data-src="download/avatar/great_avatar.png" width="90" height="90" alt="foo">',
|
||||||
|
[]
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'avatar_group.html',
|
||||||
|
[
|
||||||
|
'row' => [
|
||||||
|
'group_avatar' => 'foo@bar.com',
|
||||||
|
'group_avatar_type' => 'avatar.driver.gravatar',
|
||||||
|
'group_avatar_width' => 90,
|
||||||
|
'group_avatar_height' => 90,
|
||||||
|
],
|
||||||
|
'alt' => 'foo'
|
||||||
|
],
|
||||||
|
[],
|
||||||
|
[],
|
||||||
|
'',
|
||||||
|
[]
|
||||||
|
],
|
||||||
[
|
[
|
||||||
'extension_username_test.html',
|
'extension_username_test.html',
|
||||||
[
|
[
|
||||||
|
|
1
tests/template/templates/avatar_group.html
Normal file
1
tests/template/templates/avatar_group.html
Normal file
|
@ -0,0 +1 @@
|
||||||
|
{{ avatar('group', row, alt, ignore_config, lazy) }}
|
Loading…
Add table
Reference in a new issue