[!!!][FEATURE] FormEngine element level refactoring
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / FieldControl / InsertClipboard.php
1 <?php
2 declare(strict_types=1);
3 namespace TYPO3\CMS\Backend\Form\FieldControl;
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
18 use TYPO3\CMS\Backend\Form\AbstractNode;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Lang\LanguageService;
21
22 /**
23 * Renders the icon "insert record from clipboard",
24 * typically used for type=group.
25 */
26 class InsertClipboard extends AbstractNode
27 {
28 /**
29 * Add button control
30 *
31 * @return array As defined by FieldControl class
32 */
33 public function render()
34 {
35 $languageService = $this->getLanguageService();
36
37 $parameterArray = $this->data['parameterArray'];
38 $elementName = $parameterArray['itemFormElName'];
39 $config = $parameterArray['fieldConf']['config'];
40 $internalType = (string)$config['internal_type'];
41 $clipboardElements = $config['clipboardElements'];
42
43 if ((isset($config['readOnly']) && $config['readOnly'])
44 || empty($clipboardElements)
45 ) {
46 return [];
47 }
48
49 $title = '';
50 $clipboardOnClick = [];
51 if ($internalType === 'file_reference' || $internalType === 'file') {
52 $title = sprintf($languageService->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.clipInsert_file'), count($clipboardElements));
53 foreach ($clipboardElements as $clipboardElement) {
54 $value = $clipboardElement['value'];
55 $title = 'unescape(' . GeneralUtility::quoteJSvalue(rawurlencode(basename($clipboardElement['title']))) . ')';
56 $clipboardOnClick[] = 'setFormValueFromBrowseWin('
57 . GeneralUtility::quoteJSvalue($elementName) . ','
58 . 'unescape(' . GeneralUtility::quoteJSvalue(rawurlencode(str_replace('%20', ' ', $value))) . '),'
59 . $title . ','
60 . $title
61 . ');';
62 }
63 } elseif ($internalType === 'db') {
64 $title = sprintf($languageService->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.clipInsert_db'), count($clipboardElements));
65 foreach ($clipboardElements as $clipboardElement) {
66 $value = $clipboardElement['value'];
67 $title = GeneralUtility::quoteJSvalue($clipboardElement['title']);
68 $clipboardOnClick[] = 'setFormValueFromBrowseWin('
69 . GeneralUtility::quoteJSvalue($elementName) . ','
70 . 'unescape(' . GeneralUtility::quoteJSvalue(rawurlencode(str_replace('%20', ' ', $value))) . '),'
71 . $title . ','
72 . $title
73 . ');';
74 }
75 }
76 $clipboardOnClick[] = 'return false;';
77
78 return [
79 'iconIdentifier' => 'actions-document-paste-into',
80 'title' => $title,
81 'linkAttributes' => [
82 'onClick' => implode('', $clipboardOnClick),
83 ],
84 ];
85 }
86
87 /**
88 * @return LanguageService
89 */
90 protected function getLanguageService()
91 {
92 return $GLOBALS['LANG'];
93 }
94 }