Toolbar Plugins
Fred supports adding functionality to individual Elements by registering new buttons in the toolbar above each Element.
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 classToolbarPlugin
– the ToolbarPlugin 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 ToolbarPlugin.
Example
This will create an additional toolbar icon at the end of the toolbar with the same icon as the Settings icon, a gear.
Icons
Toolbar icons are button elements with specific classes. For example, the delete button from the toolbar is marked up as follows:
The CSS class fred--trash
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, and a background color. You can optionally have a differnt background color when hovered:
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.
Toolbar Plugin Order
The buttons registered to the toolbars are always added after the built-in default buttons. If there are multiple Toolbar Plugins registered, they will render in the order of the MODX Plugin’s rank in the MODX Manager.
Limiting Plugins for Elements
By default, all Toolbar Plugins will register for every Element. To specify the order and/or omit some plugins, modify an Element’s Option Set setting to either include or exclude specific Fred Plugins with a plugins-include
or plugins-exclude
attribute.
Note: The plugins are unique names of the class created for the plugins. As a general rule, this should match the plugin name used for the MODX Package Provider.
If a plugins-include
attribute is included, it will ignore any plugins-exclude
lines. To include only specific Plugins for an Element, use a plugins-include
Options setting:
To exclude one or more specific Plugins on an Element, use a plugins-exclude
option:
To prevent all Plugins from registering on an Element completely, specify an empty array for a plugins-include
option:
Note: The plugins are unique names of the class created for the plugins. As a general rule, this should match the plugin name used for the MODX Package Provider.
If a pluginsInclude
attribute is included, it will ignore any pluginsExclude
lines. To include only specific Plugins for an Element, use a pluginsInclude
Options setting:
To exclude one or more specific Plugins on an Element, use a pluginsExclude
option:
To prevent all Plugins from registering on an Element completely, specify an empty array for a pluginsInclude
option:
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 registerToolbarPlugin
function from Fred with two arguments:
name
- a unique name for your plugin. Fred cannot register multiple Plugins with the same name.init function
- theTestToolbarPluginInit
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 logging to the console via console.log
. In fact, much of Fred's functionality is already coded as Plugins. To review the current Toolbar 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.
Element Data
Toolbar Plugins typically should affect the Elements on which they act. This data are attached to the Fred element where to toolbar action occurred. To save data, use this.el.setPluginValue('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, use this.el.getPluginValue('Namespace', 'VariableName')
which takes two arguments:
namespace
- the same namespace used when callingsetPluginValue
name
- the same name used when callingsetPluginValue
Global Data
Data assoiated with a Plugin can optionally be saved globally, not attached to a specific Element. To save data this way, call pluginTools.fredConfig.setPluginsData('Namespace', 'VariableName', 'Data')
. This function takes same arguments as this.el.setPluginValue
.
To load global data, call pluginTools.fredConfig.getPluginsData('Namespace', 'VariableName')
. This function takes same arguments as this.el.getPluginValue
.