‪TYPO3CMS  9.5
ExtensionUtility.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
22 {
23  const ‪PLUGIN_TYPE_PLUGIN = 'list_type';
25 
43  public static function ‪configurePlugin($extensionName, $pluginName, array $controllerActions, array $nonCacheableControllerActions = [], $pluginType = self::PLUGIN_TYPE_PLUGIN)
44  {
45  ‪self::checkPluginNameFormat($pluginName);
46  ‪self::checkExtensionNameFormat($extensionName);
47 
48  // Check if vendor name is prepended to extensionName in the format {vendorName}.{extensionName}
49  $vendorName = null;
50  $delimiterPosition = strrpos($extensionName, '.');
51  if ($delimiterPosition !== false) {
52  $vendorName = str_replace('.', '\\', substr($extensionName, 0, $delimiterPosition));
53  $extensionName = substr($extensionName, $delimiterPosition + 1);
54 
55  if (!empty($vendorName)) {
56  ‪self::checkVendorNameFormat($vendorName, $extensionName);
57  }
58  }
59  $extensionName = str_replace(' ', '', ucwords(str_replace('_', ' ', $extensionName)));
60 
61  $pluginSignature = strtolower($extensionName . '_' . $pluginName);
62  if (!is_array(‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName] ?? false)) {
63  ‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName] = [];
64  }
65  foreach ($controllerActions as $controllerName => $actionsList) {
66  ‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName]['controllers'][$controllerName] = ['actions' => \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $actionsList)];
67  if (!empty($nonCacheableControllerActions[$controllerName])) {
68  ‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName]['controllers'][$controllerName]['nonCacheableActions'] = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $nonCacheableControllerActions[$controllerName]);
69  }
70  }
71 
72  switch ($pluginType) {
74  $pluginContent = trim('
75 tt_content.list.20.' . $pluginSignature . ' = USER
76 tt_content.list.20.' . $pluginSignature . ' {
77  userFunc = TYPO3\\CMS\\Extbase\\Core\\Bootstrap->run
78  extensionName = ' . $extensionName . '
79  pluginName = ' . $pluginName . (null !== $vendorName ? ("\n\t" . 'vendorName = ' . $vendorName) : '') . '
80 }');
81  break;
83  $pluginContent = trim('
84 tt_content.' . $pluginSignature . ' =< lib.contentElement
85 tt_content.' . $pluginSignature . ' {
86  templateName = Generic
87  20 = USER
88  20 {
89  userFunc = TYPO3\\CMS\\Extbase\\Core\\Bootstrap->run
90  extensionName = ' . $extensionName . '
91  pluginName = ' . $pluginName . (null !== $vendorName ? ("\n\t\t" . 'vendorName = ' . $vendorName) : '') . '
92  }
93 }');
94  break;
95  default:
96  throw new \InvalidArgumentException('The pluginType "' . $pluginType . '" is not suported', 1289858856);
97  }
98  ‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName]['pluginType'] = $pluginType;
100 # Setting ' . $extensionName . ' plugin TypoScript
101 ' . $pluginContent, 'defaultContentRendering');
102  }
103 
114  public static function ‪registerPlugin($extensionName, $pluginName, $pluginTitle, $pluginIcon = null)
115  {
116  ‪self::checkPluginNameFormat($pluginName);
117  ‪self::checkExtensionNameFormat($extensionName);
118 
119  $delimiterPosition = strrpos($extensionName, '.');
120  if ($delimiterPosition !== false) {
121  $extensionName = substr($extensionName, $delimiterPosition + 1);
122  }
123  $extensionName = str_replace(' ', '', ucwords(str_replace('_', ' ', $extensionName)));
124  $pluginSignature = strtolower($extensionName) . '_' . strtolower($pluginName);
125 
126  // At this point $extensionName is normalized, no matter which format the method was feeded with.
127  // Calculate the original extensionKey from this again.
128  $extensionKey = \TYPO3\CMS\Core\Utility\GeneralUtility::camelCaseToLowerCaseUnderscored($extensionName);
129 
130  // pluginType is usually defined by configurePlugin() in the global array. Use this or fall back to default "list_type".
131  $pluginType = ‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName]['pluginType'] ?? 'list_type';
132 
134  [$pluginTitle, $pluginSignature, $pluginIcon],
135  $pluginType,
136  $extensionKey
137  );
138  }
139 
152  public static function ‪registerModule($extensionName, $mainModuleName = '', $subModuleName = '', $position = '', array $controllerActions = [], array $moduleConfiguration = [])
153  {
154  ‪self::checkExtensionNameFormat($extensionName);
155 
156  // Check if vendor name is prepended to extensionName in the format {vendorName}.{extensionName}
157  $vendorName = null;
158  if (false !== $delimiterPosition = strrpos($extensionName, '.')) {
159  $vendorName = str_replace('.', '\\', substr($extensionName, 0, $delimiterPosition));
160  $extensionName = substr($extensionName, $delimiterPosition + 1);
161 
162  if (!empty($vendorName)) {
163  ‪self::checkVendorNameFormat($vendorName, $extensionName);
164  }
165  }
166 
167  $extensionName = str_replace(' ', '', ucwords(str_replace('_', ' ', $extensionName)));
168  $defaultModuleConfiguration = [
169  'access' => 'admin',
170  'icon' => 'EXT:extbase/Resources/Public/Icons/Extension.png',
171  'labels' => ''
172  ];
173  if ($mainModuleName !== '' && !array_key_exists($mainModuleName, ‪$GLOBALS['TBE_MODULES'])) {
174  $mainModuleName = $extensionName . \TYPO3\CMS\Core\Utility\GeneralUtility::underscoredToUpperCamelCase($mainModuleName);
175  } else {
176  $mainModuleName = $mainModuleName !== '' ? $mainModuleName : 'web';
177  }
178  // add mandatory parameter to use new pagetree
179  if ($mainModuleName === 'web') {
180  $defaultModuleConfiguration['navigationComponentId'] = 'TYPO3/CMS/Backend/PageTree/PageTreeElement';
181  }
182  ‪\TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($defaultModuleConfiguration, $moduleConfiguration);
183  $moduleConfiguration = $defaultModuleConfiguration;
184  $moduleSignature = $mainModuleName;
185  if ($subModuleName !== '') {
186  $subModuleName = $extensionName . \TYPO3\CMS\Core\Utility\GeneralUtility::underscoredToUpperCamelCase($subModuleName);
187  $moduleSignature .= '_' . $subModuleName;
188  }
189  $moduleConfiguration['name'] = $moduleSignature;
190  if (null !== $vendorName) {
191  $moduleConfiguration['vendorName'] = $vendorName;
192  }
193  $moduleConfiguration['extensionName'] = $extensionName;
194  $moduleConfiguration['routeTarget'] = \TYPO3\CMS\Extbase\Core\Bootstrap::class . '::handleBackendRequest';
195  if (!is_array(‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['modules'][$moduleSignature] ?? false)) {
196  ‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['modules'][$moduleSignature] = [];
197  }
198  foreach ($controllerActions as $controllerName => $actions) {
199  ‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['modules'][$moduleSignature]['controllers'][$controllerName] = [
200  'actions' => \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $actions)
201  ];
202  }
203  ‪\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addModule($mainModuleName, $subModuleName, $position, null, $moduleConfiguration);
204  }
205 
211  public static function ‪registerTypeConverter($typeConverterClassName)
212  {
213  if (!isset(‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['typeConverters']) ||
214  !is_array(‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['typeConverters'])
215  ) {
216  ‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['typeConverters'] = [];
217  }
218  if (!in_array($typeConverterClassName, ‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['typeConverters'])) {
219  ‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['typeConverters'][] = $typeConverterClassName;
220  }
221  }
222 
230  protected static function ‪checkVendorNameFormat($vendorName, $extensionName)
231  {
232  if (preg_match('/^[A-Z]/', $vendorName) !== 1) {
233  trigger_error('The vendor name from tx_' . $extensionName . ' must begin with a capital letter.', E_USER_DEPRECATED);
234  }
235  }
236 
243  protected static function ‪checkExtensionNameFormat($extensionName)
244  {
245  if (empty($extensionName)) {
246  throw new \InvalidArgumentException('The extension name must not be empty', 1239891990);
247  }
248  }
249 
256  protected static function ‪checkPluginNameFormat($pluginName)
257  {
258  if (empty($pluginName)) {
259  throw new \InvalidArgumentException('The plugin name must not be empty', 1239891988);
260  }
261  }
262 }
‪TYPO3\CMS\Core\Utility\ExtensionManagementUtility\addModule
‪static addModule($main, $sub='', $position='', $path=null, $moduleConfiguration=[])
Definition: ExtensionManagementUtility.php:863
‪TYPO3\CMS\Extbase\Utility\ExtensionUtility
Definition: ExtensionUtility.php:22
‪TYPO3\CMS\Extbase\Utility\ExtensionUtility\checkVendorNameFormat
‪static checkVendorNameFormat($vendorName, $extensionName)
Definition: ExtensionUtility.php:230
‪TYPO3\CMS\Extbase\Utility\ExtensionUtility\PLUGIN_TYPE_CONTENT_ELEMENT
‪const PLUGIN_TYPE_CONTENT_ELEMENT
Definition: ExtensionUtility.php:24
‪TYPO3\CMS\Extbase\Utility\ExtensionUtility\registerModule
‪static registerModule($extensionName, $mainModuleName='', $subModuleName='', $position='', array $controllerActions=[], array $moduleConfiguration=[])
Definition: ExtensionUtility.php:152
‪TYPO3\CMS\Core\Utility\ExtensionManagementUtility\addPlugin
‪static addPlugin($itemArray, $type='list_type', $extensionKey=null)
Definition: ExtensionManagementUtility.php:1236
‪TYPO3\CMS\Core\Utility\ArrayUtility\mergeRecursiveWithOverrule
‪static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)
Definition: ArrayUtility.php:614
‪TYPO3\CMS\Extbase\Utility\ExtensionUtility\registerTypeConverter
‪static registerTypeConverter($typeConverterClassName)
Definition: ExtensionUtility.php:211
‪TYPO3\CMS\Extbase\Utility\ExtensionUtility\PLUGIN_TYPE_PLUGIN
‪const PLUGIN_TYPE_PLUGIN
Definition: ExtensionUtility.php:23
‪TYPO3\CMS\Extbase\Utility\ExtensionUtility\configurePlugin
‪static configurePlugin($extensionName, $pluginName, array $controllerActions, array $nonCacheableControllerActions=[], $pluginType=self::PLUGIN_TYPE_PLUGIN)
Definition: ExtensionUtility.php:43
‪TYPO3\CMS\Extbase\Utility\ExtensionUtility\checkPluginNameFormat
‪static checkPluginNameFormat($pluginName)
Definition: ExtensionUtility.php:256
‪TYPO3\CMS\Extbase\Utility\ExtensionUtility\checkExtensionNameFormat
‪static checkExtensionNameFormat($extensionName)
Definition: ExtensionUtility.php:243
‪TYPO3\CMS\Extbase\Utility\ExtensionUtility\registerPlugin
‪static registerPlugin($extensionName, $pluginName, $pluginTitle, $pluginIcon=null)
Definition: ExtensionUtility.php:114
‪TYPO3\CMS\Core\Utility\ExtensionManagementUtility\addTypoScript
‪static addTypoScript(string $key, string $type, string $content, $afterStaticUid=0)
Definition: ExtensionManagementUtility.php:1463
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Extbase\Utility
Definition: DebuggerUtility.php:2