‪TYPO3CMS  9.5
CategoryRegistry.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 
27 {
31  protected ‪$registry = [];
32 
36  protected ‪$extensions = [];
37 
41  protected ‪$addedCategoryTabs = [];
42 
46  protected ‪$template = '';
47 
54  public static function ‪getInstance()
55  {
56  return GeneralUtility::makeInstance(__CLASS__);
57  }
58 
62  public function ‪__construct()
63  {
64  $this->template = str_repeat(PHP_EOL, 3) . 'CREATE TABLE %s (' . PHP_EOL
65  . ' %s int(11) DEFAULT \'0\' NOT NULL' . PHP_EOL . ');' . str_repeat(PHP_EOL, 3);
66  }
67 
87  public function ‪add($extensionKey, $tableName, $fieldName = 'categories', array $options = [], $override = false)
88  {
89  $didRegister = false;
90  if (empty($tableName) || !is_string($tableName)) {
91  throw new \InvalidArgumentException('No or invalid table name "' . $tableName . '" given.', 1369122038);
92  }
93  if (empty($extensionKey) || !is_string($extensionKey)) {
94  throw new \InvalidArgumentException('No or invalid extension key "' . $extensionKey . '" given.', 1397836158);
95  }
96 
97  if ($override) {
98  $this->remove($tableName, $fieldName);
99  }
100 
101  if (!$this->‪isRegistered($tableName, $fieldName)) {
102  $this->registry[$tableName][$fieldName] = $options;
103  $this->extensions[$extensionKey][$tableName][$fieldName] = $fieldName;
104 
105  if (isset(‪$GLOBALS['TCA'][$tableName]['columns'])) {
106  $this->‪applyTcaForTableAndField($tableName, $fieldName);
107  $didRegister = true;
108  }
109  }
110 
111  return $didRegister;
112  }
113 
120  public function ‪getExtensionKeys()
121  {
122  return array_keys($this->extensions);
123  }
124 
131  public function ‪getCategorizedTables()
132  {
133  return array_keys($this->registry);
134  }
135 
144  public function ‪getCategoryFieldsForTable(array &$configuration)
145  {
146  $table = $configuration['config']['itemsProcConfig']['table'] ?? '';
147  // Lookup table for legacy menu content element
148  if (empty($table)) {
149  $menuType = $configuration['row']['menu_type'][0] ?? '';
150  // Define the table being looked up from the type of menu
151  if ($menuType === 'categorized_pages') {
152  $table = 'pages';
153  } elseif ($menuType === 'categorized_content') {
154  $table = 'tt_content';
155  }
156  }
157  // Return early if no table is defined
158  if (empty($table)) {
159  throw new \UnexpectedValueException('The given menu_type is not supported.', 1381823570);
160  }
161  // Loop on all registries and find entries for the correct table
162  foreach ($this->registry as $tableName => ‪$fields) {
163  if ($tableName === $table) {
164  foreach (‪$fields as $fieldName => $options) {
165  $fieldLabel = $this->‪getLanguageService()->‪sL(‪$GLOBALS['TCA'][$tableName]['columns'][$fieldName]['label']);
166  $configuration['items'][] = [$fieldLabel, $fieldName];
167  }
168  }
169  }
170  }
171 
180  public function ‪isRegistered($tableName, $fieldName = 'categories')
181  {
182  return isset($this->registry[$tableName][$fieldName]);
183  }
184 
191  public function ‪getDatabaseTableDefinitions()
192  {
193  $sql = '';
194  foreach ($this->‪getExtensionKeys() as $extensionKey) {
195  $sql .= $this->‪getDatabaseTableDefinition($extensionKey);
196  }
197  return $sql;
198  }
199 
207  public function ‪getDatabaseTableDefinition($extensionKey)
208  {
209  if (!isset($this->extensions[$extensionKey]) || !is_array($this->extensions[$extensionKey])) {
210  return '';
211  }
212  $sql = '';
213 
214  foreach ($this->extensions[$extensionKey] as $tableName => ‪$fields) {
215  foreach (‪$fields as $fieldName) {
216  $sql .= sprintf($this->template, $tableName, $fieldName);
217  }
218  }
219  return $sql;
220  }
221 
227  public function ‪applyTcaForPreRegisteredTables()
228  {
230  foreach ($this->registry as $tableName => ‪$fields) {
231  foreach (‪$fields as $fieldName => $_) {
232  $this->‪applyTcaForTableAndField($tableName, $fieldName);
233  }
234  }
235  }
236 
243  protected function ‪applyTcaForTableAndField($tableName, $fieldName)
244  {
245  $this->‪addTcaColumn($tableName, $fieldName, $this->registry[$tableName][$fieldName]);
246  $this->‪addToAllTCAtypes($tableName, $fieldName, $this->registry[$tableName][$fieldName]);
247  }
248 
252  protected function ‪registerDefaultCategorizedTables()
253  {
254  $defaultCategorizedTables = GeneralUtility::trimExplode(
255  ',',
256  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['defaultCategorizedTables'],
257  true
258  );
259  foreach ($defaultCategorizedTables as $defaultCategorizedTable) {
260  if (!$this->‪isRegistered($defaultCategorizedTable)) {
261  $this->‪add('core', $defaultCategorizedTable, 'categories');
262  }
263  }
264  }
265 
276  protected function ‪addToAllTCAtypes($tableName, $fieldName, array $options)
277  {
278 
279  // Makes sure to add more TCA to an existing structure
280  if (isset(‪$GLOBALS['TCA'][$tableName]['columns'])) {
281  if (empty($options['fieldList'])) {
282  $fieldList = $this->‪addCategoryTab($tableName, $fieldName);
283  } else {
284  $fieldList = $options['fieldList'];
285  }
286 
287  $typesList = '';
288  if (isset($options['typesList']) && $options['typesList'] !== '') {
289  $typesList = $options['typesList'];
290  }
291 
292  $position = '';
293  if (!empty($options['position'])) {
294  $position = $options['position'];
295  }
296 
297  // Makes the new "categories" field to be visible in TSFE.
298  ‪ExtensionManagementUtility::addToAllTCAtypes($tableName, $fieldList, $typesList, $position);
299  }
300  }
301 
310  protected function ‪addCategoryTab($tableName, $fieldName)
311  {
312  $fieldList = '';
313  if (!isset($this->addedCategoryTabs[$tableName])) {
314  $fieldList .= '--div--;LLL:EXT:core/Resources/Private/Language/locallang_tca.xlf:sys_category.tabs.category, ';
315  $this->addedCategoryTabs[$tableName] = $tableName;
316  }
317  $fieldList .= $fieldName;
318  return $fieldList;
319  }
320 
333  protected function ‪addTcaColumn($tableName, $fieldName, array $options)
334  {
335  // Makes sure to add more TCA to an existing structure
336  if (isset(‪$GLOBALS['TCA'][$tableName]['columns'])) {
337  // Take specific label into account
338  $label = 'LLL:EXT:core/Resources/Private/Language/locallang_tca.xlf:sys_category.categories';
339  if (!empty($options['label'])) {
340  $label = $options['label'];
341  }
342 
343  // Take specific value of exclude flag into account
344  $exclude = true;
345  if (isset($options['exclude'])) {
346  $exclude = (bool)$options['exclude'];
347  }
348 
349  $fieldConfiguration = empty($options['fieldConfiguration']) ? [] : $options['fieldConfiguration'];
350 
351  $columns = [
352  $fieldName => [
353  'exclude' => $exclude,
354  'label' => $label,
355  'config' => static::getTcaFieldConfiguration($tableName, $fieldName, $fieldConfiguration),
356  ],
357  ];
358 
359  if (isset($options['l10n_mode'])) {
360  $columns[$fieldName]['l10n_mode'] = $options['l10n_mode'];
361  }
362  if (isset($options['l10n_display'])) {
363  $columns[$fieldName]['l10n_display'] = $options['l10n_display'];
364  }
365  if (isset($options['displayCond'])) {
366  $columns[$fieldName]['displayCond'] = $options['displayCond'];
367  }
368  if (isset($options['onChange'])) {
369  $columns[$fieldName]['onChange'] = $options['onChange'];
370  }
371 
372  // Register opposite references for the foreign side of a relation
373  if (empty(‪$GLOBALS['TCA']['sys_category']['columns']['items']['config']['MM_oppositeUsage'][$tableName])) {
374  ‪$GLOBALS['TCA']['sys_category']['columns']['items']['config']['MM_oppositeUsage'][$tableName] = [];
375  }
376  if (!in_array($fieldName, ‪$GLOBALS['TCA']['sys_category']['columns']['items']['config']['MM_oppositeUsage'][$tableName])) {
377  ‪$GLOBALS['TCA']['sys_category']['columns']['items']['config']['MM_oppositeUsage'][$tableName][] = $fieldName;
378  }
379 
380  // Add field to interface list per default (unless the 'interface' property is FALSE)
381  if (
382  (!isset($options['interface']) || $options['interface'])
383  && !empty(‪$GLOBALS['TCA'][$tableName]['interface']['showRecordFieldList'])
384  && !GeneralUtility::inList(‪$GLOBALS['TCA'][$tableName]['interface']['showRecordFieldList'], $fieldName)
385  ) {
386  ‪$GLOBALS['TCA'][$tableName]['interface']['showRecordFieldList'] .= ',' . $fieldName;
387  }
388 
389  // Adding fields to an existing table definition
390  ‪ExtensionManagementUtility::addTCAcolumns($tableName, $columns);
391  }
392  }
393 
404  public static function ‪getTcaFieldConfiguration($tableName, $fieldName = 'categories', array $fieldConfigurationOverride = [])
405  {
406  // Forges a new field, default name is "categories"
407  $fieldConfiguration = [
408  'type' => 'select',
409  'renderType' => 'selectTree',
410  'foreign_table' => 'sys_category',
411  'foreign_table_where' => ' AND sys_category.sys_language_uid IN (-1, 0) ORDER BY sys_category.sorting ASC',
412  'MM' => 'sys_category_record_mm',
413  'MM_opposite_field' => 'items',
414  'MM_match_fields' => [
415  'tablenames' => $tableName,
416  'fieldname' => $fieldName,
417  ],
418  'size' => 20,
419  'maxitems' => 9999,
420  'treeConfig' => [
421  'parentField' => 'parent',
422  'appearance' => [
423  'expandAll' => true,
424  'showHeader' => true,
425  'maxLevels' => 99,
426  ],
427  ],
428  ];
429 
430  // Merge changes to TCA configuration
431  if (!empty($fieldConfigurationOverride)) {
433  $fieldConfiguration,
434  $fieldConfigurationOverride
435  );
436  }
437 
438  return $fieldConfiguration;
439  }
440 
449  public function ‪addCategoryDatabaseSchemaToTablesDefinition(array $sqlString)
450  {
452  $sqlString[] = $this->‪getDatabaseTableDefinitions();
453  return ['sqlString' => $sqlString];
454  }
455 
459  protected function ‪getLanguageService()
460  {
461  return ‪$GLOBALS['LANG'];
462  }
463 
470  protected function remove($tableName, $fieldName)
471  {
472  if (!$this->‪isRegistered($tableName, $fieldName)) {
473  return;
474  }
475 
476  unset($this->registry[$tableName][$fieldName]);
477 
478  foreach ($this->extensions as $extensionKey => $tableFieldConfig) {
479  foreach ($tableFieldConfig as $extTableName => $fieldNameArray) {
480  if ($extTableName === $tableName && isset($fieldNameArray[$fieldName])) {
481  unset($this->extensions[$extensionKey][$tableName][$fieldName]);
482  break;
483  }
484  }
485  }
486 
487  // If no more fields are configured we unregister the categories tab.
488  if (empty($this->registry[$tableName]) && isset($this->addedCategoryTabs[$tableName])) {
489  unset($this->addedCategoryTabs[$tableName]);
490  }
491  }
492 }
‪TYPO3\CMS\Core\Category\CategoryRegistry\applyTcaForTableAndField
‪applyTcaForTableAndField($tableName, $fieldName)
Definition: CategoryRegistry.php:239
‪TYPO3\CMS\Core\Category\CategoryRegistry\$registry
‪array $registry
Definition: CategoryRegistry.php:30
‪TYPO3\CMS\Core\Category\CategoryRegistry\registerDefaultCategorizedTables
‪registerDefaultCategorizedTables()
Definition: CategoryRegistry.php:248
‪TYPO3\CMS\Core\Category\CategoryRegistry\isRegistered
‪bool isRegistered($tableName, $fieldName='categories')
Definition: CategoryRegistry.php:176
‪TYPO3\CMS\Core\Category\CategoryRegistry\getTcaFieldConfiguration
‪static array getTcaFieldConfiguration($tableName, $fieldName='categories', array $fieldConfigurationOverride=[])
Definition: CategoryRegistry.php:400
‪TYPO3\CMS\Core\Category\CategoryRegistry\addCategoryTab
‪string addCategoryTab($tableName, $fieldName)
Definition: CategoryRegistry.php:306
‪TYPO3\CMS\Core\Category\CategoryRegistry\$addedCategoryTabs
‪array $addedCategoryTabs
Definition: CategoryRegistry.php:38
‪TYPO3\CMS\Core\Category\CategoryRegistry\addCategoryDatabaseSchemaToTablesDefinition
‪array addCategoryDatabaseSchemaToTablesDefinition(array $sqlString)
Definition: CategoryRegistry.php:445
‪TYPO3\CMS\Core\Utility\ExtensionManagementUtility\addToAllTCAtypes
‪static addToAllTCAtypes($table, $newFieldsString, $typeList='', $position='')
Definition: ExtensionManagementUtility.php:281
‪TYPO3\CMS\Core\Utility\ExtensionManagementUtility\addTCAcolumns
‪static addTCAcolumns($table, $columnArray)
Definition: ExtensionManagementUtility.php:260
‪TYPO3\CMS\Core\Utility\ArrayUtility\mergeRecursiveWithOverrule
‪static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)
Definition: ArrayUtility.php:614
‪TYPO3\CMS\Core\Localization\LanguageService\sL
‪string sL($input)
Definition: LanguageService.php:158
‪$fields
‪$fields
Definition: pages.php:4
‪TYPO3\CMS\Core\Category\CategoryRegistry\$template
‪string $template
Definition: CategoryRegistry.php:42
‪TYPO3\CMS\Core\Utility\ExtensionManagementUtility
Definition: ExtensionManagementUtility.php:36
‪TYPO3\CMS\Core\Category\CategoryRegistry\getInstance
‪static CategoryRegistry getInstance()
Definition: CategoryRegistry.php:50
‪TYPO3\CMS\Core\Category\CategoryRegistry
Definition: CategoryRegistry.php:27
‪TYPO3\CMS\Core\Category
Definition: CategoryRegistry.php:2
‪TYPO3\CMS\Core\Category\CategoryRegistry\addToAllTCAtypes
‪addToAllTCAtypes($tableName, $fieldName, array $options)
Definition: CategoryRegistry.php:272
‪TYPO3\CMS\Core\Category\CategoryRegistry\getDatabaseTableDefinition
‪string getDatabaseTableDefinition($extensionKey)
Definition: CategoryRegistry.php:203
‪TYPO3\CMS\Core\Category\CategoryRegistry\getLanguageService
‪LanguageService getLanguageService()
Definition: CategoryRegistry.php:455
‪TYPO3\CMS\Core\Category\CategoryRegistry\getDatabaseTableDefinitions
‪string getDatabaseTableDefinitions()
Definition: CategoryRegistry.php:187
‪TYPO3\CMS\Core\Category\CategoryRegistry\getCategorizedTables
‪array getCategorizedTables()
Definition: CategoryRegistry.php:127
‪TYPO3\CMS\Core\Category\CategoryRegistry\getExtensionKeys
‪array getExtensionKeys()
Definition: CategoryRegistry.php:116
‪TYPO3\CMS\Core\Category\CategoryRegistry\add
‪bool add($extensionKey, $tableName, $fieldName='categories', array $options=[], $override=false)
Definition: CategoryRegistry.php:83
‪TYPO3\CMS\Core\Category\CategoryRegistry\getCategoryFieldsForTable
‪getCategoryFieldsForTable(array &$configuration)
Definition: CategoryRegistry.php:140
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:23
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪$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:329
‪TYPO3\CMS\Core\Category\CategoryRegistry\applyTcaForPreRegisteredTables
‪applyTcaForPreRegisteredTables()
Definition: CategoryRegistry.php:223
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:29
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Category\CategoryRegistry\__construct
‪__construct()
Definition: CategoryRegistry.php:58
‪TYPO3\CMS\Core\Category\CategoryRegistry\$extensions
‪array $extensions
Definition: CategoryRegistry.php:34