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