‪TYPO3CMS  11.5
CategoryRegistry.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 
24 
30 {
34  protected ‪$registry = [];
35 
39  protected ‪$extensions = [];
40 
44  protected ‪$addedCategoryTabs = [];
45 
49  protected ‪$template = '';
50 
57  public static function ‪getInstance()
58  {
59  return GeneralUtility::makeInstance(__CLASS__);
60  }
61 
65  public function ‪__construct()
66  {
67  $this->template = str_repeat(PHP_EOL, 3) . 'CREATE TABLE %s (' . PHP_EOL
68  . ' %s int(11) DEFAULT \'0\' NOT NULL' . PHP_EOL . ');' . str_repeat(PHP_EOL, 3);
69  }
70 
90  public function ‪add($extensionKey, $tableName, $fieldName = 'categories', array $options = [], $override = false)
91  {
92  $didRegister = false;
93  if (empty($tableName) || !is_string($tableName)) {
94  throw new \InvalidArgumentException('No or invalid table name "' . $tableName . '" given.', 1369122038);
95  }
96  if (empty($extensionKey) || !is_string($extensionKey)) {
97  throw new \InvalidArgumentException('No or invalid extension key "' . $extensionKey . '" given.', 1397836158);
98  }
99 
100  if ($override) {
101  $this->remove($tableName, $fieldName);
102  }
103 
104  if (!$this->‪isRegistered($tableName, $fieldName)) {
105  $this->registry[$tableName][$fieldName] = $options;
106  $this->extensions[$extensionKey][$tableName][$fieldName] = $fieldName;
107 
108  if (isset(‪$GLOBALS['TCA'][$tableName]['columns'])) {
109  $this->‪applyTcaForTableAndField($tableName, $fieldName);
110  $didRegister = true;
111  }
112  }
113 
114  return $didRegister;
115  }
116 
123  public function ‪getExtensionKeys()
124  {
125  return array_keys($this->extensions);
126  }
127 
134  public function ‪getCategorizedTables()
135  {
136  return array_keys($this->registry);
137  }
138 
147  public function ‪getCategoryFieldsForTable(array &$configuration)
148  {
149  $table = $configuration['config']['itemsProcConfig']['table'] ?? '';
150  // Return early if no table is defined
151  if (empty($table)) {
152  throw new \UnexpectedValueException('No table is given.', 1381823570);
153  }
154  // Loop on all registries and find entries for the correct table
155  foreach ($this->registry as $tableName => ‪$fields) {
156  if ($tableName === $table) {
157  foreach (‪$fields as $fieldName => $options) {
158  $fieldLabel = $this->‪getLanguageService()->‪sL(‪$GLOBALS['TCA'][$tableName]['columns'][$fieldName]['label']);
159  $configuration['items'][] = [$fieldLabel, $fieldName];
160  }
161  }
162  }
163  }
164 
173  public function ‪isRegistered($tableName, $fieldName = 'categories')
174  {
175  return isset($this->registry[$tableName][$fieldName]);
176  }
177 
184  public function ‪getDatabaseTableDefinitions()
185  {
186  $sql = '';
187  foreach ($this->‪getExtensionKeys() as $extensionKey) {
188  $sql .= $this->‪getDatabaseTableDefinition($extensionKey);
189  }
190  return $sql;
191  }
192 
200  public function ‪getDatabaseTableDefinition($extensionKey)
201  {
202  if (!isset($this->extensions[$extensionKey]) || !is_array($this->extensions[$extensionKey])) {
203  return '';
204  }
205  $sql = '';
206 
207  foreach ($this->extensions[$extensionKey] as $tableName => ‪$fields) {
208  foreach (‪$fields as $fieldName) {
209  $sql .= sprintf($this->template, $tableName, $fieldName);
210  }
211  }
212  return $sql;
213  }
214 
220  public function ‪applyTcaForPreRegisteredTables()
221  {
223  foreach ($this->registry as $tableName => ‪$fields) {
224  foreach (‪$fields as $fieldName => $_) {
225  $this->‪applyTcaForTableAndField($tableName, $fieldName);
226  }
227  }
228  }
229 
236  protected function ‪applyTcaForTableAndField($tableName, $fieldName)
237  {
238  $this->‪addTcaColumn($tableName, $fieldName, $this->registry[$tableName][$fieldName]);
239  $this->‪addToAllTCAtypes($tableName, $fieldName, $this->registry[$tableName][$fieldName]);
240  }
241 
245  protected function ‪registerDefaultCategorizedTables()
246  {
247  $defaultCategorizedTables = ‪GeneralUtility::trimExplode(
248  ',',
249  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['defaultCategorizedTables'],
250  true
251  );
252  if ($defaultCategorizedTables !== []) {
253  trigger_error(
254  '$GLOBALS[\'TYPO3_CONF_VARS\'][\'SYS\'][\'defaultCategorizedTables\'] is deprecated and will be removed in v12. Register category fields with the TCA type "category" instead.',
255  E_USER_DEPRECATED
256  );
257  }
258  foreach ($defaultCategorizedTables as $defaultCategorizedTable) {
259  if (!$this->‪isRegistered($defaultCategorizedTable)) {
260  $this->‪add('core', $defaultCategorizedTable, 'categories');
261  }
262  }
263  }
264 
275  protected function ‪addToAllTCAtypes($tableName, $fieldName, array $options)
276  {
277  // Makes sure to add more TCA to an existing structure
278  if (isset(‪$GLOBALS['TCA'][$tableName]['columns'])) {
279  if (empty($options['fieldList'])) {
280  $fieldList = $this->‪addCategoryTab($tableName, $fieldName);
281  } else {
282  $fieldList = $options['fieldList'];
283  }
284 
285  $typesList = '';
286  if (isset($options['typesList']) && $options['typesList'] !== '') {
287  $typesList = $options['typesList'];
288  }
289 
290  $position = '';
291  if (!empty($options['position'])) {
292  $position = $options['position'];
293  }
294 
295  // Makes the new "categories" field to be visible in TSFE.
296  ‪ExtensionManagementUtility::addToAllTCAtypes($tableName, $fieldList, $typesList, $position);
297  }
298  }
299 
308  protected function ‪addCategoryTab($tableName, $fieldName)
309  {
310  $fieldList = '';
311  if (!isset($this->addedCategoryTabs[$tableName])) {
312  $fieldList .= '--div--;LLL:EXT:core/Resources/Private/Language/locallang_tca.xlf:sys_category.tabs.category, ';
313  $this->addedCategoryTabs[$tableName] = $tableName;
314  }
315  $fieldList .= $fieldName;
316  return $fieldList;
317  }
318 
331  protected function ‪addTcaColumn($tableName, $fieldName, array $options)
332  {
333  // Makes sure to add more TCA to an existing structure
334  if (isset(‪$GLOBALS['TCA'][$tableName]['columns'])) {
335  // Take specific label into account
336  $label = 'LLL:EXT:core/Resources/Private/Language/locallang_tca.xlf:sys_category.categories';
337  if (!empty($options['label'])) {
338  $label = $options['label'];
339  }
340 
341  // Take specific value of exclude flag into account
342  $exclude = true;
343  if (isset($options['exclude'])) {
344  $exclude = (bool)$options['exclude'];
345  }
346 
347  $fieldConfiguration = empty($options['fieldConfiguration']) ? [] : $options['fieldConfiguration'];
348 
349  $columns = [
350  $fieldName => [
351  'exclude' => $exclude,
352  'label' => $label,
353  'config' => static::getTcaFieldConfiguration($tableName, $fieldName, $fieldConfiguration),
354  ],
355  ];
356 
357  if (isset($options['l10n_mode'])) {
358  $columns[$fieldName]['l10n_mode'] = $options['l10n_mode'];
359  }
360  if (isset($options['l10n_display'])) {
361  $columns[$fieldName]['l10n_display'] = $options['l10n_display'];
362  }
363  if (isset($options['displayCond'])) {
364  $columns[$fieldName]['displayCond'] = $options['displayCond'];
365  }
366  if (isset($options['onChange'])) {
367  $columns[$fieldName]['onChange'] = $options['onChange'];
368  }
369 
370  // Register opposite references for the foreign side of a relation
371  if (empty(‪$GLOBALS['TCA']['sys_category']['columns']['items']['config']['MM_oppositeUsage'][$tableName])) {
372  ‪$GLOBALS['TCA']['sys_category']['columns']['items']['config']['MM_oppositeUsage'][$tableName] = [];
373  }
374  if (!in_array($fieldName, ‪$GLOBALS['TCA']['sys_category']['columns']['items']['config']['MM_oppositeUsage'][$tableName])) {
375  ‪$GLOBALS['TCA']['sys_category']['columns']['items']['config']['MM_oppositeUsage'][$tableName][] = $fieldName;
376  }
377 
378  // Adding fields to an existing table definition
379  ‪ExtensionManagementUtility::addTCAcolumns($tableName, $columns);
380  }
381  }
382 
393  public static function ‪getTcaFieldConfiguration($tableName, $fieldName = 'categories', array $fieldConfigurationOverride = [])
394  {
395  // Forges a new field, default name is "categories"
396  $fieldConfiguration = [
397  'type' => 'select',
398  'renderType' => 'selectTree',
399  'foreign_table' => 'sys_category',
400  'foreign_table_where' => ' AND {#sys_category}.{#sys_language_uid} IN (-1, 0)',
401  'MM' => 'sys_category_record_mm',
402  'MM_opposite_field' => 'items',
403  'MM_match_fields' => [
404  'tablenames' => $tableName,
405  'fieldname' => $fieldName,
406  ],
407  'size' => 20,
408  'maxitems' => 9999,
409  'treeConfig' => [
410  'parentField' => 'parent',
411  'appearance' => [
412  'expandAll' => true,
413  'showHeader' => true,
414  'maxLevels' => 99,
415  ],
416  ],
417  ];
418 
419  // Merge changes to TCA configuration
420  if (!empty($fieldConfigurationOverride)) {
422  $fieldConfiguration,
423  $fieldConfigurationOverride
424  );
425  }
426 
427  return $fieldConfiguration;
428  }
429 
437  public function ‪addCategoryDatabaseSchema(AlterTableDefinitionStatementsEvent $event): void
438  {
440  $event->addSqlData($this->‪getDatabaseTableDefinitions());
441  }
442 
446  protected function ‪getLanguageService()
447  {
448  return ‪$GLOBALS['LANG'];
449  }
450 
457  protected function remove($tableName, $fieldName)
458  {
459  if (!$this->‪isRegistered($tableName, $fieldName)) {
460  return;
461  }
462 
463  unset($this->registry[$tableName][$fieldName]);
464 
465  foreach ($this->extensions as $extensionKey => $tableFieldConfig) {
466  foreach ($tableFieldConfig as $extTableName => $fieldNameArray) {
467  if ($extTableName === $tableName && isset($fieldNameArray[$fieldName])) {
468  unset($this->extensions[$extensionKey][$tableName][$fieldName]);
469  break;
470  }
471  }
472  }
473 
474  // If no more fields are configured we unregister the categories tab.
475  if (empty($this->registry[$tableName]) && isset($this->addedCategoryTabs[$tableName])) {
476  unset($this->addedCategoryTabs[$tableName]);
477  }
478  }
479 }
‪TYPO3\CMS\Core\Category\CategoryRegistry\applyTcaForTableAndField
‪applyTcaForTableAndField($tableName, $fieldName)
Definition: CategoryRegistry.php:232
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static list< string > trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
Definition: GeneralUtility.php:999
‪TYPO3\CMS\Core\Category\CategoryRegistry\$registry
‪array $registry
Definition: CategoryRegistry.php:33
‪TYPO3\CMS\Core\Category\CategoryRegistry\registerDefaultCategorizedTables
‪registerDefaultCategorizedTables()
Definition: CategoryRegistry.php:241
‪TYPO3\CMS\Core\Category\CategoryRegistry\addCategoryDatabaseSchema
‪addCategoryDatabaseSchema(AlterTableDefinitionStatementsEvent $event)
Definition: CategoryRegistry.php:433
‪TYPO3\CMS\Core\Category\CategoryRegistry\isRegistered
‪bool isRegistered($tableName, $fieldName='categories')
Definition: CategoryRegistry.php:169
‪TYPO3\CMS\Core\Category\CategoryRegistry\getTcaFieldConfiguration
‪static array getTcaFieldConfiguration($tableName, $fieldName='categories', array $fieldConfigurationOverride=[])
Definition: CategoryRegistry.php:389
‪TYPO3\CMS\Core\Category\CategoryRegistry\addCategoryTab
‪string addCategoryTab($tableName, $fieldName)
Definition: CategoryRegistry.php:304
‪TYPO3\CMS\Core\Category\CategoryRegistry\$addedCategoryTabs
‪array $addedCategoryTabs
Definition: CategoryRegistry.php:41
‪TYPO3\CMS\Core\Database\Event\AlterTableDefinitionStatementsEvent\addSqlData
‪addSqlData($data)
Definition: AlterTableDefinitionStatementsEvent.php:35
‪TYPO3\CMS\Core\Utility\ExtensionManagementUtility\addToAllTCAtypes
‪static addToAllTCAtypes($table, $newFieldsString, $typeList='', $position='')
Definition: ExtensionManagementUtility.php:224
‪TYPO3\CMS\Core\Utility\ExtensionManagementUtility\addTCAcolumns
‪static addTCAcolumns($table, $columnArray)
Definition: ExtensionManagementUtility.php:203
‪TYPO3\CMS\Core\Utility\ArrayUtility\mergeRecursiveWithOverrule
‪static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)
Definition: ArrayUtility.php:654
‪TYPO3\CMS\Core\Localization\LanguageService\sL
‪string sL($input)
Definition: LanguageService.php:161
‪$fields
‪$fields
Definition: pages.php:5
‪TYPO3\CMS\Core\Category\CategoryRegistry\$template
‪string $template
Definition: CategoryRegistry.php:45
‪TYPO3\CMS\Core\Utility\ExtensionManagementUtility
Definition: ExtensionManagementUtility.php:43
‪TYPO3\CMS\Core\Category\CategoryRegistry\getInstance
‪static CategoryRegistry getInstance()
Definition: CategoryRegistry.php:53
‪TYPO3\CMS\Core\Category\CategoryRegistry
Definition: CategoryRegistry.php:30
‪TYPO3\CMS\Core\Category
Definition: CategoryRegistry.php:16
‪TYPO3\CMS\Core\Category\CategoryRegistry\addToAllTCAtypes
‪addToAllTCAtypes($tableName, $fieldName, array $options)
Definition: CategoryRegistry.php:271
‪TYPO3\CMS\Core\Category\CategoryRegistry\getDatabaseTableDefinition
‪string getDatabaseTableDefinition($extensionKey)
Definition: CategoryRegistry.php:196
‪TYPO3\CMS\Core\Category\CategoryRegistry\getLanguageService
‪LanguageService getLanguageService()
Definition: CategoryRegistry.php:442
‪TYPO3\CMS\Core\Category\CategoryRegistry\getDatabaseTableDefinitions
‪string getDatabaseTableDefinitions()
Definition: CategoryRegistry.php:180
‪TYPO3\CMS\Core\Category\CategoryRegistry\getCategorizedTables
‪array getCategorizedTables()
Definition: CategoryRegistry.php:130
‪TYPO3\CMS\Core\Category\CategoryRegistry\getExtensionKeys
‪array getExtensionKeys()
Definition: CategoryRegistry.php:119
‪TYPO3\CMS\Core\Category\CategoryRegistry\add
‪bool add($extensionKey, $tableName, $fieldName='categories', array $options=[], $override=false)
Definition: CategoryRegistry.php:86
‪TYPO3\CMS\Core\Category\CategoryRegistry\getCategoryFieldsForTable
‪getCategoryFieldsForTable(array &$configuration)
Definition: CategoryRegistry.php:143
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:24
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Category\CategoryRegistry\addTcaColumn
‪addTcaColumn($tableName, $fieldName, array $options)
Definition: CategoryRegistry.php:327
‪TYPO3\CMS\Core\Database\Event\AlterTableDefinitionStatementsEvent
Definition: AlterTableDefinitionStatementsEvent.php:24
‪TYPO3\CMS\Core\Category\CategoryRegistry\applyTcaForPreRegisteredTables
‪applyTcaForPreRegisteredTables()
Definition: CategoryRegistry.php:216
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:42
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Category\CategoryRegistry\__construct
‪__construct()
Definition: CategoryRegistry.php:61
‪TYPO3\CMS\Core\Category\CategoryRegistry\$extensions
‪array $extensions
Definition: CategoryRegistry.php:37