‪TYPO3CMS  10.4
FileCollectionRegistry.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 
20 
25 {
31  protected ‪$types = [];
32 
36  public function ‪__construct()
37  {
38  foreach (‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['fal']['registeredCollections'] as $type => $class) {
39  $this->‪registerFileCollectionClass($class, $type);
40  }
41  }
42 
52  public function ‪registerFileCollectionClass($className, $type, $override = false)
53  {
54  if (strlen($type) > 30) {
55  throw new \InvalidArgumentException('FileCollection type can have a max string length of 30 bytes', 1391295611);
56  }
57 
58  if (!class_exists($className)) {
59  throw new \InvalidArgumentException('Class ' . $className . ' does not exist.', 1391295613);
60  }
61 
62  if (!in_array(AbstractFileCollection::class, class_parents($className), true)) {
63  throw new \InvalidArgumentException('FileCollection ' . $className . ' needs to extend the AbstractFileCollection.', 1391295633);
64  }
65 
66  if (isset($this->types[$type])) {
67  // Return immediately without changing configuration
68  if ($this->types[$type] === $className) {
69  return true;
70  }
71  if (!$override) {
72  throw new \InvalidArgumentException('FileCollections ' . $type . ' is already registered.', 1391295643);
73  }
74  }
75 
76  $this->types[$type] = $className;
77  return true;
78  }
79 
89  public function ‪addTypeToTCA($type, $label, $availableFields, array $additionalColumns = [])
90  {
91  ‪$GLOBALS['TCA']['sys_file_collection']['types'][$type] = [
92  'showitem' => 'sys_language_uid, l10n_parent, l10n_diffsource, title, --palette--;;1, type, ' . $availableFields
93  ];
94 
95  // search for existing type when found override label
96  $typeFound = false;
97  foreach (‪$GLOBALS['TCA']['sys_file_collection']['columns']['type']['config']['items'] as $key => $item) {
98  if ($item[1] === $type) {
99  $typeFound = true;
100  ‪$GLOBALS['TCA']['sys_file_collection']['columns']['type']['config']['items'][$key][0] = $label;
101  }
102  }
103  if (!$typeFound) {
104  ‪$GLOBALS['TCA']['sys_file_collection']['columns']['type']['config']['items'][] = [
105  0 => $label,
106  1 => $type
107  ];
108  }
109  if ($additionalColumns !== []) {
110  ‪ArrayUtility::mergeRecursiveWithOverrule(‪$GLOBALS['TCA']['sys_file_collection']['columns'], $additionalColumns);
111  }
112  return ‪$GLOBALS['TCA']['sys_file_collection'];
113  }
114 
122  public function ‪getFileCollectionClass($type)
123  {
124  if (!isset($this->types[$type])) {
125  throw new \InvalidArgumentException('Desired FileCollection type "' . $type . '" is not in the list of available FileCollections.', 1391295644);
126  }
127  return $this->types[$type];
128  }
129 
136  public function ‪fileCollectionTypeExists($type)
137  {
138  return isset($this->types[$type]);
139  }
140 }
‪TYPO3\CMS\Core\Resource\Collection
Definition: AbstractFileCollection.php:16
‪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\Resource\Collection\FileCollectionRegistry\addTypeToTCA
‪array addTypeToTCA($type, $label, $availableFields, array $additionalColumns=[])
Definition: FileCollectionRegistry.php:88
‪TYPO3\CMS\Core\Resource\Collection\FileCollectionRegistry\$types
‪array $types
Definition: FileCollectionRegistry.php:30
‪TYPO3\CMS\Core\Resource\Collection\FileCollectionRegistry
Definition: FileCollectionRegistry.php:25
‪TYPO3\CMS\Core\Resource\Collection\FileCollectionRegistry\getFileCollectionClass
‪string getFileCollectionClass($type)
Definition: FileCollectionRegistry.php:121
‪TYPO3\CMS\Core\Resource\Collection\FileCollectionRegistry\registerFileCollectionClass
‪bool registerFileCollectionClass($className, $type, $override=false)
Definition: FileCollectionRegistry.php:51
‪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\Resource\Collection\FileCollectionRegistry\fileCollectionTypeExists
‪bool fileCollectionTypeExists($type)
Definition: FileCollectionRegistry.php:135
‪TYPO3\CMS\Core\Resource\Collection\FileCollectionRegistry\__construct
‪__construct()
Definition: FileCollectionRegistry.php:35