2 declare(strict_types
=1);
3 namespace TYPO3\CMS\Recordlist\LinkHandler
;
6 * This file is part of the TYPO3 CMS project.
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.
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
15 * The TYPO3 project - inspiring people to share!
18 use Psr\Http\Message\ServerRequestInterface
;
19 use TYPO3\CMS\Backend\Utility\BackendUtility
;
20 use TYPO3\CMS\Core\LinkHandling\LinkService
;
21 use TYPO3\CMS\Core\Page\PageRenderer
;
22 use TYPO3\CMS\Core\Utility\GeneralUtility
;
23 use TYPO3\CMS\Fluid\View\StandaloneView
;
24 use TYPO3\CMS\Recordlist\Browser\RecordBrowser
;
25 use TYPO3\CMS\Recordlist\Controller\AbstractLinkBrowserController
;
26 use TYPO3\CMS\Recordlist\Tree\View\LinkParameterProviderInterface
;
27 use TYPO3\CMS\Recordlist\Tree\View\RecordBrowserPageTreeView
;
30 * Link handler for arbitrary database records
32 class RecordLinkHandler
extends AbstractLinkHandler
implements LinkHandlerInterface
, LinkParameterProviderInterface
35 * Configuration key in TSconfig TCEMAIN.linkHandler.record
39 protected $identifier;
42 * Specific TSconfig for the current instance (corresponds to TCEMAIN.linkHandler.record.identifier.configuration)
46 protected $configuration = [];
49 * Parts of the current link
53 protected $linkParts = [];
58 protected $expandPage = 0;
61 * Initializes the handler.
63 * @param AbstractLinkBrowserController $linkBrowser
64 * @param string $identifier
65 * @param array $configuration Page TSconfig
67 public function initialize(AbstractLinkBrowserController
$linkBrowser, $identifier, array $configuration)
69 parent
::initialize($linkBrowser, $identifier, $configuration);
70 $this->identifier
= $identifier;
71 $this->configuration
= $configuration;
75 * Checks if this is the right handler for the given link.
77 * Also stores information locally about currently linked record.
79 * @param array $linkParts Link parts as returned from TypoLinkCodecService
82 public function canHandleLink(array $linkParts): bool
84 if (!$linkParts['url'] ||
!isset($linkParts['url']['identifier'])) {
88 $data = $linkParts['url'];
90 // Get the related record
91 $table = $this->configuration
['table'];
92 $record = BackendUtility
::getRecord($table, $data['uid']);
93 if ($record === null) {
94 $linkParts['title'] = $this->getLanguageService()->getLL('recordNotFound');
96 $linkParts['tableName'] = $this->getLanguageService()->sL($GLOBALS['TCA'][$table]['ctrl']['title']);
97 $linkParts['pid'] = (int)$record['pid'];
98 $linkParts['title'] = $linkParts['title'] ?
: BackendUtility
::getRecordTitle($table, $record);
100 $linkParts['url']['type'] = $linkParts['type'];
101 $this->linkParts
= $linkParts;
107 * Formats information for the current record for HTML output.
111 public function formatCurrentUrl(): string
115 $this->linkParts
['tableName'],
116 $this->linkParts
['title'],
117 $this->linkParts
['url']['uid']
122 * Renders the link handler.
124 * @param ServerRequestInterface $request
127 public function render(ServerRequestInterface
$request): string
130 GeneralUtility
::makeInstance(PageRenderer
::class)->loadRequireJsModule('TYPO3/CMS/Recordlist/RecordLinkHandler');
132 // Define the current page
133 if (isset($request->getQueryParams()['expandPage'])) {
134 $this->expandPage
= (int)$request->getQueryParams()['expandPage'];
135 } elseif (isset($this->configuration
['storagePid'])) {
136 $this->expandPage
= (int)$this->configuration
['storagePid'];
137 } elseif (isset($this->linkParts
['pid'])) {
138 $this->expandPage
= (int)$this->linkParts
['pid'];
141 $databaseBrowser = GeneralUtility
::makeInstance(RecordBrowser
::class);
143 $recordList = $databaseBrowser->displayRecordsForPage(
145 $this->configuration
['table'],
146 $this->getUrlParameters([])
149 $path = GeneralUtility
::getFileAbsFileName('EXT:recordlist/Resources/Private/Templates/LinkBrowser/Record.html');
150 $view = GeneralUtility
::makeInstance(StandaloneView
::class);
151 $view->setTemplatePathAndFilename($path);
152 $view->assignMultiple([
153 'tree' => $this->configuration
['hidePageTree'] ?
'' : $this->renderPageTree(),
154 'recordList' => $recordList,
157 return $view->render();
161 * Renders the page tree.
165 protected function renderPageTree(): string
167 $backendUser = $this->getBackendUser();
169 /** @var RecordBrowserPageTreeView $pageTree */
170 $pageTree = GeneralUtility
::makeInstance(RecordBrowserPageTreeView
::class);
171 $pageTree->setLinkParameterProvider($this);
172 $pageTree->ext_showPageId
= (bool)$backendUser->getTSConfigVal('options.pageTree.showPageIdWithTitle');
173 $pageTree->ext_showNavTitle
= (bool)$backendUser->getTSConfigVal('options.pageTree.showNavTitle');
174 $pageTree->ext_showPathAboveMounts
= (bool)$backendUser->getTSConfigVal('options.pageTree.showPathAboveMounts');
175 $pageTree->addField('nav_title');
177 // Load the mount points, if any
178 // NOTE: mount points actually override the page tree
179 if (!empty($this->configuration
['pageTreeMountPoints'])) {
180 $pageTree->MOUNTS
= GeneralUtility
::intExplode(',', $this->configuration
['pageTreeMountPoints'], true);
183 return $pageTree->getBrowsableTree();
187 * Returns attributes for the body tag.
189 * @return string[] Array of body-tag attributes
191 public function getBodyTagAttributes(): array
194 'data-identifier' => 't3://record?identifier=' . $this->identifier
. '&uid=',
196 if (!empty($this->linkParts
)) {
197 $attributes['data-current-link'] = GeneralUtility
::makeInstance(LinkService
::class)->asString($this->linkParts
['url']);
204 * Returns all parameters needed to build a URL with all the necessary information.
206 * @param array $values Array of values to include into the parameters or which might influence the parameters
207 * @return string[] Array of parameters which have to be added to URLs
209 public function getUrlParameters(array $values): array
211 $pid = isset($values['pid']) ?
(int)$values['pid'] : $this->expandPage
;
213 'expandPage' => $pid,
217 $this->linkBrowser
->getUrlParameters($values),
218 ['P' => $this->linkBrowser
->getParameters()],
224 * Checks if the submitted page matches the current page.
226 * @param array $values Values to be checked
227 * @return bool Returns TRUE if the given values match the currently selected item
229 public function isCurrentlySelectedItem(array $values): bool
231 return !empty($this->linkParts
) && (int)$this->linkParts
['pid'] === (int)$values['pid'];
235 * Returns the URL of the current script
239 public function getScriptUrl(): string
241 return $this->linkBrowser
->getScriptUrl();