[BUGFIX] Respect sorting when adding main modules 24/63724/7
authorBenni Mack <benni@typo3.org>
Sat, 14 Mar 2020 18:59:59 +0000 (19:59 +0100)
committerGeorg Ringer <georg.ringer@gmail.com>
Thu, 19 Mar 2020 06:32:15 +0000 (07:32 +0100)
When adding a new main module, the "position" is now taken into
account, allowing TYPO3 Core also to use the addModule API
everywhere.

Resolves: #47663
Releases: master
Change-Id: I733ee92eada88f09fd247fa818c018dc6bee62d6
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/63724
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Susanne Moog <look@susi.dev>
Tested-by: Georg Ringer <georg.ringer@gmail.com>
Reviewed-by: Susanne Moog <look@susi.dev>
Reviewed-by: Georg Ringer <georg.ringer@gmail.com>
typo3/sysext/core/Classes/Utility/ExtensionManagementUtility.php
typo3/sysext/core/Tests/Unit/Utility/ExtensionManagementUtilityTest.php
typo3/sysext/core/ext_tables.php

index 6999afb..d6d6c36 100644 (file)
@@ -761,6 +761,9 @@ class ExtensionManagementUtility
      */
     public static function addModule($main, $sub = '', $position = '', $path = null, $moduleConfiguration = [])
     {
+        if (!isset($GLOBALS['TBE_MODULES'])) {
+            $GLOBALS['TBE_MODULES'] = [];
+        }
         // If there is already a main module by this name:
         // Adding the submodule to the correct position:
         if (isset($GLOBALS['TBE_MODULES'][$main]) && $sub) {
@@ -788,6 +791,48 @@ class ExtensionManagementUtility
             }
             // Re-inserting the submodule list:
             $GLOBALS['TBE_MODULES'][$main] = trim($modules, ',');
+        } elseif (!isset($GLOBALS['TBE_MODULES'][$main]) && empty($sub)) {
+            // Create a new main module, respecting the order, which is only possible when the module does not exist yet
+            $conf = $GLOBALS['TBE_MODULES']['_configuration'] ?? [];
+            unset($GLOBALS['TBE_MODULES']['_configuration']);
+            $navigationComponents = $GLOBALS['TBE_MODULES']['_navigationComponents'] ?? [];
+            unset($GLOBALS['TBE_MODULES']['_navigationComponents']);
+
+            $modules = array_keys($GLOBALS['TBE_MODULES']);
+            [$place, $moduleReference] = array_pad(GeneralUtility::trimExplode(':', $position, true), 2, null);
+            if ($place === null || ($moduleReference !== null && !in_array($moduleReference, $modules, true))) {
+                $place = 'bottom';
+            }
+            $newModules = [];
+            switch (strtolower($place)) {
+                case 'after':
+                    foreach ($modules as $existingMainModule) {
+                        $newModules[$existingMainModule] = $GLOBALS['TBE_MODULES'][$existingMainModule];
+                        if ($moduleReference === $existingMainModule) {
+                            $newModules[$main] = '';
+                        }
+                    }
+                    break;
+                case 'before':
+                    foreach ($modules as $existingMainModule) {
+                        if ($moduleReference === $existingMainModule) {
+                            $newModules[$main] = '';
+                        }
+                        $newModules[$existingMainModule] = $GLOBALS['TBE_MODULES'][$existingMainModule];
+                    }
+                    break;
+                case 'top':
+                    $newModules[$main] = '';
+                    $newModules += $GLOBALS['TBE_MODULES'];
+                    break;
+                case 'bottom':
+                default:
+                    $newModules = $GLOBALS['TBE_MODULES'];
+                    $newModules[$main] = '';
+            }
+            $GLOBALS['TBE_MODULES'] = $newModules;
+            $GLOBALS['TBE_MODULES']['_configuration'] = $conf;
+            $GLOBALS['TBE_MODULES']['_navigationComponents'] = $navigationComponents;
         } else {
             // Create new main modules with only one submodule, $sub (or none if $sub is blank)
             $GLOBALS['TBE_MODULES'][$main] = $sub;
index 906e03e..e123969 100644 (file)
@@ -1322,6 +1322,30 @@ class ExtensionManagementUtilityTest extends UnitTestCase
         self::assertEquals($expected, $GLOBALS['TBE_MODULES'][$mainModule]);
     }
 
+    /**
+     * @test
+     * @dataProvider addModulePositionTestsDataProvider
+     * @param $position
+     * @param $existing
+     * @param $expected
+     */
+    public function addModuleCanAddMainModule($position, $existing, $expected)
+    {
+        $mainModule = 'newModule';
+        if ($existing) {
+            foreach (explode(',', $existing) as $existingMainModule) {
+                $GLOBALS['TBE_MODULES'][$existingMainModule] = '';
+            }
+        }
+
+        ExtensionManagementUtility::addModule($mainModule, '', $position);
+
+        self::assertTrue(isset($GLOBALS['TBE_MODULES'][$mainModule]));
+        unset($GLOBALS['TBE_MODULES']['_configuration']);
+        unset($GLOBALS['TBE_MODULES']['_navigationComponents']);
+        self::assertEquals($expected, implode(',', array_keys($GLOBALS['TBE_MODULES'])));
+    }
+
     /////////////////////////////////////////
     // Tests concerning createExtLocalconfCacheEntry
     /////////////////////////////////////////
index f018b8f..dedb88c 100644 (file)
@@ -35,55 +35,89 @@ $GLOBALS['PAGES_TYPES'] = [
  * For information about adding modules to TYPO3 you should consult the
  * documentation found in "Inside TYPO3"
  */
-$GLOBALS['TBE_MODULES'] = [
-    'web' => 'list',
-    'site' => '',
-    'file' => '',
-    'user' => '',
-    'tools' => '',
-    'system' => '',
-    'help' => '',
-    '_configuration' => [
-        'web' => [
-            'labels' => 'LLL:EXT:core/Resources/Private/Language/locallang_mod_web.xlf',
-            'name' => 'web',
-            'iconIdentifier' => 'modulegroup-web'
-        ],
-        'site' => [
-            'labels' => 'LLL:EXT:core/Resources/Private/Language/locallang_mod_site.xlf',
-            'name' => 'site',
-            'workspaces' => 'online',
-            'iconIdentifier' => 'modulegroup-site',
-        ],
-        'file' => [
-            'labels' => 'LLL:EXT:core/Resources/Private/Language/locallang_mod_file.xlf',
-            'navigationFrameModule' => 'file_navframe',
-            'name' => 'file',
-            'workspaces' => 'online,custom',
-            'iconIdentifier' => 'modulegroup-file'
-        ],
-        'user' => [
-            'labels' => 'LLL:EXT:core/Resources/Private/Language/locallang_mod_usertools.xlf',
-            'name' => 'user',
-            'iconIdentifier' => 'modulegroup-user'
-        ],
-        'tools' => [
-            'labels' => 'LLL:EXT:core/Resources/Private/Language/locallang_mod_admintools.xlf',
-            'name' => 'tools',
-            'iconIdentifier' => 'modulegroup-tools'
-        ],
-        'system' => [
-            'labels' => 'LLL:EXT:core/Resources/Private/Language/locallang_mod_system.xlf',
-            'name' => 'system',
-            'iconIdentifier' => 'modulegroup-system'
-        ],
-        'help' => [
-            'labels' => 'LLL:EXT:core/Resources/Private/Language/locallang_mod_help.xlf',
-            'name' => 'help',
-            'iconIdentifier' => 'modulegroup-help'
-        ]
+\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addModule(
+    'web',
+    '',
+    '',
+    null,
+    [
+        'labels' => 'LLL:EXT:core/Resources/Private/Language/locallang_mod_web.xlf',
+        'name' => 'web',
+        'iconIdentifier' => 'modulegroup-web'
     ]
-];
+);
+// workaround to add web->list by default
+$GLOBALS['TBE_MODULES']['web'] = 'list';
+
+\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addModule(
+    'site',
+    '',
+    '',
+    null,
+    [
+        'labels' => 'LLL:EXT:core/Resources/Private/Language/locallang_mod_site.xlf',
+        'name' => 'site',
+        'workspaces' => 'online',
+        'iconIdentifier' => 'modulegroup-site',
+    ]
+);
+\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addModule(
+    'file',
+    '',
+    '',
+    null,
+    [
+        'labels' => 'LLL:EXT:core/Resources/Private/Language/locallang_mod_file.xlf',
+        'navigationFrameModule' => 'file_navframe',
+        'name' => 'file',
+        'workspaces' => 'online,custom',
+        'iconIdentifier' => 'modulegroup-file'
+    ]
+);
+\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addModule(
+    'user',
+    '',
+    '',
+    null,
+    [
+        'labels' => 'LLL:EXT:core/Resources/Private/Language/locallang_mod_usertools.xlf',
+        'name' => 'user',
+        'iconIdentifier' => 'modulegroup-user'
+    ]
+);
+\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addModule(
+    'tools',
+    '',
+    '',
+    null,
+    [
+        'labels' => 'LLL:EXT:core/Resources/Private/Language/locallang_mod_admintools.xlf',
+        'name' => 'tools',
+        'iconIdentifier' => 'modulegroup-tools'
+    ]
+);
+\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addModule(
+    'system',
+    '',
+    '',
+    null,
+    [
+        'labels' => 'LLL:EXT:core/Resources/Private/Language/locallang_mod_system.xlf',
+        'name' => 'system',
+        'iconIdentifier' => 'modulegroup-system'
+    ]
+);
+\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addModule(
+    'help',
+    '',
+    '',
+    null,
+    [
+        'labels' => 'LLL:EXT:core/Resources/Private/Language/locallang_mod_help.xlf',
+        'name' => 'help',
+        'iconIdentifier' => 'modulegroup-help'
+    ]
+);
 
 // Register the page tree core navigation component
 \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addCoreNavigationComponent('web', 'TYPO3/CMS/Backend/PageTree/PageTreeElement');