‪TYPO3CMS  10.4
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 
29 {
33  protected ‪$registry = [];
34 
38  protected ‪$extensions = [];
39 
43  protected ‪$addedCategoryTabs = [];
44 
48  protected ‪$template = '';
49 
56  public static function ‪getInstance()
57  {
58  return GeneralUtility::makeInstance(__CLASS__);
59  }
60 
64  public function ‪__construct()
65  {
66  $this->template = str_repeat(PHP_EOL, 3) . 'CREATE TABLE %s (' . PHP_EOL
67  . ' %s int(11) DEFAULT \'0\' NOT NULL' . PHP_EOL . ');' . str_repeat(PHP_EOL, 3);
68  }
69 
89  public function ‪add($extensionKey, $tableName, $fieldName = 'categories', array $options = [], $override = false)
90  {
91  $didRegister = false;
92  if (empty($tableName) || !is_string($tableName)) {
93  throw new \InvalidArgumentException('No or invalid table name "' . $tableName . '" given.', 1369122038);
94  }
95  if (empty($extensionKey) || !is_string($extensionKey)) {
96  throw new \InvalidArgumentException('No or invalid extension key "' . $extensionKey . '" given.', 1397836158);
97  }
98 
99  if ($override) {
100  $this->remove($tableName, $fieldName);
101  }
102 
103  if (!$this->‪isRegistered($tableName, $fieldName)) {
104  $this->registry[$tableName][$fieldName] = $options;
105  $this->extensions[$extensionKey][$tableName][$fieldName] = $fieldName;
106 
107  if (isset(‪$GLOBALS['TCA'][$tableName]['columns'])) {
108  $this->‪applyTcaForTableAndField($tableName, $fieldName);
109  $didRegister = true;
110  }
111  }
112 
113  return $didRegister;
114  }
115 
122  public function ‪getExtensionKeys()
123  {
124  return array_keys($this->extensions);
125  }
126 
133  public function ‪getCategorizedTables()
134  {
135  return array_keys($this->registry);
136  }
137 
146  public function ‪getCategoryFieldsForTable(array &$configuration)
147  {
148  $table = $configuration['config']['itemsProcConfig']['table'] ?? '';
149  // Return early if no table is defined
150  if (empty($table)) {
151  throw new \UnexpectedValueException('No table is given.', 1381823570);
152  }
153  // Loop on all registries and find entries for the correct table
154  foreach ($this->registry as $tableName => ‪$fields) {
155  if ($tableName === $table) {
156  foreach (‪$fields as $fieldName => $options) {
157  $fieldLabel = $this->‪getLanguageService()->‪sL(‪$GLOBALS['TCA'][$tableName]['columns'][$fieldName]['label']);
158  $configuration['items'][] = [$fieldLabel, $fieldName];
159  }
160  }
161  }
162  }
163 
172  public function ‪isRegistered($tableName, $fieldName = 'categories')
173  {
174  return isset($this->registry[$tableName][$fieldName]);
175  }
176 
183  public function ‪getDatabaseTableDefinitions()
184  {
185  $sql = '';
186  foreach ($this->‪getExtensionKeys() as $extensionKey) {
187  $sql .= $this->‪getDatabaseTableDefinition($extensionKey);
188  }
189  return $sql;
190  }
191 
199  public function ‪getDatabaseTableDefinition($extensionKey)
200  {
201  if (!isset($this->extensions[$extensionKey]) || !is_array($this->extensions[$extensionKey])) {
202  return '';
203  }
204  $sql = '';
205 
206  foreach ($this->extensions[$extensionKey] as $tableName => ‪$fields) {
207  foreach (‪$fields as $fieldName) {
208  $sql .= sprintf($this->template, $tableName, $fieldName);
209  }
210  }
211  return $sql;
212  }
213 
219  public function ‪applyTcaForPreRegisteredTables()
220  {
222  foreach ($this->registry as $tableName => ‪$fields) {
223  foreach (‪$fields as $fieldName => $_) {
224  $this->‪applyTcaForTableAndField($tableName, $fieldName);
225  }
226  }
227  }
228 
235  protected function ‪applyTcaForTableAndField($tableName, $fieldName)
236  {
237  $this->‪addTcaColumn($tableName, $fieldName, $this->registry[$tableName][$fieldName]);
238  $this->‪addToAllTCAtypes($tableName, $fieldName, $this->registry[$tableName][$fieldName]);
239  }
240 
244  protected function ‪registerDefaultCategorizedTables()
245  {
246  $defaultCategorizedTables = ‪GeneralUtility::trimExplode(
247  ',',
248  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['defaultCategorizedTables'],
249  true
250  );
251  foreach ($defaultCategorizedTables as $defaultCategorizedTable) {
252  if (!$this->‪isRegistered($defaultCategorizedTable)) {
253  $this->‪add('core', $defaultCategorizedTable, 'categories');
254  }
255  }
256  }
257 
268  protected function ‪addToAllTCAtypes($tableName, $fieldName, array $options)
269  {
270 
271  // Makes sure to add more TCA to an existing structure
272  if (isset(‪$GLOBALS['TCA'][$tableName]['columns'])) {
273  if (empty($options['fieldList'])) {
274  $fieldList = $this->‪addCategoryTab($tableName, $fieldName);
275  } else {
276  $fieldList = $options['fieldList'];
277  }
278 
279  $typesList = '';
280  if (isset($options['typesList']) && $options['typesList'] !== '') {
281  $typesList = $options['typesList'];
282  }
283 
284  $position = '';
285  if (!empty($options['position'])) {
286  $position = $options['position'];
287  }
288 
289  // Makes the new "categories" field to be visible in TSFE.
290  ‪ExtensionManagementUtility::addToAllTCAtypes($tableName, $fieldList, $typesList, $position);
291  }
292  }
293 
302  protected function ‪addCategoryTab($tableName, $fieldName)
303  {
304  $fieldList = '';
305  if (!isset($this->addedCategoryTabs[$tableName])) {
306  $fieldList .= '--div--;LLL:EXT:core/Resources/Private/Language/locallang_tca.xlf:sys_category.tabs.category, ';
307  $this->addedCategoryTabs[$tableName] = $tableName;
308  }
309  $fieldList .= $fieldName;
310  return $fieldList;
311  }
312 
325  protected function ‪addTcaColumn($tableName, $fieldName, array $options)
326  {
327  // Makes sure to add more TCA to an existing structure
328  if (isset(‪$GLOBALS['TCA'][$tableName]['columns'])) {
329  // Take specific label into account
330  $label = 'LLL:EXT:core/Resources/Private/Language/locallang_tca.xlf:sys_category.categories';
331  if (!empty($options['label'])) {
332  $label = $options['label'];
333  }
334 
335  // Take specific value of exclude flag into account
336  $exclude = true;
337  if (isset($options['exclude'])) {
338  $exclude = (bool)$options['exclude'];
339  }
340 
341  $fieldConfiguration = empty($options['fieldConfiguration']) ? [] : $options['fieldConfiguration'];
342 
343  $columns = [
344  $fieldName => [
345  'exclude' => $exclude,
346  'label' => $label,
347  'config' => static::getTcaFieldConfiguration($tableName, $fieldName, $fieldConfiguration),
348  ],
349  ];
350 
351  if (isset($options['l10n_mode'])) {
352  $columns[$fieldName]['l10n_mode'] = $options['l10n_mode'];
353  }
354  if (isset($options['l10n_display'])) {
355  $columns[$fieldName]['l10n_display'] = $options['l10n_display'];
356  }
357  if (isset($options['displayCond'])) {
358  $columns[$fieldName]['displayCond'] = $options['displayCond'];
359  }
360  if (isset($options['onChange'])) {
361  $columns[$fieldName]['onChange'] = $options['onChange'];
362  }
363 
364  // Register opposite references for the foreign side of a relation
365  if (empty(‪$GLOBALS['TCA']['sys_category']['columns']['items']['config']['MM_oppositeUsage'][$tableName])) {
366  ‪$GLOBALS['TCA']['sys_category']['columns']['items']['config']['MM_oppositeUsage'][$tableName] = [];
367  }
368  if (!in_array($fieldName, ‪$GLOBALS['TCA']['sys_category']['columns']['items']['config']['MM_oppositeUsage'][$tableName])) {
369  ‪$GLOBALS['TCA']['sys_category']['columns']['items']['config']['MM_oppositeUsage'][$tableName][] = $fieldName;
370  }
371 
372  // Adding fields to an existing table definition
373  ‪ExtensionManagementUtility::addTCAcolumns($tableName, $columns);
374  }
375  }
376 
387  public static function ‪getTcaFieldConfiguration($tableName, $fieldName = 'categories', array $fieldConfigurationOverride = [])
388  {
389  // Forges a new field, default name is "categories"
390  $fieldConfiguration = [
391  'type' => 'select',
392  'renderType' => 'selectTree',
393  'foreign_table' => 'sys_category',
394  'foreign_table_where' => ' AND sys_category.sys_language_uid IN (-1, 0)',
395  'MM' => 'sys_category_record_mm',
396  'MM_opposite_field' => 'items',
397  'MM_match_fields' => [
398  'tablenames' => $tableName,
399  'fieldname' => $fieldName,
400  ],
401  'size' => 20,
402  'maxitems' => 9999,
403  'treeConfig' => [
404  'parentField' => 'parent',
405  'appearance' => [
406  'expandAll' => true,
407  'showHeader' => true,
408  'maxLevels' => 99,
409  ],
410  ],
411  ];
412 
413  // Merge changes to TCA configuration
414  if (!empty($fieldConfigurationOverride)) {
416  $fieldConfiguration,
417  $fieldConfigurationOverride
418  );
419  }
420 
421  return $fieldConfiguration;
422  }
423 
431  public function ‪addCategoryDatabaseSchema(AlterTableDefinitionStatementsEvent $event): void
432  {
434  $event->addSqlData($this->‪getDatabaseTableDefinitions());
435  }
436 
440  protected function ‪getLanguageService()
441  {
442  return ‪$GLOBALS['LANG'];
443  }
444 
451  protected function remove($tableName, $fieldName)
452  {
453  if (!$this->‪isRegistered($tableName, $fieldName)) {
454  return;
455  }
456 
457  unset($this->registry[$tableName][$fieldName]);
458 
459  foreach ($this->extensions as $extensionKey => $tableFieldConfig) {
460  foreach ($tableFieldConfig as $extTableName => $fieldNameArray) {
461  if ($extTableName === $tableName && isset($fieldNameArray[$fieldName])) {
462  unset($this->extensions[$extensionKey][$tableName][$fieldName]);
463  break;
464  }
465  }
466  }
467 
468  // If no more fields are configured we unregister the categories tab.
469  if (empty($this->registry[$tableName]) && isset($this->addedCategoryTabs[$tableName])) {
470  unset($this->addedCategoryTabs[$tableName]);
471  }
472  }
473 }
‪TYPO3\CMS\Core\Category\CategoryRegistry\applyTcaForTableAndField
‪applyTcaForTableAndField($tableName, $fieldName)
Definition: CategoryRegistry.php:231
‪TYPO3\CMS\Core\Category\CategoryRegistry\$registry
‪array $registry
Definition: CategoryRegistry.php:32
‪TYPO3\CMS\Core\Category\CategoryRegistry\registerDefaultCategorizedTables
‪registerDefaultCategorizedTables()
Definition: CategoryRegistry.php:240
‪TYPO3\CMS\Core\Category\CategoryRegistry\addCategoryDatabaseSchema
‪addCategoryDatabaseSchema(AlterTableDefinitionStatementsEvent $event)
Definition: CategoryRegistry.php:427
‪TYPO3\CMS\Core\Category\CategoryRegistry\isRegistered
‪bool isRegistered($tableName, $fieldName='categories')
Definition: CategoryRegistry.php:168
‪TYPO3\CMS\Core\Category\CategoryRegistry\getTcaFieldConfiguration
‪static array getTcaFieldConfiguration($tableName, $fieldName='categories', array $fieldConfigurationOverride=[])
Definition: CategoryRegistry.php:383
‪TYPO3\CMS\Core\Category\CategoryRegistry\addCategoryTab
‪string addCategoryTab($tableName, $fieldName)
Definition: CategoryRegistry.php:298
‪TYPO3\CMS\Core\Category\CategoryRegistry\$addedCategoryTabs
‪array $addedCategoryTabs
Definition: CategoryRegistry.php:40
‪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:209
‪TYPO3\CMS\Core\Utility\ExtensionManagementUtility\addTCAcolumns
‪static addTCAcolumns($table, $columnArray)
Definition: ExtensionManagementUtility.php:188
‪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:194
‪$fields
‪$fields
Definition: pages.php:5
‪TYPO3\CMS\Core\Category\CategoryRegistry\$template
‪string $template
Definition: CategoryRegistry.php:44
‪TYPO3\CMS\Core\Utility\ExtensionManagementUtility
Definition: ExtensionManagementUtility.php:43
‪TYPO3\CMS\Core\Category\CategoryRegistry\getInstance
‪static CategoryRegistry getInstance()
Definition: CategoryRegistry.php:52
‪TYPO3\CMS\Core\Category\CategoryRegistry
Definition: CategoryRegistry.php:29
‪TYPO3\CMS\Core\Category
Definition: CategoryRegistry.php:16
‪TYPO3\CMS\Core\Category\CategoryRegistry\addToAllTCAtypes
‪addToAllTCAtypes($tableName, $fieldName, array $options)
Definition: CategoryRegistry.php:264
‪TYPO3\CMS\Core\Category\CategoryRegistry\getDatabaseTableDefinition
‪string getDatabaseTableDefinition($extensionKey)
Definition: CategoryRegistry.php:195
‪TYPO3\CMS\Core\Category\CategoryRegistry\getLanguageService
‪LanguageService getLanguageService()
Definition: CategoryRegistry.php:436
‪TYPO3\CMS\Core\Category\CategoryRegistry\getDatabaseTableDefinitions
‪string getDatabaseTableDefinitions()
Definition: CategoryRegistry.php:179
‪TYPO3\CMS\Core\Category\CategoryRegistry\getCategorizedTables
‪array getCategorizedTables()
Definition: CategoryRegistry.php:129
‪TYPO3\CMS\Core\Category\CategoryRegistry\getExtensionKeys
‪array getExtensionKeys()
Definition: CategoryRegistry.php:118
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static string[] trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
Definition: GeneralUtility.php:1059
‪TYPO3\CMS\Core\Category\CategoryRegistry\add
‪bool add($extensionKey, $tableName, $fieldName='categories', array $options=[], $override=false)
Definition: CategoryRegistry.php:85
‪TYPO3\CMS\Core\Category\CategoryRegistry\getCategoryFieldsForTable
‪getCategoryFieldsForTable(array &$configuration)
Definition: CategoryRegistry.php:142
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:24
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:23
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Category\CategoryRegistry\addTcaColumn
‪addTcaColumn($tableName, $fieldName, array $options)
Definition: CategoryRegistry.php:321
‪TYPO3\CMS\Core\Database\Event\AlterTableDefinitionStatementsEvent
Definition: AlterTableDefinitionStatementsEvent.php:24
‪TYPO3\CMS\Core\Category\CategoryRegistry\applyTcaForPreRegisteredTables
‪applyTcaForPreRegisteredTables()
Definition: CategoryRegistry.php:215
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:42
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Category\CategoryRegistry\__construct
‪__construct()
Definition: CategoryRegistry.php:60
‪TYPO3\CMS\Core\Category\CategoryRegistry\$extensions
‪array $extensions
Definition: CategoryRegistry.php:36