|
2 | 2 |
|
3 | 3 | namespace BarrelStrength\Sprout\core\modules; |
4 | 4 |
|
| 5 | +use BarrelStrength\Sprout\core\db\SproutPluginMigrationInterface; |
5 | 6 | use BarrelStrength\Sprout\core\Sprout; |
| 7 | +use Craft; |
| 8 | +use Illuminate\Support\Collection; |
6 | 9 |
|
7 | 10 | class CpNavHelper |
8 | 11 | { |
9 | 12 | /** |
10 | 13 | * Updates Craft's CP sidebar navigation to include nav items for Sprout modules |
| 14 | + * |
| 15 | + * Add $hasCpSection to any Sprout plugin that uses modules that should be added to the CP sidebar navigation. |
11 | 16 | */ |
12 | 17 | public static function getUpdatedCpNavItems(array $cpNavItems): array |
13 | 18 | { |
14 | | - $beforePluginNavItemKeys = [ |
15 | | - 'dashboard', |
16 | | - 'entries', |
17 | | - 'globals', |
18 | | - 'categories', |
19 | | - 'assets', |
20 | | - 'users', |
21 | | - ]; |
22 | | - |
23 | | - $afterPluginNavItemKeys = [ |
24 | | - 'graphql', |
25 | | - 'utilities', |
26 | | - 'settings', |
27 | | - 'plugin-store', |
28 | | - ]; |
29 | | - |
30 | | - $newCpNavItems = []; |
31 | | - $afterCpNavItems = []; |
32 | | - $otherCpNavItems = []; |
33 | | - |
34 | | - // Break out the current nav into multiple arrays that we can re-assemble later |
35 | | - // 1. Craft defaults at the top of the nav |
36 | | - // 2. Plugins and stuff |
37 | | - // 3. Craft defaults and settings at bottom of nav |
38 | | - foreach ($cpNavItems as $cpNavItem) { |
39 | | - switch (true) { |
40 | | - case in_array($cpNavItem['url'], $beforePluginNavItemKeys, true): |
41 | | - $newCpNavItems[] = $cpNavItem; |
42 | | - break; |
43 | | - |
44 | | - case in_array($cpNavItem['url'], $afterPluginNavItemKeys, true): |
45 | | - $afterCpNavItems[] = $cpNavItem; |
46 | | - break; |
47 | | - default: |
48 | | - $otherCpNavItems[] = $cpNavItem; |
49 | | - break; |
| 19 | + $plugins = Craft::$app->getPlugins()->getAllPlugins(); |
| 20 | + |
| 21 | + $pluginsWithCpSections = array_filter($plugins, static function($plugin) { |
| 22 | + return $plugin->hasCpSection; |
| 23 | + }); |
| 24 | + |
| 25 | + // We have to enable $hasCpSection in each plugin then remove the default output just in case no other plugins with CP sections are installed |
| 26 | + $cpNavSproutPluginNavKeys = array_keys(array_filter($pluginsWithCpSections, static function($plugin) { |
| 27 | + return $plugin instanceof SproutPluginMigrationInterface; |
| 28 | + })); |
| 29 | + |
| 30 | + // get the nav items of the plugins with cp sections from the $cpNavItems based on the $pluginsWithCpSections matching the url to the plugin handle |
| 31 | + $cpNavOldPluginNavItems = array_filter($cpNavItems, static function($navItem) use ($pluginsWithCpSections) { |
| 32 | + foreach ($pluginsWithCpSections as $plugin) { |
| 33 | + if ($navItem['url'] === $plugin->handle) { |
| 34 | + return true; |
| 35 | + } |
50 | 36 | } |
51 | | - } |
| 37 | + |
| 38 | + return false; |
| 39 | + }); |
| 40 | + |
| 41 | + $cpNavSproutModuleNavItems = []; |
52 | 42 |
|
53 | 43 | $sproutNavGroupsInfo = Sprout::getInstance()->coreSettings->getCraftCpSidebarNavItems(); |
54 | 44 | $sproutNavGroups = []; |
@@ -100,31 +90,55 @@ public static function getUpdatedCpNavItems(array $cpNavItems): array |
100 | 90 | unset($sproutNavGroup['subnav']); |
101 | 91 | } |
102 | 92 |
|
103 | | - $otherCpNavItems[] = $sproutNavGroup; |
| 93 | + $cpNavSproutModuleNavItems[] = $sproutNavGroup; |
104 | 94 | } |
105 | 95 |
|
106 | | - // Sort custom nav items alphabetically by label |
107 | | - uasort($otherCpNavItems, static fn($a, $b) => $a['label'] <=> $b['label']); |
| 96 | + $cpNavNewPluginNavItems = array_merge($cpNavOldPluginNavItems, $cpNavSproutModuleNavItems); |
108 | 97 |
|
109 | | - // Add the custom nav items back to the nav |
110 | | - foreach ($otherCpNavItems as $otherCpNavItem) { |
111 | | - $newCpNavItems[] = $otherCpNavItem; |
112 | | - } |
| 98 | + $cpNavNewPluginNavItems = array_filter($cpNavNewPluginNavItems, static function($navItem) use ($cpNavSproutPluginNavKeys) { |
| 99 | + foreach ($cpNavSproutPluginNavKeys as $sproutNavKey) { |
| 100 | + if ($navItem['url'] === $sproutNavKey) { |
| 101 | + return false; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + return true; |
| 106 | + }); |
113 | 107 |
|
114 | | - // Add the Craft defaults back to the bottom of the nav |
115 | | - foreach ($afterCpNavItems as $afterCpNavItem) { |
116 | | - $newCpNavItems[] = $afterCpNavItem; |
| 108 | + uasort($cpNavNewPluginNavItems, static fn($a, $b) => $a['label'] <=> $b['label']); |
| 109 | + |
| 110 | + // Remove all the Sprout Plugin hasCpSection nav items |
| 111 | + $newCpNavItems = array_filter($cpNavItems, static function($navItem) use ($pluginsWithCpSections) { |
| 112 | + foreach ($pluginsWithCpSections as $plugin) { |
| 113 | + if ($navItem['url'] === $plugin->handle) { |
| 114 | + return false; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + return true; |
| 119 | + }); |
| 120 | + |
| 121 | + // If no other plugins are installed and no modules are enabled, this will be the Sprout plugin and just get removed later |
| 122 | + $cpNavFirstPluginItemKey = array_key_first($pluginsWithCpSections); |
| 123 | + $cpNavFirstPluginItemIndex = Collection::make($cpNavItems)->search(fn(array $item) => $item['url'] === $cpNavFirstPluginItemKey); |
| 124 | + |
| 125 | + // If we don't find any plugins with CP sections (a Sprout Plugin should always be there), we'll just add the Sprout Modules to the end of the nav |
| 126 | + if ($cpNavFirstPluginItemIndex === false) { |
| 127 | + $cpNavFirstPluginItemIndex = count($newCpNavItems); |
117 | 128 | } |
118 | 129 |
|
| 130 | + // Insert the Sprout Module nav items |
| 131 | + array_splice($newCpNavItems, $cpNavFirstPluginItemIndex, 0, $cpNavNewPluginNavItems); |
| 132 | + |
119 | 133 | return $newCpNavItems; |
120 | 134 | } |
121 | 135 |
|
122 | 136 | /** |
123 | 137 | * Adds nav items for a give module to the Sprout settings sidebar navigation |
124 | 138 | */ |
125 | 139 | public static function mergeSproutCpSettingsNavItems( |
126 | | - array $oldNavItems, |
127 | | - array $newNavItems, |
| 140 | + array $oldNavItems, |
| 141 | + array $newNavItems, |
128 | 142 | string $groupName, |
129 | 143 | ): array { |
130 | 144 | $navItems = $oldNavItems; |
|
0 commit comments