[BUGFIX] Move option formatToPageTypeMapping
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Service / ExtensionService.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Service;
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
17 use TYPO3\CMS\Core\Database\ConnectionPool;
18 use TYPO3\CMS\Core\Database\Query\Restriction\FrontendRestrictionContainer;
19 use TYPO3\CMS\Core\Utility\ArrayUtility;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21 use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
22
23 /**
24 * Service for determining basic extension params
25 */
26 class ExtensionService implements \TYPO3\CMS\Core\SingletonInterface
27 {
28 const PLUGIN_TYPE_PLUGIN = 'list_type';
29 const PLUGIN_TYPE_CONTENT_ELEMENT = 'CType';
30
31 /**
32 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
33 */
34 protected $objectManager;
35
36 /**
37 * @var ConfigurationManagerInterface
38 */
39 protected $configurationManager;
40
41 /**
42 * Cache of result for getTargetPidByPlugin()
43 * @var array
44 */
45 protected $targetPidPluginCache = [];
46
47 /**
48 * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
49 */
50 public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
51 {
52 $this->objectManager = $objectManager;
53 }
54
55 /**
56 * @param ConfigurationManagerInterface $configurationManager
57 */
58 public function injectConfigurationManager(ConfigurationManagerInterface $configurationManager)
59 {
60 $this->configurationManager = $configurationManager;
61 }
62
63 /**
64 * Determines the plugin namespace of the specified plugin (defaults to "tx_[extensionname]_[pluginname]")
65 * If plugin.tx_$pluginSignature.view.pluginNamespace is set, this value is returned
66 * If pluginNamespace is not specified "tx_[extensionname]_[pluginname]" is returned.
67 *
68 * @param string $extensionName name of the extension to retrieve the namespace for
69 * @param string $pluginName name of the plugin to retrieve the namespace for
70 * @return string plugin namespace
71 */
72 public function getPluginNamespace($extensionName, $pluginName)
73 {
74 $pluginSignature = strtolower($extensionName . '_' . $pluginName);
75 $defaultPluginNamespace = 'tx_' . $pluginSignature;
76 $frameworkConfiguration = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK, $extensionName, $pluginName);
77 if (!isset($frameworkConfiguration['view']['pluginNamespace']) || empty($frameworkConfiguration['view']['pluginNamespace'])) {
78 return $defaultPluginNamespace;
79 }
80 return $frameworkConfiguration['view']['pluginNamespace'];
81 }
82
83 /**
84 * Iterates through the global TypoScript configuration and returns the name of the plugin
85 * that matches specified extensionName, controllerName and actionName.
86 * If no matching plugin was found, NULL is returned.
87 * If more than one plugin matches and the current plugin is not configured to handle the action,
88 * an Exception will be thrown
89 *
90 * @param string $extensionName name of the target extension (UpperCamelCase)
91 * @param string $controllerName name of the target controller (UpperCamelCase)
92 * @param string $actionName name of the target action (lowerCamelCase)
93 * @throws \TYPO3\CMS\Extbase\Exception
94 * @return string name of the target plugin (UpperCamelCase) or NULL if no matching plugin configuration was found
95 */
96 public function getPluginNameByAction($extensionName, $controllerName, $actionName)
97 {
98 $frameworkConfiguration = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
99 // check, whether the current plugin is configured to handle the action
100 if ($extensionName === $frameworkConfiguration['extensionName']) {
101 if (isset($frameworkConfiguration['controllerConfiguration'][$controllerName]) && in_array($actionName, $frameworkConfiguration['controllerConfiguration'][$controllerName]['actions'])) {
102 return $frameworkConfiguration['pluginName'];
103 }
104 }
105 $plugins = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'] ?? false;
106 if (!$plugins) {
107 return null;
108 }
109 $pluginNames = [];
110 foreach ($plugins as $pluginName => $pluginConfiguration) {
111 foreach ($pluginConfiguration['controllers'] ?? [] as $pluginControllerName => $pluginControllerActions) {
112 if (strtolower($pluginControllerName) !== strtolower($controllerName)) {
113 continue;
114 }
115 if (in_array($actionName, $pluginControllerActions['actions'])) {
116 $pluginNames[] = $pluginName;
117 }
118 }
119 }
120 if (count($pluginNames) > 1) {
121 throw new \TYPO3\CMS\Extbase\Exception('There is more than one plugin that can handle this request (Extension: "' . $extensionName . '", Controller: "' . $controllerName . '", action: "' . $actionName . '"). Please specify "pluginName" argument', 1280825466);
122 }
123 return !empty($pluginNames) ? $pluginNames[0] : null;
124 }
125
126 /**
127 * Checks if the given action is cacheable or not.
128 *
129 * @param string $extensionName Name of the target extension, without underscores
130 * @param string $pluginName Name of the target plugin
131 * @param string $controllerName Name of the target controller
132 * @param string $actionName Name of the action to be called
133 * @return bool TRUE if the specified plugin action is cacheable, otherwise FALSE
134 */
135 public function isActionCacheable($extensionName, $pluginName, $controllerName, $actionName)
136 {
137 $frameworkConfiguration = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK, $extensionName, $pluginName);
138 if (isset($frameworkConfiguration['controllerConfiguration'][$controllerName]) && is_array($frameworkConfiguration['controllerConfiguration'][$controllerName]) && is_array($frameworkConfiguration['controllerConfiguration'][$controllerName]['nonCacheableActions']) && in_array($actionName, $frameworkConfiguration['controllerConfiguration'][$controllerName]['nonCacheableActions'])) {
139 return false;
140 }
141 return true;
142 }
143
144 /**
145 * Determines the target page of the specified plugin.
146 * If plugin.tx_$pluginSignature.view.defaultPid is set, this value is used as target page id
147 * If defaultPid is set to "auto", a the target pid is determined by loading the tt_content record that contains this plugin
148 * If the page could not be determined, NULL is returned
149 * If defaultPid is "auto" and more than one page contains the specified plugin, an Exception is thrown
150 *
151 * @param string $extensionName name of the extension to retrieve the target PID for
152 * @param string $pluginName name of the plugin to retrieve the target PID for
153 * @throws \TYPO3\CMS\Extbase\Exception
154 * @return int uid of the target page or NULL if target page could not be determined
155 */
156 public function getTargetPidByPlugin($extensionName, $pluginName)
157 {
158 $frameworkConfiguration = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK, $extensionName, $pluginName);
159 if (!isset($frameworkConfiguration['view']['defaultPid']) || empty($frameworkConfiguration['view']['defaultPid'])) {
160 return null;
161 }
162 $pluginSignature = strtolower($extensionName . '_' . $pluginName);
163 if ($frameworkConfiguration['view']['defaultPid'] === 'auto') {
164 if (!array_key_exists($pluginSignature, $this->targetPidPluginCache)) {
165 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
166 ->getQueryBuilderForTable('tt_content');
167 $queryBuilder->setRestrictions(GeneralUtility::makeInstance(FrontendRestrictionContainer::class));
168
169 $pages = $queryBuilder
170 ->select('pid')
171 ->from('tt_content')
172 ->where(
173 $queryBuilder->expr()->eq(
174 'list_type',
175 $queryBuilder->createNamedParameter($pluginSignature, \PDO::PARAM_STR)
176 ),
177 $queryBuilder->expr()->eq(
178 'CType',
179 $queryBuilder->createNamedParameter('list', \PDO::PARAM_STR)
180 ),
181 $queryBuilder->expr()->eq(
182 'sys_language_uid',
183 $queryBuilder->createNamedParameter($GLOBALS['TSFE']->sys_language_uid, \PDO::PARAM_INT)
184 )
185 )
186 ->setMaxResults(2)
187 ->execute()
188 ->fetchAll();
189
190 if (count($pages) > 1) {
191 throw new \TYPO3\CMS\Extbase\Exception('There is more than one "' . $pluginSignature . '" plugin in the current page tree. Please remove one plugin or set the TypoScript configuration "plugin.tx_' . $pluginSignature . '.view.defaultPid" to a fixed page id', 1280773643);
192 }
193 $this->targetPidPluginCache[$pluginSignature] = !empty($pages) ? $pages[0]['pid'] : null;
194 }
195 return $this->targetPidPluginCache[$pluginSignature];
196 }
197 return (int)$frameworkConfiguration['view']['defaultPid'];
198 }
199
200 /**
201 * This returns the name of the first controller of the given plugin.
202 *
203 * @param string $extensionName name of the extension to retrieve the target PID for
204 * @param string $pluginName name of the plugin to retrieve the target PID for
205 * @return string|null
206 */
207 public function getDefaultControllerNameByPlugin($extensionName, $pluginName)
208 {
209 $controllers = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName]['controllers'] ?? false;
210 return $controllers ? key($controllers) : null;
211 }
212
213 /**
214 * This returns the name of the first action of the given plugin controller.
215 *
216 * @param string $extensionName name of the extension to retrieve the target PID for
217 * @param string $pluginName name of the plugin to retrieve the target PID for
218 * @param string $controllerName name of the controller to retrieve default action for
219 * @return string|null
220 */
221 public function getDefaultActionNameByPluginAndController($extensionName, $pluginName, $controllerName)
222 {
223 $actions = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName]['controllers'][$controllerName]['actions'] ?? false;
224 return $actions ? current($actions) : null;
225 }
226
227 /**
228 * Resolve the page type number to use for building a link for a specific format
229 *
230 * @param string $extensionName name of the extension that has defined the target page type
231 * @param string $format The format for which to look up the page type
232 * @return int Page type number for target page
233 */
234 public function getTargetPageTypeByFormat($extensionName, $format)
235 {
236 // Legacy location
237 $settings = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS, $extensionName);
238 if (isset($settings['view']['formatToPageTypeMapping']) && is_array($settings['view']['formatToPageTypeMapping'])) {
239 trigger_error('Extension "' . $extensionName . '": Defining settings.view.formatToPageTypeMapping will be removed in TYPO3 10. Move definition to view.formatToPageTypeMapping.');
240 $formatToPageTypeMapping = $settings['view']['formatToPageTypeMapping'];
241 }
242 // Default behaviour
243 $settings = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK, $extensionName);
244 if (isset($settings['view']['formatToPageTypeMapping']) && is_array($settings['view']['formatToPageTypeMapping'])) {
245 ArrayUtility::mergeRecursiveWithOverrule($formatToPageTypeMapping, $settings['view']['formatToPageTypeMapping']);
246 }
247 return $formatToPageTypeMapping[$format] ?? 0;
248 }
249 }