[FEATURE] Allow multiple category fields per table
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Category / CategoryRegistry.php
index c8ef04a..3cd4f83 100644 (file)
@@ -39,6 +39,11 @@ class CategoryRegistry implements \TYPO3\CMS\Core\SingletonInterface {
         */
        protected $registry = array();
 
+       /**
+        * @var array
+        */
+       protected $addedCategoryTabs = array();
+
        /**
         * @var string
         */
@@ -70,23 +75,25 @@ class CategoryRegistry implements \TYPO3\CMS\Core\SingletonInterface {
         *              + fieldList: field configuration to be added to showitems
         *              + typesList: list of types that shall visualize the categories field
         *              + position: insert position of the categories field
+        *              + label: backend label of the categories field
         *              + fieldConfiguration: TCA field config array to override defaults
         * @return boolean
-        * @throws \RuntimeException
+        * @throws \InvalidArgumentException
         */
        public function add($extensionKey, $tableName, $fieldName = 'categories', $options = array()) {
                $result = FALSE;
 
                if ($tableName === '') {
-                       throw new \RuntimeException('TYPO3\\CMS\\Core\\Category\\CategoryRegistry No tableName given.', 1369122038);
+                       throw new \InvalidArgumentException('TYPO3\\CMS\\Core\\Category\\CategoryRegistry No tableName given.', 1369122038);
                }
 
-                       // Makes sure there is an existing table configuration and nothing registered yet:
-               if (!$this->isRegistered($tableName, $fieldName)) {
-                       $this->registry[$extensionKey][$tableName] = array (
-                               'fieldName' => $fieldName,
-                               'options' => $options,
-                       );
+               if (!is_array($options)) {
+                       throw new \InvalidArgumentException('TYPO3\\CMS\\Core\\Category\\CategoryRegistry options parameter must be an array', 1378976970);
+               }
+
+               // Makes sure there is an existing table configuration and nothing registered yet:
+               if (isset($GLOBALS['TCA'][$tableName]) && !$this->isRegistered($tableName, $fieldName)) {
+                       $this->registry[$extensionKey][$tableName][$fieldName] = $options;
                        $result = TRUE;
                }
                return $result;
@@ -95,9 +102,11 @@ class CategoryRegistry implements \TYPO3\CMS\Core\SingletonInterface {
        /**
         * Gets the registered category configurations.
         *
+        * @deprecated since 6.2 will be removed two versions later - Use ->isRegistered to get information about registered category fields.
         * @return array
         */
        public function get() {
+               \TYPO3\CMS\Core\Utility\GeneralUtility::logDeprecatedFunction();
                return $this->registry;
        }
 
@@ -135,7 +144,7 @@ class CategoryRegistry implements \TYPO3\CMS\Core\SingletonInterface {
        public function isRegistered($tableName, $fieldName = 'categories') {
                $isRegistered = FALSE;
                foreach ($this->registry as $configuration) {
-                       if (!empty($configuration[$tableName]['fieldName']) && $configuration[$tableName]['fieldName'] === $fieldName) {
+                       if (isset($configuration[$tableName][$fieldName])) {
                                $isRegistered = TRUE;
                                break;
                        }
@@ -168,8 +177,10 @@ class CategoryRegistry implements \TYPO3\CMS\Core\SingletonInterface {
                }
                $sql = '';
 
-               foreach ($this->registry[$extensionKey] as $tableName => $tableInfo) {
-                       $sql .= sprintf($this->template, $tableName, $tableInfo['fieldName']);
+               foreach ($this->registry[$extensionKey] as $tableName => $fields) {
+                       foreach (array_keys($fields) as $fieldName) {
+                               $sql .= sprintf($this->template, $tableName, $fieldName);
+                       }
                }
                return $sql;
        }
@@ -184,9 +195,11 @@ class CategoryRegistry implements \TYPO3\CMS\Core\SingletonInterface {
                $this->registerDefaultCategorizedTables();
 
                foreach ($this->registry as $registry) {
-                       foreach ($registry as $tableName => $tableInfo) {
-                               $this->addTcaColumn($tableName, $tableInfo['fieldName'], $tableInfo['options']);
-                               $this->addToAllTCAtypes($tableName, $tableInfo['fieldName'], $tableInfo['options']);
+                       foreach ($registry as $tableName => $fields) {
+                               foreach ($fields as $fieldName => $options) {
+                                       $this->addTcaColumn($tableName, $fieldName, $options);
+                                       $this->addToAllTCAtypes($tableName, $fieldName, $options);
+                               }
                        }
                }
        }
@@ -225,8 +238,9 @@ class CategoryRegistry implements \TYPO3\CMS\Core\SingletonInterface {
                // Makes sure to add more TCA to an existing structure
                if (isset($GLOBALS['TCA'][$tableName]['columns'])) {
 
-                       $fieldList = '--div--;LLL:EXT:lang/locallang_tca.xlf:sys_category.tabs.category, ' . $fieldName;
-                       if (!empty($options['fieldList'])) {
+                       if (empty($options['fieldList'])) {
+                               $fieldList = $this->addCategoryTab($tableName, $fieldName);
+                       } else {
                                $fieldList = $options['fieldList'];
                        }
 
@@ -246,6 +260,24 @@ class CategoryRegistry implements \TYPO3\CMS\Core\SingletonInterface {
                }
        }
 
+       /**
+        * Creates the 'fieldList' string for $fieldName which includes a categories tab.
+        * But only one categories tab is added per table.
+        *
+        * @param string $tableName
+        * @param string $fieldName
+        * @return string
+        */
+       protected function addCategoryTab($tableName, $fieldName) {
+               $fieldList = '';
+               if (!in_array($tableName, $this->addedCategoryTabs)) {
+                       $fieldList .= '--div--;LLL:EXT:lang/locallang_tca.xlf:sys_category.tabs.category, ';
+                       $this->addedCategoryTabs[] = $tableName;
+               }
+               $fieldList .= $fieldName;
+               return $fieldList;
+       }
+
        /**
         * Add a new TCA Column
         *
@@ -253,6 +285,7 @@ class CategoryRegistry implements \TYPO3\CMS\Core\SingletonInterface {
         * @param string $fieldName Name of the field to be used to store categories
         * @param array $options Additional configuration options
         *              + fieldConfiguration: TCA field config array to override defaults
+        *              + label: backend label of the categories field
         * @return void
         */
        protected function addTcaColumn($tableName, $fieldName, $options) {
@@ -267,7 +300,10 @@ class CategoryRegistry implements \TYPO3\CMS\Core\SingletonInterface {
                                'foreign_table_where' => ' AND sys_category.sys_language_uid IN (-1, 0) ORDER BY sys_category.title ASC',
                                'MM' => 'sys_category_record_mm',
                                'MM_opposite_field' => 'items',
-                               'MM_match_fields' => array('tablenames' => $tableName),
+                               'MM_match_fields' => array(
+                                       'tablenames' => $tableName,
+                                       'fieldname' => $fieldName,
+                               ),
                                'size' => 10,
                                'autoSizeMax' => 50,
                                'maxitems' => 9999,
@@ -311,10 +347,15 @@ class CategoryRegistry implements \TYPO3\CMS\Core\SingletonInterface {
                                );
                        }
 
+                       $label = 'LLL:EXT:lang/locallang_tca.xlf:sys_category.categories';
+                       if (!empty($options['label'])) {
+                               $label = $options['label'];
+                       }
+
                        $columns = array(
                                $fieldName => array(
                                        'exclude' => 0,
-                                       'label' => 'LLL:EXT:lang/locallang_tca.xlf:sys_category.categories',
+                                       'label' => $label,
                                        'config' => $fieldConfiguration,
                                ),
                        );