‪TYPO3CMS  ‪main
Avatar.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
23 use TYPO3\CMS\Core\Imaging\IconSize;
27 
34 class ‪Avatar
35 {
41  protected array ‪$avatarProviders = [];
42 
43  public function ‪__construct(
44  protected readonly ‪FrontendInterface $cache,
45  protected readonly ‪DependencyOrderingService $dependencyOrderingService,
46  protected readonly ‪IconFactory $iconFactory
47  ) {
49  }
50 
56  public function ‪render(array $backendUser = null, int $size = 32, bool $showIcon = false): string
57  {
58  if (!is_array($backendUser)) {
60  $backendUser = $this->‪getBackendUser()->user;
61  }
62  $cacheId = 'avatar_' . sha1($backendUser['uid'] . $size . $showIcon);
63  $avatar = $this->cache->get($cacheId);
64  if (!$avatar) {
65  $icon = $showIcon ? $this->iconFactory->getIconForRecord('be_users', $backendUser, IconSize::SMALL)->render() : '';
66  $avatar =
67  '<span class="avatar" style="--avatar-size: ' . $size . 'px;">'
68  . '<span class="avatar-image">' . $this->‪getImgTag($backendUser, $size) . '</span>'
69  . ($showIcon ? '<span class="avatar-icon">' . $icon . '</span>' : '')
70  . '</span>';
71  $this->cache->set($cacheId, $avatar);
72  }
73  return $avatar;
74  }
75 
79  protected function ‪getImgTag(array $backendUser, int $size = 32): string
80  {
81  $avatarImage = $this->‪getImage($backendUser, $size);
82  return '<img src="' . htmlspecialchars($avatarImage->getUrl()) . '" ' .
83  'width="' . (int)$avatarImage->getWidth() . '" ' .
84  'height="' . (int)$avatarImage->getHeight() . '" ' .
85  'alt="" ' .
86  'aria-hidden="true" ' .
87  'loading="lazy" />';
88  }
89 
93  protected function ‪getImage(array $backendUser, int $size): ‪Image
94  {
95  foreach ($this->avatarProviders as $provider) {
96  $avatarImage = $provider->getImage($backendUser, $size);
97  if (!empty($avatarImage)) {
98  return $avatarImage;
99  }
100  }
101  return GeneralUtility::makeInstance(
102  Image::class,
103  ‪PathUtility::getPublicResourceWebPath('EXT:core/Resources/Public/Icons/T3Icons/svgs/avatar/avatar-default.svg'),
104  $size,
105  $size
106  );
107  }
108 
114  protected function ‪validateSortAndInitiateAvatarProviders(): void
115  {
117  $providers = ‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['backend']['avatarProviders'] ?? [];
118  if (empty($providers)) {
119  return;
120  }
121  foreach ($providers as ‪$identifier => $configuration) {
122  if (empty($configuration) || !is_array($configuration)) {
123  throw new \RuntimeException(
124  'Missing configuration for avatar provider "' . ‪$identifier . '".',
125  1439317801
126  );
127  }
128  if (!is_string($configuration['provider']) || empty($configuration['provider']) || !class_exists($configuration['provider']) || !is_subclass_of(
129  $configuration['provider'],
130  AvatarProviderInterface::class
131  )) {
132  throw new \RuntimeException(
133  'The avatar provider "' . ‪$identifier . '" defines an invalid provider. Ensure the class exists and implements the "' . AvatarProviderInterface::class . '".',
134  1439317802
135  );
136  }
137  }
138  $orderedProviders = $this->dependencyOrderingService->orderByDependencies($providers);
139  foreach ($orderedProviders as $configuration) {
141  $avatarProvider = GeneralUtility::makeInstance($configuration['provider']);
142  $this->avatarProviders[] = $avatarProvider;
143  }
144  }
145 
147  {
148  return ‪$GLOBALS['BE_USER'];
149  }
150 }
‪TYPO3\CMS\Backend\Backend\Avatar\Avatar\getImgTag
‪getImgTag(array $backendUser, int $size=32)
Definition: Avatar.php:79
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:27
‪TYPO3\CMS\Backend\Backend\Avatar\Avatar\render
‪render(array $backendUser=null, int $size=32, bool $showIcon=false)
Definition: Avatar.php:56
‪TYPO3\CMS\Backend\Backend\Avatar\Avatar
Definition: Avatar.php:35
‪TYPO3\CMS\Backend\Backend\Avatar\Avatar\getImage
‪getImage(array $backendUser, int $size)
Definition: Avatar.php:93
‪TYPO3\CMS\Core\Imaging\IconFactory
Definition: IconFactory.php:34
‪TYPO3\CMS\Core\Utility\PathUtility\getPublicResourceWebPath
‪static getPublicResourceWebPath(string $resourcePath, bool $prefixWithSitePath=true)
Definition: PathUtility.php:97
‪TYPO3\CMS\Backend\Backend\Avatar\Avatar\$avatarProviders
‪array $avatarProviders
Definition: Avatar.php:41
‪TYPO3\CMS\Backend\Backend\Avatar\Avatar\validateSortAndInitiateAvatarProviders
‪validateSortAndInitiateAvatarProviders()
Definition: Avatar.php:114
‪TYPO3\CMS\Core\Service\DependencyOrderingService
Definition: DependencyOrderingService.php:32
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:22
‪TYPO3\CMS\Backend\Backend\Avatar
Definition: Avatar.php:18
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Backend\Backend\Avatar\Image
Definition: Image.php:23
‪TYPO3\CMS\Backend\Backend\Avatar\Avatar\__construct
‪__construct(protected readonly FrontendInterface $cache, protected readonly DependencyOrderingService $dependencyOrderingService, protected readonly IconFactory $iconFactory)
Definition: Avatar.php:43
‪TYPO3\CMS\Backend\Backend\Avatar\Avatar\getBackendUser
‪getBackendUser()
Definition: Avatar.php:146
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37