‪TYPO3CMS  ‪main
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\Imaging\IconSize;
33 
53 {
57  protected string ‪$routeIdentifier = '';
58 
62  protected ‪$displayName = '';
63 
67  protected ‪$arguments = [];
68 
69  protected bool ‪$copyUrlToClipboard = true;
70 
74  public function ‪getRouteIdentifier(): string
75  {
77  }
78 
82  public function ‪setRouteIdentifier(string ‪$routeIdentifier): self
83  {
84  $this->routeIdentifier = ‪$routeIdentifier;
85  return $this;
86  }
87 
93  public function ‪getDisplayName()
94  {
95  return ‪$this->displayName;
96  }
97 
104  public function ‪setDisplayName(‪$displayName)
105  {
106  $this->displayName = ‪$displayName;
107  return $this;
108  }
109 
113  public function ‪setArguments(array ‪$arguments): self
114  {
115  $this->arguments = ‪$arguments;
116  return $this;
117  }
118 
125  public function ‪setCopyUrlToClipboard(bool ‪$copyUrlToClipboard): self
126  {
127  $this->copyUrlToClipboard = ‪$copyUrlToClipboard;
128  return $this;
129  }
130 
136  public function ‪getPosition()
137  {
139  }
140 
146  public function ‪getGroup()
147  {
148  return 91;
149  }
150 
156  public function ‪getType()
157  {
158  return static::class;
159  }
160 
166  public function ‪isValid()
167  {
168  return $this->displayName !== '' && $this->‪routeExists($this->routeIdentifier);
169  }
170 
174  public function ‪__toString(): string
175  {
176  return $this->‪render();
177  }
178 
184  public function ‪render()
185  {
186  if (!$this->‪getBackendUser()->mayMakeShortcut()) {
187  // Early return in case the current user is not allowed to create shortcuts.
188  // Note: This is not checked in isValid(), since it only concerns the current
189  // user and does not mean, the button is not configured properly.
190  return '';
191  }
192 
195  $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
196 
197  // The route parameter is not needed, since this is already provided with the $routeIdentifier
198  unset(‪$arguments['route']);
199 
200  // returnUrl should not be stored in the database
201  unset(‪$arguments['returnUrl']);
202 
203  // Encode arguments to be stored in the database
204  $encodedArguments = json_encode(‪$arguments) ?: '';
205 
206  $confirmationText = $this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.makeBookmark');
207  $alreadyBookmarkedText = $this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.alreadyBookmarked');
208 
209  if (!$this->copyUrlToClipboard) {
210  $shortcutButton = GeneralUtility::makeInstance(GenericButton::class);
211  if (GeneralUtility::makeInstance(ShortcutRepository::class)->shortcutExists(‪$routeIdentifier, $encodedArguments)) {
212  $shortcutButton->setLabel($alreadyBookmarkedText);
213  $shortcutButton->setIcon($iconFactory->getIcon('actions-system-shortcut-active', IconSize::SMALL));
214  } else {
215  $shortcutButton->setIcon($iconFactory->getIcon('actions-system-shortcut-new', IconSize::SMALL));
216  $shortcutButton->setLabel($confirmationText);
217  $shortcutButton->setAttributes($this->‪getDispatchActionAttrs($routeIdentifier, $encodedArguments, $confirmationText));
218  }
219  return (string)$shortcutButton;
220  }
221 
222  $dropdownItems = [];
223 
224  // Shortcut Button
225  $shortcutItem = GeneralUtility::makeInstance(DropDownItem::class);
226  $shortcutItem->setTag('button');
227  $attributes = $this->‪getDispatchActionAttrs($routeIdentifier, $encodedArguments, $confirmationText);
228  if (GeneralUtility::makeInstance(ShortcutRepository::class)->shortcutExists(‪$routeIdentifier, $encodedArguments)) {
229  $shortcutItem->setLabel($alreadyBookmarkedText);
230  $shortcutItem->setIcon($iconFactory->getIcon('actions-system-shortcut-active', IconSize::SMALL));
231  $shortcutItem->setAttributes($attributes + ['data-dispatch-disabled' => 'disabled', 'disabled' => 'disabled']);
232  } else {
233  $shortcutItem->setLabel($confirmationText);
234  $shortcutItem->setIcon($iconFactory->getIcon('actions-system-shortcut-new', IconSize::SMALL));
235  $shortcutItem->setAttributes($attributes);
236  }
237  $dropdownItems[] = $shortcutItem;
238 
239  // Clipboard Button
240  $pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
241  $pageRenderer->loadJavaScriptModule('@typo3/backend/copy-to-clipboard.js');
242  $pageRenderer->addInlineLanguageLabelFile('EXT:backend/Resources/Private/Language/locallang_copytoclipboard.xlf');
243  $clipboardItem = GeneralUtility::makeInstance(DropDownItem::class);
244  $clipboardItem->setTag('typo3-copy-to-clipboard');
245  $clipboardItem->setLabel($this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.copyCurrentUrl'));
246  $clipboardItem->setAttributes([
247  'text' => GeneralUtility::makeInstance(UriBuilder::class)->buildUriFromRoute(
251  ),
252  ]);
253  $clipboardItem->setIcon($iconFactory->getIcon('actions-link', IconSize::SMALL));
254  $dropdownItems[] = $clipboardItem;
255 
256  $dropdownButton = GeneralUtility::makeInstance(DropDownButton::class);
257  $dropdownButton->setLabel($this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.share'));
258  $dropdownButton->setIcon($iconFactory->getIcon('actions-share-alt', IconSize::SMALL));
259  foreach ($dropdownItems as $dropdownItem) {
260  $dropdownButton->addItem($dropdownItem);
261  }
262 
263  return (string)$dropdownButton;
264  }
265 
269  protected function ‪getDispatchActionAttrs(string ‪$routeIdentifier, string $encodedArguments, string $confirmationText): array
270  {
271  return [
272  'data-dispatch-action' => 'TYPO3.ShortcutMenu.createShortcut',
273  'data-dispatch-args' => GeneralUtility::jsonEncodeForHtmlAttribute([
275  $encodedArguments,
276  $this->displayName,
277  $confirmationText,
278  '{$target}',
279  ]),
280  ];
281  }
282 
283  protected function ‪routeExists(string ‪$routeIdentifier): bool
284  {
285  return GeneralUtility::makeInstance(Router::class)->hasRoute(‪$routeIdentifier);
286  }
287 
288  protected function ‪getBackendUser(): ‪BackendUserAuthentication
289  {
290  return ‪$GLOBALS['BE_USER'];
291  }
292 
293  protected function ‪getLanguageService(): ‪LanguageService
294  {
295  return ‪$GLOBALS['LANG'];
296  }
297 }
‪TYPO3\CMS\Backend\Backend\Shortcut\ShortcutRepository
Definition: ShortcutRepository.php:42
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\$copyUrlToClipboard
‪bool $copyUrlToClipboard
Definition: ShortcutButton.php:68
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action
Definition: ShortcutButton.php:16
‪TYPO3\CMS\Backend\Template\Components\ButtonBar
Definition: ButtonBar.php:33
‪TYPO3\CMS\Backend\Template\Components\Buttons\GenericButton
Definition: GenericButton.php:34
‪TYPO3\CMS\Backend\Template\Components\Buttons\DropDownButton
Definition: DropDownButton.php:48
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\setArguments
‪$this setArguments(array $arguments)
Definition: ShortcutButton.php:112
‪TYPO3\CMS\Core\Imaging\IconFactory
Definition: IconFactory.php:34
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\$routeIdentifier
‪string $routeIdentifier
Definition: ShortcutButton.php:57
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\$displayName
‪string $displayName
Definition: ShortcutButton.php:61
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\getDispatchActionAttrs
‪getDispatchActionAttrs(string $routeIdentifier, string $encodedArguments, string $confirmationText)
Definition: ShortcutButton.php:268
‪TYPO3\CMS\Backend\Template\Components\Buttons\PositionInterface
Definition: PositionInterface.php:22
‪TYPO3\CMS\Backend\Routing\UriBuilder\SHAREABLE_URL
‪const SHAREABLE_URL
Definition: UriBuilder.php:58
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\getGroup
‪int getGroup()
Definition: ShortcutButton.php:145
‪TYPO3\CMS\Core\Page\PageRenderer
Definition: PageRenderer.php:44
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\setDisplayName
‪ShortcutButton setDisplayName($displayName)
Definition: ShortcutButton.php:103
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\setRouteIdentifier
‪setRouteIdentifier(string $routeIdentifier)
Definition: ShortcutButton.php:81
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton
Definition: ShortcutButton.php:53
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:44
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\getDisplayName
‪string getDisplayName()
Definition: ShortcutButton.php:92
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\isValid
‪bool isValid()
Definition: ShortcutButton.php:165
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\getPosition
‪string getPosition()
Definition: ShortcutButton.php:135
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\getType
‪string getType()
Definition: ShortcutButton.php:155
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\__toString
‪__toString()
Definition: ShortcutButton.php:173
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\routeExists
‪routeExists(string $routeIdentifier)
Definition: ShortcutButton.php:282
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:46
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\setCopyUrlToClipboard
‪$this setCopyUrlToClipboard(bool $copyUrlToClipboard)
Definition: ShortcutButton.php:124
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\render
‪string render()
Definition: ShortcutButton.php:183
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\getRouteIdentifier
‪getRouteIdentifier()
Definition: ShortcutButton.php:73
‪TYPO3\CMS\Backend\Template\Components\ButtonBar\BUTTON_POSITION_RIGHT
‪const BUTTON_POSITION_RIGHT
Definition: ButtonBar.php:42
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\getLanguageService
‪getLanguageService()
Definition: ShortcutButton.php:292
‪TYPO3\CMS\Backend\Routing\Router
Definition: Router.php:40
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\$arguments
‪$arguments
Definition: ShortcutButton.php:66
‪TYPO3\CMS\Backend\Template\Components\Buttons\Action\ShortcutButton\getBackendUser
‪getBackendUser()
Definition: ShortcutButton.php:287
‪TYPO3\CMS\Backend\Template\Components\Buttons\DropDown\DropDownItem
Definition: DropDownItem.php:34
‪TYPO3\CMS\Backend\Template\Components\Buttons\ButtonInterface
Definition: ButtonInterface.php:22