mirror of
				https://github.com/strapi/strapi.git
				synced 2025-10-30 17:37:26 +00:00 
			
		
		
		
	Add option to add a link into the global setting section
This commit is contained in:
		
							parent
							
								
									b2572f9016
								
							
						
					
					
						commit
						09d1a6af3a
					
				| @ -191,3 +191,85 @@ export default strapi => { | |||||||
|   return strapi.registerPlugin(plugin); |   return strapi.registerPlugin(plugin); | ||||||
| }; | }; | ||||||
| ``` | ``` | ||||||
|  | 
 | ||||||
|  | ## Adding a setting into the global section | ||||||
|  | 
 | ||||||
|  | In order to add a link into the global section of the settings view you need to create a global array containing the links you want to add; | ||||||
|  | 
 | ||||||
|  | **Path —** `plugins/my-plugin/admin/src/index.js`. | ||||||
|  | 
 | ||||||
|  | import pluginPkg from '../../package.json'; | ||||||
|  | // Import the component | ||||||
|  | import Settings from './containers/Settings'; | ||||||
|  | import SettingLink from './components/SettingLink'; | ||||||
|  | import pluginId from './pluginId'; | ||||||
|  | 
 | ||||||
|  | export default strapi => { | ||||||
|  | const pluginDescription = | ||||||
|  | pluginPkg.strapi.description || pluginPkg.description; | ||||||
|  | 
 | ||||||
|  | // Declare the links that will be injected into the settings menu | ||||||
|  | const menuSection = { | ||||||
|  | id: pluginId, | ||||||
|  | title: { | ||||||
|  | id: `${pluginId}.foo`, | ||||||
|  | defaultMessage: 'Super cool setting', | ||||||
|  | }, | ||||||
|  | links: [ | ||||||
|  | { | ||||||
|  | title: 'Setting page 1', | ||||||
|  | to: `${strapi.settingsBaseURL}/${pluginId}/setting1`, | ||||||
|  | name: 'setting1', | ||||||
|  | }, | ||||||
|  | { | ||||||
|  | title: { | ||||||
|  | id: `${pluginId}.bar`, | ||||||
|  | defaultMessage: 'Setting page 2', | ||||||
|  | }, | ||||||
|  | to: `${strapi.settingsBaseURL}/${pluginId}/setting2`, | ||||||
|  | name: 'setting2', | ||||||
|  | }, | ||||||
|  | ], | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | const plugin = { | ||||||
|  | blockerComponent: null, | ||||||
|  | blockerComponentProps: {}, | ||||||
|  | description: pluginDescription, | ||||||
|  | icon: pluginPkg.strapi.icon, | ||||||
|  | id: pluginId, | ||||||
|  | initializer: () => null, | ||||||
|  | injectedComponents: [], | ||||||
|  | isReady: true, | ||||||
|  | leftMenuLinks: [], | ||||||
|  | leftMenuSections: [], | ||||||
|  | mainComponent: null, | ||||||
|  | name: pluginPkg.strapi.name, | ||||||
|  | preventComponentRendering: false, | ||||||
|  | settings: { | ||||||
|  | // Add a link into the global section of the settings view | ||||||
|  | global: [ | ||||||
|  | { | ||||||
|  | title: 'Setting link 1', | ||||||
|  | to: `${strapi.settingsBaseURL}/setting-link-1`, | ||||||
|  | name: 'settingLink1', | ||||||
|  | Component: SettingLink, | ||||||
|  | // True or false | ||||||
|  | exact: false, | ||||||
|  | }, | ||||||
|  | ], | ||||||
|  | mainComponent: Settings, | ||||||
|  | menuSection, | ||||||
|  | }, | ||||||
|  | trads: {}, | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | return strapi.registerPlugin(plugin); | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | ``` | ||||||
|  | 
 | ||||||
|  | ::: danger | ||||||
|  | It is currently not possible to add a link into another plugin's setting section | ||||||
|  | ::: | ||||||
|  | ``` | ||||||
|  | |||||||
| @ -6,29 +6,30 @@ | |||||||
| 
 | 
 | ||||||
| import React, { memo } from 'react'; | import React, { memo } from 'react'; | ||||||
| import { useGlobalContext, LeftMenu, LeftMenuList } from 'strapi-helper-plugin'; | import { useGlobalContext, LeftMenu, LeftMenuList } from 'strapi-helper-plugin'; | ||||||
| import { get } from 'lodash'; |  | ||||||
| import { Switch, Redirect, Route, useParams } from 'react-router-dom'; | import { Switch, Redirect, Route, useParams } from 'react-router-dom'; | ||||||
| 
 |  | ||||||
| import EditView from '../Webhooks/EditView'; | import EditView from '../Webhooks/EditView'; | ||||||
| import ListView from '../Webhooks/ListView'; | import ListView from '../Webhooks/ListView'; | ||||||
| import SettingDispatcher from './SettingDispatcher'; | import SettingDispatcher from './SettingDispatcher'; | ||||||
| import Wrapper from './Wrapper'; | import Wrapper from './Wrapper'; | ||||||
|  | import retrieveGlobalLinks from './utils/retrieveGlobalLinks'; | ||||||
|  | import retrievePluginsMenu from './utils/retrievePluginsMenu'; | ||||||
| 
 | 
 | ||||||
| function SettingsPage() { | function SettingsPage() { | ||||||
|   const { settingId } = useParams(); |   const { settingId } = useParams(); | ||||||
|   const { formatMessage, plugins, settingsBaseURL } = useGlobalContext(); |   const { formatMessage, plugins, settingsBaseURL } = useGlobalContext(); | ||||||
|  |   // Retrieve the links that will be injected into the global section
 | ||||||
|  |   const globalLinks = retrieveGlobalLinks(plugins); | ||||||
|  |   // Create the plugins settings section
 | ||||||
|  |   // Note it is currently not possible to add a link into a plugin section
 | ||||||
|  |   const pluginsMenu = retrievePluginsMenu(plugins); | ||||||
| 
 | 
 | ||||||
|   const pluginsMenu = Object.keys(plugins).reduce((acc, current) => { |   const createdRoutes = globalLinks | ||||||
|     const pluginMenu = get(plugins, [current, 'settings', 'menuSection'], null); |     .map(({ to, Component, exact }) => ( | ||||||
| 
 |       <Route path={to} key={to} component={Component} exact={exact || false} /> | ||||||
|     if (!pluginMenu) { |     )) | ||||||
|       return acc; |     .filter((route, index, refArray) => { | ||||||
|     } |       return refArray.findIndex(obj => obj.key === route.key) === index; | ||||||
| 
 |     }); | ||||||
|     acc.push(pluginMenu); |  | ||||||
| 
 |  | ||||||
|     return acc; |  | ||||||
|   }, []); |  | ||||||
| 
 | 
 | ||||||
|   const menuItems = [ |   const menuItems = [ | ||||||
|     { |     { | ||||||
| @ -40,6 +41,7 @@ function SettingsPage() { | |||||||
|           to: `${settingsBaseURL}/webhooks`, |           to: `${settingsBaseURL}/webhooks`, | ||||||
|           name: 'webhooks', |           name: 'webhooks', | ||||||
|         }, |         }, | ||||||
|  |         ...globalLinks, | ||||||
|       ], |       ], | ||||||
|     }, |     }, | ||||||
|     ...pluginsMenu, |     ...pluginsMenu, | ||||||
| @ -74,6 +76,7 @@ function SettingsPage() { | |||||||
|               path={`${settingsBaseURL}/webhooks/:id`} |               path={`${settingsBaseURL}/webhooks/:id`} | ||||||
|               component={EditView} |               component={EditView} | ||||||
|             /> |             /> | ||||||
|  |             {createdRoutes} | ||||||
|             <Route |             <Route | ||||||
|               path={`${settingsBaseURL}/:pluginId`} |               path={`${settingsBaseURL}/:pluginId`} | ||||||
|               component={SettingDispatcher} |               component={SettingDispatcher} | ||||||
|  | |||||||
| @ -0,0 +1,17 @@ | |||||||
|  | import { get } from 'lodash'; | ||||||
|  | 
 | ||||||
|  | const retrieveGlobalLinks = pluginsObj => { | ||||||
|  |   return Object.values(pluginsObj).reduce((acc, current) => { | ||||||
|  |     const links = get(current, ['settings', 'global'], null); | ||||||
|  | 
 | ||||||
|  |     if (links) { | ||||||
|  |       for (let i = 0; i < links.length; i++) { | ||||||
|  |         acc.push(links[i]); | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     return acc; | ||||||
|  |   }, []); | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | export default retrieveGlobalLinks; | ||||||
| @ -0,0 +1,17 @@ | |||||||
|  | import { get } from 'lodash'; | ||||||
|  | 
 | ||||||
|  | const retrievePluginsMenu = pluginsObj => { | ||||||
|  |   return Object.values(pluginsObj).reduce((acc, current) => { | ||||||
|  |     const pluginMenu = get(current, ['settings', 'menuSection'], null); | ||||||
|  | 
 | ||||||
|  |     if (!pluginMenu) { | ||||||
|  |       return acc; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     acc.push(pluginMenu); | ||||||
|  | 
 | ||||||
|  |     return acc; | ||||||
|  |   }, []); | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | export default retrievePluginsMenu; | ||||||
| @ -0,0 +1,33 @@ | |||||||
|  | import retrieveGlobalLinks from '../retrieveGlobalLinks'; | ||||||
|  | 
 | ||||||
|  | describe('ADMIN | containers | SettingsPage | utils', () => { | ||||||
|  |   describe('retrieveGlobalLinks', () => { | ||||||
|  |     it('should return an empty array if there is no plugins', () => { | ||||||
|  |       expect(retrieveGlobalLinks({})).toHaveLength(0); | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     it('should return an array of links', () => { | ||||||
|  |       const plugins = { | ||||||
|  |         test: { | ||||||
|  |           settings: { | ||||||
|  |             global: [], | ||||||
|  |           }, | ||||||
|  |         }, | ||||||
|  |         noSettings: {}, | ||||||
|  |         foo: { | ||||||
|  |           settings: { | ||||||
|  |             global: ['test'], | ||||||
|  |           }, | ||||||
|  |         }, | ||||||
|  |         bar: { | ||||||
|  |           settings: { | ||||||
|  |             global: ['test2'], | ||||||
|  |           }, | ||||||
|  |         }, | ||||||
|  |       }; | ||||||
|  |       const expected = ['test', 'test2']; | ||||||
|  | 
 | ||||||
|  |       expect(retrieveGlobalLinks(plugins)).toEqual(expected); | ||||||
|  |     }); | ||||||
|  |   }); | ||||||
|  | }); | ||||||
| @ -0,0 +1,33 @@ | |||||||
|  | import retrievePluginsMenu from '../retrievePluginsMenu'; | ||||||
|  | 
 | ||||||
|  | describe('ADMIN | containers | SettingsPage | utils', () => { | ||||||
|  |   describe('retrievePluginsMenu', () => { | ||||||
|  |     it('should return an empty array if there is no plugins', () => { | ||||||
|  |       expect(retrievePluginsMenu({})).toHaveLength(0); | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     it('should return an array of menu sections', () => { | ||||||
|  |       const plugins = { | ||||||
|  |         test: { | ||||||
|  |           settings: { | ||||||
|  |             menuSection: null, | ||||||
|  |           }, | ||||||
|  |         }, | ||||||
|  |         noSettings: {}, | ||||||
|  |         foo: { | ||||||
|  |           settings: { | ||||||
|  |             menuSection: { label: 'test' }, | ||||||
|  |           }, | ||||||
|  |         }, | ||||||
|  |         bar: { | ||||||
|  |           settings: { | ||||||
|  |             menuSection: { label: 'test2' }, | ||||||
|  |           }, | ||||||
|  |         }, | ||||||
|  |       }; | ||||||
|  |       const expected = [{ label: 'test' }, { label: 'test2' }]; | ||||||
|  | 
 | ||||||
|  |       expect(retrievePluginsMenu(plugins)).toEqual(expected); | ||||||
|  |     }); | ||||||
|  |   }); | ||||||
|  | }); | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 soupette
						soupette