‪TYPO3CMS  ‪main
BrowseLinksController.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 
20 use Psr\Http\Message\ServerRequestInterface;
21 use TYPO3\CMS\Backend\Controller\AbstractLinkBrowserController;
33 
38 class BrowseLinksController extends AbstractLinkBrowserController
39 {
40  protected string $editorId;
41 
45  protected string $contentsLanguage;
46  protected ?‪LanguageService $contentLanguageService;
47  protected array $buttonConfig = [];
48  protected array $thisConfig = [];
49  protected array $classesAnchorDefault = [];
50  protected array $classesAnchorDefaultTarget = [];
51  protected array $classesAnchorJSOptions = [];
52  protected string $defaultLinkTarget = '';
53  protected string $siteUrl = '';
54 
55  public function __construct(
56  protected readonly ‪LinkService $linkService,
57  protected readonly ‪Richtext $richtext,
58  protected readonly ‪LanguageServiceFactory $languageServiceFactory,
59  protected readonly ‪FlashMessageService $flashMessageService,
60  ) {}
61 
65  public function getConfiguration(): array
66  {
67  return $this->buttonConfig;
68  }
69 
73  public function getUrlParameters(array $overrides = null): array
74  {
75  return [
76  'act' => $overrides['act'] ?? $this->displayedLinkHandlerId,
77  'P' => $overrides['P'] ?? $this->parameters,
78  'editorId' => $this->editorId,
79  'contentsLanguage' => $this->contentsLanguage,
80  ];
81  }
82 
83  protected function initDocumentTemplate(): void
84  {
85  $this->pageRenderer->getJavaScriptRenderer()->addJavaScriptModuleInstruction(
86  ‪JavaScriptModuleInstruction::create('@typo3/rte-ckeditor/rte-link-browser.js')
87  ->invoke('initialize', $this->editorId)
88  );
89  }
90 
91  protected function getCurrentPageId(): int
92  {
93  return (int)$this->parameters['pid'];
94  }
95 
96  protected function initVariables(ServerRequestInterface $request): void
97  {
98  parent::initVariables($request);
99  $queryParameters = $request->getQueryParams();
100  $this->siteUrl = $request->getAttribute('normalizedParams')->getSiteUrl();
101  $this->currentLinkParts = $queryParameters['P']['curUrl'] ?? [];
102  $this->editorId = $queryParameters['editorId'] ?? '';
103  $this->contentsLanguage = $queryParameters['contentsLanguage'] ?? '';
104  $this->contentLanguageService = $this->languageServiceFactory->create($this->contentsLanguage);
105  $tcaFieldConf = [
106  'enableRichtext' => true,
107  'richtextConfiguration' => $this->parameters['richtextConfigurationName'] ?: null,
108  ];
109  $this->thisConfig = $this->richtext->getConfiguration(
110  $this->parameters['table'],
111  $this->parameters['fieldName'],
112  (int)$this->parameters['pid'],
113  $this->parameters['recordType'],
114  $tcaFieldConf
115  );
116  $this->buttonConfig = $this->thisConfig['buttons']['link'] ?? [];
117  }
118 
119  protected function initCurrentUrl(): void
120  {
121  if (empty($this->currentLinkParts)) {
122  return;
123  }
124  if (!empty($this->currentLinkParts['url'])) {
125  try {
126  $data = $this->linkService->resolve($this->currentLinkParts['url']);
127  $this->currentLinkParts['type'] = $data['type'];
128  unset($data['type']);
129  $this->currentLinkParts['url'] = $data;
130  if (!empty($this->currentLinkParts['url']['parameters'])) {
131  $this->currentLinkParts['params'] = '&' . $this->currentLinkParts['url']['parameters'];
132  }
133  } catch (UnknownLinkHandlerException $e) {
134  $this->flashMessageService->getMessageQueueByIdentifier()->enqueue(
135  new FlashMessage(message: $e->getMessage(), severity: ContextualFeedbackSeverity::ERROR)
136  );
137  }
138  }
139  parent::initCurrentUrl();
140  }
141 
142  protected function renderLinkAttributeFields(ViewInterface $view): string
143  {
144  // Processing the classes configuration
145  if (!empty($this->buttonConfig['properties']['class']['allowedClasses'])) {
146  $classesAnchorArray = is_array($this->buttonConfig['properties']['class']['allowedClasses'])
147  ? $this->buttonConfig['properties']['class']['allowedClasses']
148  : ‪GeneralUtility::trimExplode(',', $this->buttonConfig['properties']['class']['allowedClasses'], true);
149  // Collecting allowed classes and configured default values
150  $classesAnchor = [
151  'all' => [],
152  ];
153 
154  if (is_array($this->thisConfig['classesAnchor'] ?? null)) {
155  foreach ($this->thisConfig['classesAnchor'] as $label => $conf) {
156  if (in_array($conf['class'] ?? null, $classesAnchorArray, true)) {
157  $classesAnchor['all'][] = $conf['class'];
158  if ($conf['type'] === $this->displayedLinkHandlerId) {
159  $classesAnchor[$conf['type']][] = $conf['class'];
160  if (($this->buttonConfig[$conf['type']]['properties']['class']['default'] ?? null) === $conf['class']) {
161  $this->classesAnchorDefault[$conf['type']] = $conf['class'];
162  if (isset($conf['target'])) {
163  $this->classesAnchorDefaultTarget[$conf['type']] = trim((string)$conf['target']);
164  }
165  }
166  }
167  }
168  }
169  }
170 
171  $linkClass = $this->linkAttributeValues['class'] ?? '';
172  if ($linkClass !== '') {
173  $currentLinkClassIsAllowed = true;
174  if (!in_array($linkClass, $classesAnchorArray, true)) {
175  // Current class is not a globally allowed class
176  $currentLinkClassIsAllowed = false;
177  }
178  if (
179  isset($classesAnchor[$this->displayedLinkHandlerId]) &&
180  in_array($linkClass, $classesAnchor['all'], true) &&
181  !in_array($linkClass, $classesAnchor[$this->displayedLinkHandlerId], true)
182  ) {
183  // Current class is limited to specific link types but not available in current link type
184  $currentLinkClassIsAllowed = false;
185  }
186 
187  if (!$currentLinkClassIsAllowed) {
188  $this->classesAnchorJSOptions[$this->displayedLinkHandlerId] ??= '';
189  // Add a dummy option that preserved the current class value (despite being invalid)
190  // in order to prevent unintentional modification of assigned classes.
191  $this->classesAnchorJSOptions[$this->displayedLinkHandlerId] .= sprintf(
192  '<option selected="selected" value="%s">%s</option>',
193  htmlspecialchars($linkClass),
194  htmlspecialchars(
195  @sprintf(
196  '[ ' . $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.noMatchingValue') . ' ]',
197  $linkClass
198  )
199  )
200  );
201  }
202  }
203 
204  // Constructing the class selector options
205  foreach ($classesAnchorArray as $class) {
206  if (
207  !in_array($class, $classesAnchor['all'], true)
208  || (
209  in_array($class, $classesAnchor['all'], true)
210  && is_array($classesAnchor[$this->displayedLinkHandlerId] ?? null)
211  && in_array($class, $classesAnchor[$this->displayedLinkHandlerId])
212  )
213  ) {
214  $selected = '';
215  if (
216  (($this->linkAttributeValues['class'] ?? false) === $class)
217  || ($this->classesAnchorDefault[$this->displayedLinkHandlerId] ?? false) === $class
218  ) {
219  $selected = 'selected="selected"';
220  }
221  $classLabel = !empty($this->thisConfig['classes'][$class]['name'])
222  ? $this->getPageConfigLabel($this->thisConfig['classes'][$class]['name'], false)
223  : $class;
224  $classStyle = !empty($this->thisConfig['classes'][$class]['value'])
225  ? $this->thisConfig['classes'][$class]['value']
226  : '';
227 
228  $this->classesAnchorJSOptions[$this->displayedLinkHandlerId] ??= '';
229  $this->classesAnchorJSOptions[$this->displayedLinkHandlerId] .= '<option ' . $selected . ' value="' . htmlspecialchars($class) . '"'
230  . ($classStyle ? ' style="' . htmlspecialchars($classStyle) . '"' : '')
231  . '>' . htmlspecialchars($classLabel)
232  . '</option>';
233  }
234  }
235  if (
236  ($this->classesAnchorJSOptions[$this->displayedLinkHandlerId] ?? false)
237  && !(
238  ($this->buttonConfig['properties']['class']['required'] ?? false)
239  || ($this->buttonConfig[$this->displayedLinkHandlerId]['properties']['class']['required'] ?? false)
240  )
241  ) {
242  $selected = '';
243  if (!($this->linkAttributeValues['class'] ?? false) && !($this->classesAnchorDefault[$this->displayedLinkHandlerId] ?? false)) {
244  $selected = 'selected="selected"';
245  }
246  $this->classesAnchorJSOptions[$this->displayedLinkHandlerId] = '<option ' . $selected . ' value=""></option>' . $this->classesAnchorJSOptions[$this->displayedLinkHandlerId];
247  }
248  }
249  // Default target
250  $this->defaultLinkTarget = ($this->classesAnchorDefault[$this->displayedLinkHandlerId] ?? false) && ($this->classesAnchorDefaultTarget[$this->displayedLinkHandlerId] ?? false)
251  ? $this->classesAnchorDefaultTarget[$this->displayedLinkHandlerId]
252  : ($this->buttonConfig[$this->displayedLinkHandlerId]['properties']['target']['default'] ?? $this->buttonConfig['properties']['target']['default'] ?? '');
253 
254  return parent::renderLinkAttributeFields($view);
255  }
256 
264  protected function getPageConfigLabel(string $string, bool $JScharCode = true): string
265  {
266  $label = $this->getLanguageService()->sL(trim($string));
267  $label = str_replace(['\\\'', '"'], ['\'', '\\"'], $label);
268  return $JScharCode ? GeneralUtility::quoteJSvalue($label) : $label;
269  }
270 
271  protected function renderCurrentUrl(ViewInterface $view): void
272  {
273  $view->assign('removeCurrentLink', true);
274  parent::renderCurrentUrl($view);
275  }
276 
280  protected function getAllowedItems(): array
281  {
282  $allowedItems = parent::getAllowedItems();
283 
284  if (isset($this->thisConfig['allowedTypes'])) {
285  $allowedItems = array_intersect($allowedItems, ‪GeneralUtility::trimExplode(',', $this->thisConfig['allowedTypes'], true));
286  } elseif (isset($this->thisConfig['blindLinkOptions'])) {
287  // @todo Deprecate this option
288  $allowedItems = array_diff($allowedItems, ‪GeneralUtility::trimExplode(',', $this->thisConfig['blindLinkOptions'], true));
289  }
290 
291  if (is_array($this->buttonConfig['options'] ?? null) && !empty($this->buttonConfig['options']['removeItems'])) {
292  $allowedItems = array_diff($allowedItems, ‪GeneralUtility::trimExplode(',', $this->buttonConfig['options']['removeItems'], true));
293  }
294 
295  return $allowedItems;
296  }
297 
301  protected function getAllowedLinkAttributes(): array
302  {
303  $allowedLinkAttributes = parent::getAllowedLinkAttributes();
304 
305  if (isset($this->thisConfig['allowedOptions'])) {
306  $allowedLinkAttributes = array_intersect($allowedLinkAttributes, ‪GeneralUtility::trimExplode(',', $this->thisConfig['allowedOptions'], true));
307  } elseif (isset($this->thisConfig['blindLinkFields'])) {
308  // @todo Deprecate this option
309  $allowedLinkAttributes = array_diff($allowedLinkAttributes, ‪GeneralUtility::trimExplode(',', $this->thisConfig['blindLinkFields'], true));
310  }
311 
312  return $allowedLinkAttributes;
313  }
314 
320  protected function getLinkAttributeFieldDefinitions(): array
321  {
322  $fieldRenderingDefinitions = parent::getLinkAttributeFieldDefinitions();
323  $fieldRenderingDefinitions['class'] = $this->getClassField();
324  $fieldRenderingDefinitions['target'] = $this->getTargetField();
325  $fieldRenderingDefinitions['rel'] = $this->getRelField();
326  if (empty($this->buttonConfig['queryParametersSelector']['enabled'])) {
327  unset($fieldRenderingDefinitions['params']);
328  }
329  return $fieldRenderingDefinitions;
330  }
331 
332  protected function getRelField(): string
333  {
334  if (empty($this->buttonConfig['relAttribute']['enabled'])) {
335  return '';
336  }
337 
338  $currentRel = '';
339  if ($this->displayedLinkHandler === $this->currentLinkHandler
340  && !empty($this->currentLinkParts)
341  && is_string($this->linkAttributeValues['rel'] ?? null)
342  ) {
343  $currentRel = $this->linkAttributeValues['rel'];
344  }
345 
346  return '
347  <div class="element-browser-form-group">
348  <label for="lrel" class="form-label">' .
349  htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:backend/Resources/Private/Language/locallang_browse_links.xlf:linkRelationship')) .
350  '</label>
351  <input type="text" name="lrel" class="form-control" value="' . htmlspecialchars($currentRel) . '" />
352  </div>
353  ';
354  }
355 
356  protected function getTargetField(): string
357  {
358  $targetSelectorConfig = [];
359  if (is_array($this->buttonConfig['targetSelector'] ?? null)) {
360  $targetSelectorConfig = $this->buttonConfig['targetSelector'];
361  }
362  $target = !empty($this->linkAttributeValues['target']) ? $this->linkAttributeValues['target'] : $this->defaultLinkTarget;
363  $lang = $this->getLanguageService();
364 
365  $disabled = $targetSelectorConfig['disabled'] ?? false;
366  if ($disabled) {
367  return '';
368  }
369 
370  return '
371  <div class="element-browser-form-group">
372  <label for="ltarget" class="form-label">
373  ' . htmlspecialchars($lang->sL('LLL:EXT:backend/Resources/Private/Language/locallang_browse_links.xlf:target')) . '
374  </label>
375  <span class="input-group">
376  <input id="ltarget" type="text" name="ltarget" class="t3js-linkTarget form-control"
377  value="' . htmlspecialchars($target) . '" />
378  <select name="ltarget_type" class="t3js-targetPreselect form-select">
379  <option value=""></option>
380  <option value="_top">' . htmlspecialchars($lang->sL('LLL:EXT:backend/Resources/Private/Language/locallang_browse_links.xlf:top')) . '</option>
381  <option value="_blank">' . htmlspecialchars($lang->sL('LLL:EXT:backend/Resources/Private/Language/locallang_browse_links.xlf:newWindow')) . '</option>
382  </select>
383  </span>
384  </div>';
385  }
386 
392  protected function getClassField(): string
393  {
394  if (!isset($this->classesAnchorJSOptions[$this->displayedLinkHandlerId])) {
395  return '';
396  }
397 
398  return '
399  <div class="element-browser-form-group">
400  <label for="lclass" class="form-label">
401  ' . htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:backend/Resources/Private/Language/locallang_browse_links.xlf:class')) . '
402  </label>
403  <select id="lclass" name="lclass" class="t3js-class-selector form-select">
404  ' . $this->classesAnchorJSOptions[$this->displayedLinkHandlerId] . '
405  </select>
406  </div>
407  ';
408  }
409 
413  protected function getBodyTagAttributes(): array
414  {
415  $parameters = parent::getBodyTagAttributes();
416  $parameters['data-site-url'] = $this->siteUrl;
417  $parameters['data-default-link-target'] = $this->defaultLinkTarget;
418  return $parameters;
419  }
420 }
‪TYPO3\CMS\Core\Localization\LanguageServiceFactory
Definition: LanguageServiceFactory.php:25
‪TYPO3\CMS\Core\View\ViewInterface
Definition: ViewInterface.php:24
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\create
‪static create(string $name, string $exportName=null)
Definition: JavaScriptModuleInstruction.php:47
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction
Definition: JavaScriptModuleInstruction.php:23
‪TYPO3\CMS\Core\Type\ContextualFeedbackSeverity
‪ContextualFeedbackSeverity
Definition: ContextualFeedbackSeverity.php:25
‪TYPO3\CMS\RteCKEditor\Controller
Definition: BrowseLinksController.php:18
‪TYPO3\CMS\Core\Messaging\FlashMessage
Definition: FlashMessage.php:27
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Configuration\Richtext
Definition: Richtext.php:34
‪TYPO3\CMS\Core\Messaging\FlashMessageService
Definition: FlashMessageService.php:27
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static list< string > trimExplode(string $delim, string $string, bool $removeEmptyValues=false, int $limit=0)
Definition: GeneralUtility.php:822