[BUGFIX] Set anchor in all preview links of content elements
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / ContextMenu / ItemProviders / AbstractProvider.php
1 <?php
2 declare(strict_types=1);
3 namespace TYPO3\CMS\Backend\ContextMenu\ItemProviders;
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\Clipboard\Clipboard;
19 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
20 use TYPO3\CMS\Core\Imaging\Icon;
21 use TYPO3\CMS\Core\Imaging\IconFactory;
22 use TYPO3\CMS\Core\Localization\LanguageService;
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24
25 /**
26 * Abstract provider is a base class for context menu item providers
27 */
28 class AbstractProvider implements ProviderInterface
29 {
30
31 /**
32 * Language Service property. Used to access localized labels
33 *
34 * @var LanguageService
35 */
36 protected $languageService;
37
38 /**
39 * @var BackendUserAuthentication
40 */
41 protected $backendUser;
42
43 /**
44 * @var \TYPO3\CMS\Backend\Clipboard\Clipboard
45 */
46 protected $clipboard;
47
48 /**
49 * Array of items the class is providing
50 *
51 * @var array
52 */
53 protected $itemsConfiguration = [];
54
55 /**
56 * Click menu items disabled by TSConfig
57 *
58 * @var array
59 */
60 protected $disabledItems = [];
61
62 /**
63 * Current table name
64 *
65 * @var string
66 */
67 protected $table = '';
68
69 /**
70 * @var string clicked record identifier (usually uid or file combined identifier)
71 */
72 protected $identifier = '';
73
74 /**
75 * Context - from where the click menu was triggered (e.g. 'tree')
76 *
77 * @var string
78 */
79 protected $context = '';
80
81 /**
82 * Lightweight constructor, just to be able to call ->canHandle(). Rest of the initialization is done
83 * in the initialize() method
84 *
85 * @param string $table
86 * @param string $identifier
87 * @param string $context
88 */
89 public function __construct(string $table, string $identifier, string $context='')
90 {
91 $this->table = $table;
92 $this->identifier = $identifier;
93 $this->context = $context;
94 $this->languageService = $GLOBALS['LANG'];
95 $this->backendUser = $GLOBALS['BE_USER'];
96 }
97
98 /**
99 * Provider initialization, heavy stuff
100 */
101 protected function initialize()
102 {
103 $this->initClipboard();
104 $this->initDisabledItems();
105 }
106
107 /**
108 * Returns the provider priority which is used for determining the order in which providers are adding items
109 * to the result array. Highest priority means provider is evaluated first.
110 *
111 * @return int
112 */
113 public function getPriority(): int
114 {
115 return 100;
116 }
117
118 /**
119 * Whether this provider can handle given request (usually a check based on table, uid and context)
120 *
121 * @return bool
122 */
123 public function canHandle(): bool
124 {
125 return false;
126 }
127
128 /**
129 * Initialize clipboard object - necessary for all copy/cut/paste operations
130 */
131 protected function initClipboard()
132 {
133 $clipboard = GeneralUtility::makeInstance(Clipboard::class);
134 $clipboard->initializeClipboard();
135 // This locks the clipboard to the Normal for this request.
136 $clipboard->lockToNormal();
137 $this->clipboard = $clipboard;
138 }
139
140 /**
141 * Fills $this->disabledItems with the values from TSConfig.
142 * Disabled items can be set separately for each context.
143 */
144 protected function initDisabledItems()
145 {
146 $TSkey = $this->table . ($this->context ? '.' . $this->context : '');
147 $this->disabledItems = GeneralUtility::trimExplode(',', $this->backendUser->getTSConfigVal('options.contextMenu.table.' . $TSkey . '.disableItems'), true);
148 }
149
150 /**
151 * Adds new items to the given array or modifies existing items
152 *
153 * @param array $items
154 * @return array
155 */
156 public function addItems(array $items): array
157 {
158 $this->initialize();
159 $items += $this->prepareItems($this->itemsConfiguration);
160 return $items;
161 }
162
163 /**
164 * Converts item configuration (from $this->itemsConfiguration) into an array ready for returning by controller
165 *
166 * @param array $itemsConfiguration
167 * @return array
168 */
169 protected function prepareItems(array $itemsConfiguration): array
170 {
171 $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
172 $items = [];
173 foreach ($itemsConfiguration as $name => $configuration) {
174 $type = !empty($configuration['type']) ? $configuration['type'] : 'item';
175 if ($this->canRender($name, $type)) {
176 $items[$name] = [
177 'type' => $type,
178 'label' => !empty($configuration['label']) ? htmlspecialchars($this->languageService->sL($configuration['label'])) : '',
179 'icon' => !empty($configuration['iconIdentifier']) ? $iconFactory->getIcon($configuration['iconIdentifier'], Icon::SIZE_SMALL)->render() : '',
180 'additionalAttributes' => $this->getAdditionalAttributes($name),
181 'callbackAction' => !empty($configuration['callbackAction']) ? $configuration['callbackAction'] : ''
182 ];
183 if ($type === 'submenu') {
184 $items[$name]['childItems'] = $this->prepareItems($configuration['childItems']);
185 }
186 }
187 }
188 return $items;
189 }
190
191 /**
192 * Returns an array of additional attributes for given item. Additional attributes are used to pass item specific data
193 * to the JS. E.g. message for the delete confirmation dialog
194 *
195 * @param string $itemName
196 * @return array
197 */
198 protected function getAdditionalAttributes(string $itemName): array
199 {
200 return [];
201 }
202
203 /**
204 * Checks whether certain item can be rendered (e.g. check for disabled items or permissions)
205 *
206 * @param string $itemName
207 * @param string $type
208 * @return bool
209 */
210 protected function canRender(string $itemName, string $type): bool
211 {
212 return true;
213 }
214
215 /**
216 * Returns a clicked record identifier
217 *
218 * @return string
219 */
220 protected function getIdentifier(): string
221 {
222 return '';
223 }
224 }