‪TYPO3CMS  ‪main
AbstractResourceLinkHandler.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use Psr\Http\Message\ServerRequestInterface;
19 use TYPO3\CMS\Backend\Controller\AbstractLinkBrowserController;
48 
53 {
54  protected ?string ‪$expandFolder = null;
55  protected int ‪$currentPage = 1;
56  protected string ‪$moduleStorageIdentifier = 'file_list';
57 
58  protected ?‪FileList ‪$filelist = null;
59  protected ?string ‪$viewMode = null;
60  protected ?string ‪$displayThumbs = null;
61 
62  protected ?‪Folder ‪$selectedFolder = null;
65 
66  protected ‪LinkType ‪$type;
67  protected array ‪$linkParts = [];
68 
71  protected AbstractLinkBrowserController ‪$linkBrowser;
72 
73  public function ‪__construct(
74  protected readonly ‪IconFactory $iconFactory,
75  protected readonly ‪ResourceFactory $resourceFactory,
76  protected readonly ‪PageRenderer $pageRenderer,
77  protected readonly ‪UriBuilder $uriBuilder,
78  protected readonly ‪LanguageServiceFactory $languageServiceFactory
79  ) {
80  $this->languageService = $this->languageServiceFactory->createFromUserPreferences($this->‪getBackendUser());
81  }
82 
83  public function ‪canHandleLink(array ‪$linkParts): bool
84  {
85  if (!‪$linkParts['url']) {
86  return false;
87  }
88  if (isset(‪$linkParts['url'][$this->type->value]) && ‪$linkParts['url'][$this->type->value] instanceof ($this->type->getResourceType())) {
89  $this->linkParts = ‪$linkParts;
90  return true;
91  }
92  return false;
93  }
94 
95  public function ‪formatCurrentUrl(): string
96  {
97  return $this->linkParts['url'][$this->type->value]->getName();
98  }
99 
100  public function ‪createView(‪BackendViewFactory $backendViewFactory, ServerRequestInterface $request): ‪ViewInterface
101  {
102  return $backendViewFactory->‪create($request, ['typo3/cms-filelist']);
103  }
104 
105  public function ‪setView(‪ViewInterface ‪$view): self
106  {
107  $this->view = ‪$view;
108  return $this;
109  }
110 
111  public function ‪getView(): ‪ViewInterface
112  {
113  return ‪$this->view;
114  }
115 
116  public function ‪getLinkAttributes(): array
117  {
118  return ['target', 'title', 'class', 'params', 'rel'];
119  }
120 
121  public function ‪initialize(AbstractLinkBrowserController ‪$linkBrowser, ‪$identifier, array $configuration)
122  {
123  $this->linkBrowser = ‪$linkBrowser;
124  }
125 
126  public function ‪initializeVariables(ServerRequestInterface $request): void
127  {
128  $this->pageRenderer->loadJavaScriptModule('@typo3/backend/viewport/resizable-navigation.js');
129  $this->pageRenderer->loadJavaScriptModule('@typo3/backend/tree/file-storage-browser.js');
130  $this->pageRenderer->loadJavaScriptModule('@typo3/filelist/file-list-actions.js');
131 
132  $this->currentPage = (int)($request->getParsedBody()['currentPage'] ?? $request->getQueryParams()['currentPage'] ?? 1);
133 
134  $this->viewMode = $request->getParsedBody()['viewMode'] ?? $request->getQueryParams()['viewMode'] ?? null;
135  if ($this->viewMode !== null) {
136  $this->‪getBackendUser()->pushModuleData(
137  $this->moduleStorageIdentifier,
138  array_merge($this->‪getBackendUser()->getModuleData($this->moduleStorageIdentifier) ?? [], ['viewMode' => $this->viewMode])
139  );
140  } else {
141  $this->viewMode = $this->‪getBackendUser()->getModuleData($this->moduleStorageIdentifier)['viewMode'] ?? ‪ViewMode::TILES->value;
142  }
143 
144  $this->displayThumbs = $request->getParsedBody()['displayThumbs'] ?? $request->getQueryParams()['displayThumbs'] ?? null;
145  if ($this->displayThumbs !== null) {
146  $this->‪getBackendUser()->pushModuleData(
147  $this->moduleStorageIdentifier,
148  array_merge($this->‪getBackendUser()->getModuleData($this->moduleStorageIdentifier) ?? [], ['displayThumbs' => $this->displayThumbs])
149  );
150  } else {
151  $this->displayThumbs = $this->‪getBackendUser()->getModuleData($this->moduleStorageIdentifier)['displayThumbs'] ?? true;
152  }
153 
154  // Selected Folder folder
155  $this->expandFolder = $request->getParsedBody()['expandFolder'] ?? $request->getQueryParams()['expandFolder'] ?? null;
156  if ($this->expandFolder === null) {
157  if (!empty($this->linkParts)) {
158  $resource = $this->linkParts['url'][$this->type->value];
159  if ($resource instanceof ‪File) {
160  $resource = $resource->getParentFolder();
161  }
162  if ($resource instanceof ‪Folder) {
163  $this->expandFolder = $resource->getCombinedIdentifier();
164  if ($this->type === LinkType::FOLDER) {
165  // Select the parent folder of selected folder as entry point.
166  $parentFolder = $resource->getParentFolder();
167  if ($parentFolder instanceof ‪Folder) {
168  $this->expandFolder = $parentFolder->getCombinedIdentifier();
169  }
170  }
171  }
172  }
173  }
174  if ($this->expandFolder) {
175  try {
176  $this->selectedFolder = $this->resourceFactory->getFolderObjectFromCombinedIdentifier($this->expandFolder);
177  } catch (‪FolderDoesNotExistException $e) {
178  }
179  }
180  if (!$this->selectedFolder) {
181  $this->selectedFolder = $this->resourceFactory->getDefaultStorage()?->getRootLevelFolder() ?? null;
182  }
183 
184  $this->filelist = GeneralUtility::makeInstance(FileList::class, $request);
185  $this->filelist->viewMode = ViewMode::tryFrom($this->viewMode) ?? ‪ViewMode::TILES;
186  $this->filelist->thumbs = (‪$GLOBALS['TYPO3_CONF_VARS']['GFX']['thumbnails'] ?? false) && $this->displayThumbs;
187  }
188 
189  public function ‪modifyLinkAttributes(array $fieldDefinitions): array
190  {
191  return $fieldDefinitions;
192  }
193 
194  public function ‪isUpdateSupported(): bool
195  {
196  return true;
197  }
198 
202  public function ‪getBodyTagAttributes(): array
203  {
204  if (isset($this->linkParts['url'][$this->type->value]) && $this->linkParts['url'][$this->type->value] instanceof ($this->type->getResourceType())) {
205  return [
206  'data-linkbrowser-current-link' => GeneralUtility::makeInstance(LinkService::class)->asString([
207  'type' => $this->type->getLinkServiceType(),
208  $this->type->value => $this->linkParts['url'][$this->type->value],
209  ]),
210  ];
211  }
212  return [];
213  }
214 
215  protected function ‪createUri(ServerRequestInterface $request, array $parameters = []): string
216  {
217  return (string)$this->uriBuilder->buildUriFromRequest($request, $this->‪getUrlParameters($parameters));
218  }
219 
220  protected function ‪getViewModeButton(ServerRequestInterface $request): ‪ButtonInterface
221  {
222  $viewModeItems = [];
223  $viewModeItems[] = GeneralUtility::makeInstance(DropDownRadio::class)
224  ->setActive($this->viewMode === ‪ViewMode::TILES->value)
225  ->setHref($this->‪createUri($request, ['viewMode' => ‪ViewMode::TILES->value]))
226  ->setLabel($this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.view.tiles'))
227  ->setIcon($this->iconFactory->getIcon('actions-viewmode-tiles'));
228  $viewModeItems[] = GeneralUtility::makeInstance(DropDownRadio::class)
229  ->setActive($this->viewMode === ‪ViewMode::LIST->value)
230  ->setHref($this->‪createUri($request, ['viewMode' => ‪ViewMode::LIST->value]))
231  ->setLabel($this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.view.list'))
232  ->setIcon($this->iconFactory->getIcon('actions-viewmode-list'));
233  if (!($this->‪getBackendUser()->getTSConfig()['options.']['noThumbsInEB'] ?? false)) {
234  $viewModeItems[] = GeneralUtility::makeInstance(DropdownDivider::class);
235  $viewModeItems[] = GeneralUtility::makeInstance(DropDownToggle::class)
236  ->setActive((bool)$this->displayThumbs)
237  ->setHref($this->‪createUri($request, ['displayThumbs' => $this->displayThumbs ? 0 : 1]))
238  ->setLabel($this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.view.showThumbnails'))
239  ->setIcon($this->iconFactory->getIcon('actions-image'));
240  }
241 
242  $viewModeButton = GeneralUtility::makeInstance(DropDownButton::class)
243  ->setLabel($this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.view'));
244  foreach ($viewModeItems as $viewModeItem) {
246  $viewModeButton->addItem($viewModeItem);
247  }
248 
249  return $viewModeButton;
250  }
251 
252  public function ‪getUrlParameters(array $values): array
253  {
254  $values = array_replace_recursive([
255  'expandFolder' => $values['identifier'] ?? $this->expandFolder,
256  ], $values);
257 
258  return array_merge($this->linkBrowser->getUrlParameters($values), $values);
259  }
260 
262  {
264  }
265 
267  {
268  return ‪$GLOBALS['BE_USER'];
269  }
270 }
‪TYPO3\CMS\Core\Localization\LanguageServiceFactory
Definition: LanguageServiceFactory.php:25
‪TYPO3\CMS\Scheduler\LIST
‪@ LIST
Definition: SchedulerManagementAction.php:28
‪TYPO3\CMS\Core\View\ViewInterface
Definition: ViewInterface.php:24
‪TYPO3\CMS\Backend\Template\Components\Buttons\DropDownButton
Definition: DropDownButton.php:48
‪TYPO3\CMS\Backend\View\BackendViewFactory
Definition: BackendViewFactory.php:35
‪TYPO3\CMS\Filelist\Matcher\Matcher
Definition: Matcher.php:24
‪TYPO3\CMS\Core\Imaging\IconFactory
Definition: IconFactory.php:34
‪TYPO3\CMS\Backend\Template\Components\Buttons\DropDown\DropDownToggle
Definition: DropDownToggle.php:39
‪TYPO3\CMS\Filelist\Type\LinkType
‪LinkType
Definition: LinkType.php:28
‪TYPO3\CMS\Backend\Template\Components\Buttons\DropDown\DropDownDivider
Definition: DropDownDivider.php:27
‪TYPO3\CMS\Core\Page\PageRenderer
Definition: PageRenderer.php:46
‪TYPO3\CMS\Filelist\FileList
Definition: FileList.php:75
‪TYPO3\CMS\Backend\Template\Components\Buttons\DropDown\DropDownRadio
Definition: DropDownRadio.php:54
‪TYPO3\CMS\Backend\Template\Components\Buttons\DropDown\DropDownItemInterface
Definition: DropDownItemInterface.php:19
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:44
‪TYPO3\CMS\Core\Resource\Folder
Definition: Folder.php:37
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:41
‪TYPO3\CMS\Core\Resource\Exception\FolderDoesNotExistException
Definition: FolderDoesNotExistException.php:22
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:26
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:64
‪TYPO3\CMS\Backend\View\BackendViewFactory\create
‪create(ServerRequestInterface $request, array $packageNames=[])
Definition: BackendViewFactory.php:46
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Filelist\Type\TILES
‪@ TILES
Definition: ViewMode.php:26
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:51
‪TYPO3\CMS\Filelist\Type\ViewMode
‪ViewMode
Definition: ViewMode.php:24
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37
‪TYPO3\CMS\Backend\Template\Components\Buttons\ButtonInterface
Definition: ButtonInterface.php:22