[TASK] Fluidification of Avatar.php 65/49865/4
authorMarc Willmann <mw@f7.de>
Mon, 5 Sep 2016 12:30:21 +0000 (14:30 +0200)
committerJan Helke <typo3@helke.de>
Mon, 24 Oct 2016 17:09:29 +0000 (19:09 +0200)
Resolves: #77852
Change-Id: I60aae1b35219ac91e74d62cc57e1a48ec630a831
Reviewed-on: https://review.typo3.org/49865
Reviewed-by: Marco Bresch <marco.bresch@starfinanz.de>
Tested-by: Marco Bresch <marco.bresch@starfinanz.de>
Reviewed-by: Jan Helke <typo3@helke.de>
Tested-by: Jan Helke <typo3@helke.de>
typo3/sysext/backend/Classes/Backend/Avatar/Avatar.php
typo3/sysext/backend/Resources/Private/Templates/Avatar/Main.html [new file with mode: 0644]

index 4c359a0..06625d6 100644 (file)
@@ -19,6 +19,7 @@ use TYPO3\CMS\Core\Imaging\IconFactory;
 use TYPO3\CMS\Core\Service\DependencyOrderingService;
 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Fluid\View\StandaloneView;
 
 /**
  * Avatar renderer class
@@ -38,9 +39,15 @@ class Avatar
     public function __construct()
     {
         $this->validateSortAndInitiateAvatarProviders();
+        $this->view = $this->getFluidTemplateObject();
     }
 
     /**
+     * @var StandaloneView
+     */
+    protected $view;
+
+    /**
      * Render avatar tag
      *
      * @param array $backendUser be_users record
@@ -48,7 +55,7 @@ class Avatar
      * @param bool $showIcon show the record icon
      * @return string
      */
-    public function render(array $backendUser = null, $size = 32, $showIcon = false)
+    public function render(array $backendUser = null, int $size = 32, bool $showIcon = false)
     {
         if (!is_array($backendUser)) {
             $backendUser = $this->getBackendUser()->user;
@@ -58,12 +65,18 @@ class Avatar
         $icon = '';
         if ($showIcon) {
             $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
-            $icon = '<span class="avatar-icon">' . $iconFactory->getIconForRecord('be_users', $backendUser, Icon::SIZE_SMALL)->render() . '</span>';
+            $icon = $iconFactory->getIconForRecord('be_users', $backendUser, Icon::SIZE_SMALL)->render();
         }
 
         $image = $this->getImgTag($backendUser, $size);
 
-        return '<span class="avatar"><span class="avatar-image">' . $image . '</span>' . $icon . '</span>';
+        $this->view->assignMultiple([
+                'image' => $image,
+                'icon' => $icon
+            ]
+        );
+
+        return $this->view->render();
     }
 
     /**
@@ -155,4 +168,29 @@ class Avatar
     {
         return $GLOBALS['BE_USER'];
     }
+
+    /**
+     * Returns a new standalone view, shorthand function
+     *
+     * @param string $filename Which templateFile should be used.
+     *
+     * @return StandaloneView
+     */
+    protected function getFluidTemplateObject(string $filename = null):StandaloneView
+    {
+        /** @var StandaloneView $view */
+        $view = GeneralUtility::makeInstance(StandaloneView::class);
+        $view->setLayoutRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Layouts')]);
+        $view->setPartialRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Partials')]);
+        $view->setTemplateRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates')]);
+
+        if (is_null($filename)) {
+            $filename = 'Main.html';
+        }
+
+        $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates/Avatar/' . $filename));
+
+        $view->getRequest()->setControllerExtensionName('Backend');
+        return $view;
+    }
 }
diff --git a/typo3/sysext/backend/Resources/Private/Templates/Avatar/Main.html b/typo3/sysext/backend/Resources/Private/Templates/Avatar/Main.html
new file mode 100644 (file)
index 0000000..dac008f
--- /dev/null
@@ -0,0 +1,10 @@
+    <span class="avatar">
+    <span class="avatar-image">
+        {image -> f:format.raw()}
+    </span>
+    <f:if condition="{icon}">
+    <span class="avatar-icon">
+        {icon -> f:format.raw()}
+    </span>
+    </f:if>
+</span>
\ No newline at end of file