‪TYPO3CMS  11.5
ShortcutButton.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 
29 use TYPO3\CMS\Core\Page\PageRenderer;
31 
51 {
55  protected string ‪$routeIdentifier = '';
56 
61  protected ‪$moduleName = '';
62 
66  protected ‪$displayName = '';
67 
71  protected ‪$arguments = [];
72 
77  protected ‪$setVariables = [];
78 
83  protected ‪$getVariables = [];
84 
88  protected bool ‪$copyUrlToClipboard = true;
89 
95  public function ‪getRouteIdentifier(): string
96  {
98  }
99 
106  public function ‪setRouteIdentifier(string ‪$routeIdentifier): self
107  {
108  $this->routeIdentifier = ‪$routeIdentifier;
109  return $this;
110  }
111 
118  public function ‪getModuleName()
119  {
120  trigger_error('Method getModuleName() is deprecated and will be removed in v12. Use getRouteIdentifier() instead.', E_USER_DEPRECATED);
121  return ‪$this->moduleName;
122  }
123 
131  public function ‪setModuleName(‪$moduleName)
132  {
133  trigger_error('Method setModuleName() is deprecated and will be removed in v12. Use setRouteIdentifier() instead.', E_USER_DEPRECATED);
134  $this->moduleName = ‪$moduleName;
135  return $this;
136  }
137 
143  public function ‪getDisplayName()
144  {
145  return ‪$this->displayName;
146  }
147 
154  public function ‪setDisplayName(‪$displayName)
155  {
156  $this->displayName = ‪$displayName;
157  return $this;
158  }
159 
164  public function ‪setArguments(array ‪$arguments): self
165  {
166  $this->arguments = ‪$arguments;
167  return $this;
168  }
169 
176  public function ‪getSetVariables()
177  {
178  trigger_error('Method getSetVariables() is deprecated and will be removed in v12. Please use ShortcutButton->setArguments() instead.', E_USER_DEPRECATED);
179  return ‪$this->setVariables;
180  }
181 
189  public function ‪setSetVariables(array ‪$setVariables)
190  {
191  $this->setVariables = ‪$setVariables;
192  return $this;
193  }
194 
201  public function ‪getGetVariables()
202  {
203  trigger_error('Method getGetVariables() is deprecated and will be removed in v12. Please use ShortcutButton->setArguments() instead.', E_USER_DEPRECATED);
204  return ‪$this->getVariables;
205  }
206 
214  public function ‪setGetVariables(array ‪$getVariables)
215  {
216  $this->getVariables = ‪$getVariables;
217  return $this;
218  }
219 
227  public function ‪setCopyUrlToClipboard(bool ‪$copyUrlToClipboard): self
228  {
229  $this->copyUrlToClipboard = ‪$copyUrlToClipboard;
230  return $this;
231  }
232 
238  public function ‪getPosition()
239  {
241  }
242 
248  public function ‪getGroup()
249  {
250  return 91;
251  }
252 
258  public function ‪getType()
259  {
260  return static::class;
261  }
262 
268  public function ‪isValid()
269  {
270  return $this->moduleName !== '' || $this->routeIdentifier !== '' || (string)($this->arguments['route'] ?? '') !== '';
271  }
272 
278  public function ‪__toString()
279  {
280  return $this->‪render();
281  }
282 
288  public function ‪render()
289  {
290  if ($this->‪getBackendUser()->mayMakeShortcut()) {
291  if ($this->displayName === '') {
292  trigger_error('Creating a shortcut button without a display name is deprecated and fallbacks will be removed in v12. Please use ShortcutButton->setDisplayName() to set a display name.', E_USER_DEPRECATED);
293  }
294  if (!empty($this->routeIdentifier) || !empty($this->arguments)) {
295  $shortcutMarkup = $this->‪createShortcutMarkup();
296  } else {
297  // @deprecated since v11, the else branch will be removed in v12. Deprecation thrown by makeShortcutIcon() below
298  if (empty($this->getVariables)) {
299  $this->getVariables = ['id', 'route'];
300  }
301  $moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class);
302  $shortcutMarkup = $moduleTemplate->makeShortcutIcon(
303  implode(',', $this->getVariables),
304  implode(',', $this->setVariables),
305  $this->moduleName,
306  '',
307  $this->displayName
308  );
309  }
310  } else {
311  $shortcutMarkup = '';
312  }
313  return $shortcutMarkup;
314  }
315 
316  protected function ‪createShortcutMarkup(): string
317  {
320  $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
321 
322  if (str_contains(‪$routeIdentifier, '/')) {
323  trigger_error('Automatic fallback for the route path is deprecated and will be removed in v12.', E_USER_DEPRECATED);
324  ‪$routeIdentifier = $this->‪getRouteIdentifierByRoutePath($routeIdentifier);
325  }
326 
327  if (‪$routeIdentifier === '' && $this->moduleName !== '') {
328  trigger_error('Using ShortcutButton::$moduleNname is deprecated and will be removed in v12. Use ShortcutButton::$routeIdentifier instead.', E_USER_DEPRECATED);
329  ‪$routeIdentifier = $this->‪getRouteIdentifierByModuleName($this->moduleName);
330  }
331 
332  if (isset(‪$arguments['route'])) {
333  trigger_error('Using route as an argument is deprecated and will be removed in v12. Set the route identifier with ShortcutButton::setRouteIdentifier() instead.', E_USER_DEPRECATED);
334  if (‪$routeIdentifier === '' && is_string(‪$arguments['route'])) {
336  }
337  unset(‪$arguments['route']);
338  }
339 
340  // No route found so no shortcut button will be rendered
341  if (‪$routeIdentifier === '' || !$this->‪routeExists($routeIdentifier)) {
342  return '';
343  }
344 
345  // returnUrl will not longer be stored in the database
346  unset(‪$arguments['returnUrl']);
347 
348  // Encode arguments to be stored in the database
349  $encodedArguments = json_encode(‪$arguments) ?: '';
350 
351  $confirmationText = $this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.makeBookmark');
352  $alreadyBookmarkedText = $this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.alreadyBookmarked');
353 
354  if (!$this->copyUrlToClipboard) {
355  return GeneralUtility::makeInstance(ShortcutRepository::class)->shortcutExists(‪$routeIdentifier, $encodedArguments)
356  ? '<button type="button" class="active btn btn-default btn-sm" title="' . htmlspecialchars($alreadyBookmarkedText) . '">'
357  . $iconFactory->getIcon('actions-system-shortcut-active', ‪Icon::SIZE_SMALL)->render()
358  . '</button>'
359  : '<button type="button" class="btn btn-default btn-sm" title="' . htmlspecialchars($confirmationText) . '" ' . $this->‪getDispatchActionAttrs($routeIdentifier, $encodedArguments, $confirmationText) . '>'
360  . $iconFactory->getIcon('actions-system-shortcut-new', ‪Icon::SIZE_SMALL)->render()
361  . '</button>';
362  }
363 
364  $menuItems = [];
365 
366  if (GeneralUtility::makeInstance(ShortcutRepository::class)->shortcutExists(‪$routeIdentifier, $encodedArguments)) {
367  $menuItems[] =
368  '<li>' .
369  '<button type="button" class="dropdown-item btn btn-link disabled">' .
370  $iconFactory->getIcon('actions-system-shortcut-active', ‪Icon::SIZE_SMALL)->render() . ' ' .
371  htmlspecialchars($alreadyBookmarkedText) .
372  '</button>' .
373  '</li>';
374  } else {
375  $menuItems[] = '
376  <li>' .
377  '<button type="button" class="dropdown-item btn btn-link" ' . $this->‪getDispatchActionAttrs($routeIdentifier, $encodedArguments, $confirmationText) . '>' .
378  $iconFactory->getIcon('actions-system-shortcut-new', ‪Icon::SIZE_SMALL)->render() . ' ' .
379  htmlspecialchars($confirmationText) .
380  '</button>' .
381  '</li>';
382  }
383 
384  $pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
385  $pageRenderer->loadRequireJsModule('TYPO3/CMS/Backend/CopyToClipboard');
386  $pageRenderer->addInlineLanguageLabelFile('EXT:backend/Resources/Private/Language/locallang_copytoclipboard.xlf');
387 
388  $currentUrl = (string)GeneralUtility::makeInstance(UriBuilder::class)->buildUriFromRoute(
392  );
393 
394  $menuItems[] = '
395  <li>
396  <typo3-copy-to-clipboard text="' . htmlspecialchars($currentUrl) . '">
397  <button type="button" class="dropdown-item btn btn-link">' .
398  $iconFactory->getIcon('actions-link', ‪Icon::SIZE_SMALL)->render() . ' ' .
399  htmlspecialchars($this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.copyCurrentUrl')) .
400  '</button>' .
401  '</typo3-copy-to-clipboard>' .
402  '</li>';
403 
404  return '
405  <button type="button" class="btn btn-default btn-sm" id="dropdownShortcutMenu" data-bs-toggle="dropdown" aria-expanded="false" title="' . htmlspecialchars($this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.share')) . '">' .
406  $iconFactory->getIcon('actions-share-alt', ‪Icon::SIZE_SMALL)->render() .
407  '</button>' .
408  '<ul class="dropdown-menu" aria-labelledby="dropdownShortcutMenu">' .
409  implode(LF, $menuItems) .
410  '</ul>';
411  }
412 
420  protected function ‪getRouteIdentifierByRoutePath(string $routePath): string
421  {
422  foreach ($this->‪getRoutes() as $identifier => $route) {
423  if ($route->getPath() === $routePath
424  && (
425  $route->hasOption('moduleName')
426  || in_array($identifier, ['record_edit', 'file_edit', 'wizard_rte'], true)
427  )
428  ) {
429  return $identifier;
430  }
431  }
432 
433  return '';
434  }
435 
443  protected function ‪getRouteIdentifierByModuleName(string ‪$moduleName): string
444  {
445  $identifier = '';
446 
447  // Special case module names
448  switch (‪$moduleName) {
449  case 'xMOD_alt_doc.php':
450  $identifier = 'record_edit';
451  break;
452  case 'file_edit':
453  case 'wizard_rte':
454  $identifier = ‪$moduleName;
455  break;
456  }
457 
458  if ($identifier !== '') {
459  return $identifier;
460  }
461 
462  foreach ($this->‪getRoutes() as $identifier => $route) {
463  if ($route->hasOption('moduleName') && $route->getOption('moduleName') === ‪$moduleName) {
464  return $identifier;
465  }
466  }
467 
468  return '';
469  }
470 
479  protected function ‪getDispatchActionAttrs(string ‪$routeIdentifier, string $encodedArguments, string $confirmationText): string
480  {
481  $attrs = [
482  'data-dispatch-action' => 'TYPO3.ShortcutMenu.createShortcut',
483  'data-dispatch-args' => GeneralUtility::jsonEncodeForHtmlAttribute([
485  $encodedArguments,
486  $this->displayName,
487  $confirmationText,
488  '{$target}',
489  ], false),
490  ];
491  return GeneralUtility::implodeAttributes($attrs, true);
492  }
493 
494  protected function ‪routeExists(string ‪$routeIdentifier): bool
495  {
496  return (bool)($this->‪getRoutes()[‪$routeIdentifier] ?? false);
497  }
498 
499  protected function ‪getRoutes(): iterable
500  {
501  return GeneralUtility::makeInstance(Router::class)->getRoutes();
502  }
503 
504  protected function ‪getBackendUser(): ‪BackendUserAuthentication
505  {
506  return ‪$GLOBALS['BE_USER'];
507  }
508 
509  protected function ‪getLanguageService(): ‪LanguageService
510  {
511  return ‪$GLOBALS['LANG'];
512  }
513 }
‪TYPO3\CMS\Core\Imaging\Icon\SIZE_SMALL
‪const SIZE_SMALL
Definition: Icon.php:30
‪TYPO3\CMS\Backend\Backend\Shortcut\ShortcutRepository
Definition: ShortcutRepository.php:45
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\$copyUrlToClipboard
‪bool $copyUrlToClipboard
Definition: ShortcutButton.php:84
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\getRouteIdentifierByRoutePath
‪string getRouteIdentifierByRoutePath(string $routePath)
Definition: ShortcutButton.php:416
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action
Definition: HelpButton.php:16
‪TYPO3\CMS\Backend\Template\Components\ButtonBar
Definition: ButtonBar.php:32
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\setGetVariables
‪ShortcutButton setGetVariables(array $getVariables)
Definition: ShortcutButton.php:210
‪TYPO3\CMS\Core\Imaging\Icon
Definition: Icon.php:26
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\__toString
‪string __toString()
Definition: ShortcutButton.php:274
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\setModuleName
‪ShortcutButton setModuleName($moduleName)
Definition: ShortcutButton.php:127
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\setArguments
‪$this setArguments(array $arguments)
Definition: ShortcutButton.php:160
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\getRouteIdentifier
‪string getRouteIdentifier()
Definition: ShortcutButton.php:91
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\getModuleName
‪string getModuleName()
Definition: ShortcutButton.php:114
‪TYPO3\CMS\Core\Imaging\IconFactory
Definition: IconFactory.php:34
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\setRouteIdentifier
‪ShortcutButton setRouteIdentifier(string $routeIdentifier)
Definition: ShortcutButton.php:102
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\$routeIdentifier
‪string $routeIdentifier
Definition: ShortcutButton.php:55
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\$displayName
‪string $displayName
Definition: ShortcutButton.php:64
‪TYPO3\CMS\Backend\Template\ModuleTemplate
Definition: ModuleTemplate.php:46
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\getGetVariables
‪array getGetVariables()
Definition: ShortcutButton.php:197
‪TYPO3\CMS\Backend\Template\Components\Buttons\PositionInterface
Definition: PositionInterface.php:22
‪TYPO3\CMS\Backend\Routing\UriBuilder\SHAREABLE_URL
‪const SHAREABLE_URL
Definition: UriBuilder.php:54
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\getGroup
‪int getGroup()
Definition: ShortcutButton.php:244
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\setDisplayName
‪ShortcutButton setDisplayName($displayName)
Definition: ShortcutButton.php:150
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\getDispatchActionAttrs
‪string getDispatchActionAttrs(string $routeIdentifier, string $encodedArguments, string $confirmationText)
Definition: ShortcutButton.php:475
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton
Definition: ShortcutButton.php:51
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:40
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\getDisplayName
‪string getDisplayName()
Definition: ShortcutButton.php:139
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\isValid
‪bool isValid()
Definition: ShortcutButton.php:264
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\getRouteIdentifierByModuleName
‪string getRouteIdentifierByModuleName(string $moduleName)
Definition: ShortcutButton.php:439
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\getSetVariables
‪array getSetVariables()
Definition: ShortcutButton.php:172
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\getPosition
‪string getPosition()
Definition: ShortcutButton.php:234
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\getType
‪string getType()
Definition: ShortcutButton.php:254
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\setSetVariables
‪ShortcutButton setSetVariables(array $setVariables)
Definition: ShortcutButton.php:185
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\$moduleName
‪string $moduleName
Definition: ShortcutButton.php:60
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\$getVariables
‪array $getVariables
Definition: ShortcutButton.php:79
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\$setVariables
‪array $setVariables
Definition: ShortcutButton.php:74
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\routeExists
‪routeExists(string $routeIdentifier)
Definition: ShortcutButton.php:490
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:42
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\setCopyUrlToClipboard
‪$this setCopyUrlToClipboard(bool $copyUrlToClipboard)
Definition: ShortcutButton.php:223
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\render
‪string render()
Definition: ShortcutButton.php:284
‪TYPO3\CMS\Backend\Template\Components\ButtonBar\BUTTON_POSITION_RIGHT
‪const BUTTON_POSITION_RIGHT
Definition: ButtonBar.php:41
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\getLanguageService
‪getLanguageService()
Definition: ShortcutButton.php:505
‪TYPO3\CMS\Backend\Routing\Router
Definition: Router.php:39
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\getRoutes
‪getRoutes()
Definition: ShortcutButton.php:495
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\createShortcutMarkup
‪createShortcutMarkup()
Definition: ShortcutButton.php:312
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\$arguments
‪$arguments
Definition: ShortcutButton.php:69
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\getBackendUser
‪getBackendUser()
Definition: ShortcutButton.php:500
‪TYPO3\CMS\Backend\Template\Components\Buttons\ButtonInterface
Definition: ButtonInterface.php:22