[TASK] Fluidification of UserToolbarItem 93/50393/3
authorMarc Willmann <mw@f7.de>
Wed, 26 Oct 2016 15:41:16 +0000 (17:41 +0200)
committerAnja Leichsenring <aleichsenring@ab-softlab.de>
Wed, 26 Oct 2016 19:07:30 +0000 (21:07 +0200)
Resolves: #78451
Releases: master
Change-Id: If4d40e41abc604b430168293446ab5dfd579ca3e
Reviewed-on: https://review.typo3.org/50393
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Gianluigi Martino <gmartino27@gmail.com>
Tested-by: Gianluigi Martino <gmartino27@gmail.com>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
typo3/sysext/backend/Classes/Backend/ToolbarItems/UserToolbarItem.php
typo3/sysext/backend/Resources/Private/Templates/ToolbarItems/UserToolbarItem.html [new file with mode: 0644]
typo3/sysext/backend/Resources/Private/Templates/ToolbarItems/UserToolbarItemDropDown.html [new file with mode: 0644]

index 7a38a23..86fbf99 100644 (file)
@@ -15,13 +15,13 @@ namespace TYPO3\CMS\Backend\Backend\ToolbarItems;
  */
 
 use TYPO3\CMS\Backend\Backend\Avatar\Avatar;
-use TYPO3\CMS\Backend\Domain\Model\Module\BackendModule;
 use TYPO3\CMS\Backend\Domain\Repository\Module\BackendModuleRepository;
 use TYPO3\CMS\Backend\Toolbar\ToolbarItemInterface;
 use TYPO3\CMS\Backend\Utility\BackendUtility;
 use TYPO3\CMS\Core\Imaging\Icon;
 use TYPO3\CMS\Core\Imaging\IconFactory;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Fluid\View\StandaloneView;
 
 /**
  * User toolbar item
@@ -59,7 +59,6 @@ class UserToolbarItem implements ToolbarItemInterface
     public function getItem()
     {
         $backendUser = $this->getBackendUser();
-        $languageService = $this->getLanguageService();
 
         /** @var Avatar $avatar */
         $avatar =  GeneralUtility::makeInstance(Avatar::class);
@@ -67,22 +66,17 @@ class UserToolbarItem implements ToolbarItemInterface
 
         $realName = $backendUser->user['realName'];
         $username = $backendUser->user['username'];
-        $label = $realName ?: $username;
-        $title = $username;
 
-        // Switch user mode
-        if ($backendUser->user['ses_backuserid']) {
-            $title = $languageService->getLL('switchtouser') . ': ' . $username;
-            $label = $languageService->getLL('switchtousershort') . ' ' . ($realName ? $realName . ' (' . $username . ')' : $username);
-        }
-
-        $html = [];
-        $html[] = '<span class="toolbar-item-avatar">' . $icon . '</span>';
-        $html[] = '<span class="toolbar-item-name" title="' . htmlspecialchars($title) . '">';
-        $html[] = htmlspecialchars($label);
-        $html[] = '</span>';
+        $view = $this->getFluidTemplateObject('UserToolbarItem.html');
+        $view->assignMultiple([
+                'username' => $username,
+                'realName' => $realName,
+                'switchUserMode' => $backendUser->user['ses_backuserid'],
+                'icon' => $icon
+            ]
+        );
 
-        return implode(LF, $html);
+        return $view->render();
     }
 
     /**
@@ -93,46 +87,24 @@ class UserToolbarItem implements ToolbarItemInterface
     public function getDropDown()
     {
         $backendUser = $this->getBackendUser();
-        $languageService = $this->getLanguageService();
-
-        $dropdown = [];
-        $dropdown[] = '<div class="dropdown-table">';
 
         /** @var BackendModuleRepository $backendModuleRepository */
         $backendModuleRepository = GeneralUtility::makeInstance(BackendModuleRepository::class);
         /** @var \TYPO3\CMS\Backend\Domain\Model\Module\BackendModule $userModuleMenu */
         $userModuleMenu = $backendModuleRepository->findByModuleName('user');
-        if ($userModuleMenu != false && $userModuleMenu->getChildren()->count() > 0) {
-            foreach ($userModuleMenu->getChildren() as $module) {
-                /** @var BackendModule $module */
-                $dropdown[] = '<div'
-                    . ' class="dropdown-table-row"'
-                    . ' id="' . htmlspecialchars($module->getName()) . '"'
-                    . ' data-modulename="' . htmlspecialchars($module->getName()) . '"'
-                    . ' data-navigationcomponentid="' . htmlspecialchars($module->getNavigationComponentId()) . '"'
-                    . ' data-navigationframescript="' . htmlspecialchars($module->getNavigationFrameScript()) . '"'
-                    . ' data-navigationframescriptparameters="' . htmlspecialchars($module->getNavigationFrameScriptParameters()) . '"'
-                    . '>';
-                $dropdown[] = '<div class="dropdown-table-column dropdown-table-icon">' . $module->getIcon() . '</div>';
-                $dropdown[] = '<div class="dropdown-table-column dropdown-table-title">';
-                $dropdown[] = '<a title="' . htmlspecialchars($module->getDescription()) . '" href="' . htmlspecialchars($module->getLink()) . '" class="modlink">';
-                $dropdown[] = htmlspecialchars($module->getTitle());
-                $dropdown[] = '</a>';
-                $dropdown[] = '</div>';
-                $dropdown[] = '</div>';
-            }
-        }
-        $dropdown[] = '</div>';
 
-        $dropdown[] = '<hr>';
-        // Logout button
-        $buttonLabel = 'LLL:EXT:lang/locallang_core.xlf:' . ($backendUser->user['ses_backuserid'] ? 'buttons.exit' : 'buttons.logout');
-        $dropdown[] = '<a href="' . htmlspecialchars(BackendUtility::getModuleUrl('logout')) . '" class="btn btn-danger pull-right" target="_top">';
-        $dropdown[] = $this->iconFactory->getIcon('actions-logout', Icon::SIZE_SMALL)->render('inline') . ' ';
-        $dropdown[] = htmlspecialchars($languageService->sL($buttonLabel));
-        $dropdown[] = '</a>';
+        $icon = $this->iconFactory->getIcon('actions-logout', Icon::SIZE_SMALL)->render('inline');
 
-        return implode(LF, $dropdown);
+        $view = $this->getFluidTemplateObject('UserToolbarItemDropDown.html');
+        $view->assignMultiple([
+                'modules' => $userModuleMenu->getChildren(),
+                'logoutUrl' => BackendUtility::getModuleUrl('logout'),
+                'switchUserMode' => $backendUser->user['ses_backuserid'],
+                'icon' => $icon
+            ]
+        );
+
+        return $view->render();
     }
 
     /**
@@ -189,4 +161,25 @@ class UserToolbarItem implements ToolbarItemInterface
     {
         return $GLOBALS['LANG'];
     }
+
+    /**
+     * Returns a new standalone view, shorthand function
+     *
+     * @param string $filename Which templateFile should be used.
+     *
+     * @return StandaloneView
+     */
+    protected function getFluidTemplateObject(string $filename):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/ToolbarItems')]);
+        $view->setTemplateRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates/ToolbarItems')]);
+
+        $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates/ToolbarItems/' . $filename));
+
+        $view->getRequest()->setControllerExtensionName('Backend');
+        return $view;
+    }
 }
diff --git a/typo3/sysext/backend/Resources/Private/Templates/ToolbarItems/UserToolbarItem.html b/typo3/sysext/backend/Resources/Private/Templates/ToolbarItems/UserToolbarItem.html
new file mode 100644 (file)
index 0000000..b638733
--- /dev/null
@@ -0,0 +1,14 @@
+<span class="toolbar-item-avatar">{icon -> f:format.raw()}</span>
+<f:if condition="{switchUserMode}">
+    <f:then>
+        <span class="toolbar-item-name" title="{f:translate(key: 'LLL:EXT:lang/locallang_misc.xlf:switchtouser', htmlEscape: 'TRUE')} {username}">
+            {f:translate(key: 'LLL:EXT:lang/locallang_misc.xlf:switchtousershort', htmlEscape: 'FALSE')}
+            {f:if(condition: '{realName}', then: '{realname} ({username})', else: '{username}')}
+        </span>
+    </f:then>
+    <f:else>
+        <span class="toolbar-item-name" title="{username}">
+            {f:if(condition: '{realName}', then: '{realName}', else: '{username}')}
+        </span>
+    </f:else>
+</f:if>
diff --git a/typo3/sysext/backend/Resources/Private/Templates/ToolbarItems/UserToolbarItemDropDown.html b/typo3/sysext/backend/Resources/Private/Templates/ToolbarItems/UserToolbarItemDropDown.html
new file mode 100644 (file)
index 0000000..28ea6d9
--- /dev/null
@@ -0,0 +1,32 @@
+{namespace core = TYPO3\CMS\Core\ViewHelpers}
+
+<div class="dropdown-table">
+    <f:for each="{modules}" as="module">
+        <div class="dropdown-table-row"
+             id="{module.name -> f:format.htmlspecialchars()}"
+             data-modulename="{module.name -> f:format.htmlspecialchars()}"
+             data-navigationcomponentid="{module.navigationComponentId -> f:format.htmlspecialchars()}"
+             data-navigationframescript="{module.navigationFrameScript -> f:format.htmlspecialchars()}"
+             data-navigationframescriptparameters="{module.navigationFrameScriptParameters -> f:format.htmlspecialchars()}"
+        >
+            <div class="dropdown-table-column dropdown-table-icon">{module.icon -> f:format.raw()}</div>
+            <div class="dropdown-table-column dropdown-table-title">
+                <f:link.typolink parameter="{module.link}" title="{module.description}" class="modlink">
+                    {module.title}
+                </f:link.typolink>
+            </div>
+        </div>
+    </f:for>
+</div>
+<hr>
+<f:link.typolink parameter="{logoutUrl}" class="btn btn-danger pull-right" target="_top">
+    {icon -> f:format.raw()}
+    <f:if condition="{switchUserMode}">
+        <f:then>
+            {f:translate(key: 'LLL:EXT:lang/locallang_core.xlf:buttons.exit', htmlEscape: 'FALSE')}
+        </f:then>
+        <f:else>
+            {f:translate(key: 'LLL:EXT:lang/locallang_core.xlf:buttons.logout', htmlEscape: 'FALSE')}
+        </f:else>
+    </f:if>
+</f:link.typolink>
\ No newline at end of file