Sidebar Plugins
Fred supports adding functionality to the Sidebar by adding Sidebar plugins.
Plugins are distributed as MODX Transport Packages, which can be submitted to the MODX Extras repository or uploaded manually from the Installer inside the Manager. You can learn more about how to build Transport Packages in the MODX Documentation, or use a tool like Git Package Management to help create Transport Packages.
Init function
To initialise your plugin start by creating an init
function that will be called by Fred. An init
function takes three arguments:
fred
– a reference to the main Fred classSidebarPlugin
– the SidebarPlugin class that your plugin has to extendpluginTools
– a set of tools you can use in your Plugin to create content and save data. View the source code on Github for a list of available classes, instances and functions which can be used, including:valueParser
– parses Template Variables (such as{{theme_dir}}
) and replaces them with the correct value based on the given parametersui
– a set of UI elements and inputs to use, includingemitter
– emits or listen for eventsModal
– a class to create a modal windowfetch
– use to make XHR requestsfredConfig
– an instance offredConfig
utilitySidebar
– creates a sidebar, like the one used for Element settingsactions
- predefined Fred XHR requestsMousetrap
- library for keyboard shortcuts
The init
function must return a class that extends the SidebarPlugin.
Example
This will create an additional sidebar icon before the More
item in the sidebar with the same icon as the More item, three dots.
Icons & Menus
Sidebar icons are dt
HTML elements with specific classes. For example, the Settings button from the Sidebar is marked up as follows:
The CSS class fred--sidebar_page_settings
determines the appearance of the button. To style a new toolbar icon, you need to target the psuedo element ::before
in your plugin’s CSS with inline SVG code for a background image:
Any custom CSS file for your plugin including a custom icon like the above, and other styles needed for the plugin, can be included the same way as the JavaScript file in Register your Plugin step, below.
Fred’s default icons are the SVG versions of Font Awesome 5 icons. You can download the SVG of any icon from its detailed page.
Menus
If your plugin needs a menu similar to the Elements or Settings, they are defined inside the dd
HTML elements. For example, the Settings menu begins as follows:
Sidebar Plugin Order
The buttons registered to the sidebar are always added before the More
button. If there are multiple Sidebar Plugins registered, they will render in the order of the MODX Plugin’s rank in the MODX Manager.
Register your Plugin
When you have the init
function returning your plugin's class, you need to register it for Fred by creating a MODX Plugin on the [FredBeforeRender](/developer/modx_events#fredbeforerender)
event.
Include the JS file containing the init function using includes and registering the Plugin using beforeRender
.
To register the toolbar Plugin, you call the registerSidebarPlugin
function from Fred with two arguments:
name
- a unique name for your plugin. Fred cannot register multiple Plugins with the same name.init function
- theTestSidebarPluginInit
function we created inInit function
step, above
Example
The Plugin class
The sample Class in the Init function
step above can do much more than just show a link to Help page. In fact, much of Fred's functionality is already coded as Plugins. To review the current Sidebar Plugins for a sense of how to create your own, review the source code on Github.
Custom Data
Your Plugin can save and load custom data when the page is saved. Be aware, though, that custom data is only saved when a user saves the entire page.
To save your plugin's data call pluginTools.fredConfig.setPluginsData('Namespace', 'VariableName', 'Data')
. This function takes three arguments:
namespace
- for most cases, use the Plugin's name or something unique to prevent data from being overwritten by another Pluginname
- the name of the variable where you want to save the datavalue
- the actual data to store
To load data, call pluginTools.fredConfig.getPluginsData('Namespace', 'VariableName')
which takes two arguments:
namespace
- the same namespace used when callingsetPluginsData
name
- the same name used when callingsetPluginsData