Jump to main content Jump to doc navigation

Adding a Custom Field

Adding custom fields to manager forms - such as the Create Chunk, Update Resource, etc - in MODX Revolution is fairly straightforward. You just use the On*FormRender Plugin events.

We want to add a field called 'Home' that puts an address field into the manager interface, and then stores it into the longtitle value (this is not the best place to store it, but let's go along with it for tutorial purposes :) ).

To do so, we'd create a Plugin and associate it to the OnDocFormRender and OnDocFormSave events. Our code would look like this:

 * Register a form field to forms
switch ($modx->event->name) {
    case 'OnDocFormPrerender':
        /* if you want to add custom scripts, css, etc, register them here */
    case 'OnDocFormRender':
        $v = '';
        if (isset($scriptProperties['resource'])) {
            /* on the update screen, so set the value */
            $v = $scriptProperties['resource']->get('longtitle');
        } else {
            /* on the create screen, so set the default */
            $profile = $modx->user->getOne('Profile');
            $v = $profile->get('address');
        /* now do the HTML */
        $fields = '
<div class="x-form-item x-tab-item">
    <label class="x-form-item-label" style="width:150px;">Home</label>
    <div class="x-form-element">
        <input type="text" name="home" value="'.$v.'" class="x-form-text x-form-field" />
    case 'OnDocFormSave':
        /* do processing logic here. */
        $resource =& $scriptProperties['resource'];

Note the CSS classes and styling in the form HTML. Those are unnecessary; but will make the form "match" the styling of the rest of the fields.

Support the team building MODX with a monthly donation.

The budget raised through OpenCollective is transparent, including payouts, and any contributor can apply to be paid for their work on MODX.



$0 per month—let's make that $500!

Learn more