Add checkbox to a form
This page describes an example of how to add a checkbox to the web-domain edit form, which when activated will automatically change the domain's home directory from www/domain.name
to www/pub
.
Plugin XML description
# cat /usr/local/mgr5/etc/xml/ispmgr_mod_example.xml
<?xml version="1.0" encoding="UTF-8"?>
<mgrdata>
<handler name="dir" type="xml"> // dir — the name of the handler that will be called. Specify type XML.
<event name="webdomain.edit" after="yes"/>
// Since we need a plugin to edit the web domain, or in the event name, specify the function webdomain.edit
// To make the plugin work after a form change, set the type to after, not before.
</handler>
<metadata name="webdomain.edit" type="form">
<form>
<page name="features">
<field name="cms" experience="expert">
<input type="checkbox" name="cms" setvalues="yes"> // setvalues="yes" for the checkbox, so that the plugin is triggered immediately when the value of the checkbox changes.
</input>
</field>
</page>
</form>
</metadata>
<lang name="ru">
<messages name="webdomain.edit">
<msg name="cms">Example</msg> // Specify the checkbox name
<msg name="hint_cms">This is an example</msg> // Hint
</messages>
</lang>
</mgrdata>
The XML file must have read and write permissions for the root user.
Handler
# cat /usr/local/mgr5/addon/dir
#!/bin/bash
if [ "$PARAM_cms" = "on" ]
then
echo $(cat | sed 's/<\/doc>/<home>www\/pub<\/home><\/doc>/g')
else
cat
fi
To edit the form and change the values of the "root directory" field, you need to modify the XML from which the form is formed. The "root directory" field is answered by the element. At the stage of editing, the form is not yet defined in XML, so it is necessary to add a plugin to XML. You can use xmlstarlet
to edit XML in bash.
Set permissions for the handler with the following commands:
chmod 750 /usr/local/mgr5/addon/<file_handler_name>
chown 0:0 /usr/local/mgr5/addon/<file_handler_name>
After the plugin description XML file is created and the handler file is created, restart the panel using the command below:
/usr/local/mgr5/sbin/mgrctl -m ispmgr exit
Result
There is an additional checkbox on the web domain editing form.
When you change the value of the checkbox, the home directory of the domain changes to www/pub.