Jump to main content Jump to doc navigation

Event: OnDocFormSave

Fires after a Resource is saved in the Manager through the Resource\Create and Resource\Update processors. Also fires when a deleted Resource is edited inline in the trash grid (Resource\UpdateFromGrid).

  • Service: 1 - Parser Service Events
  • Group: Resources

Change TVs here. To change TV values, do it here, not in OnBeforeDocFormSave. Unlike that event, this one does not support $modx->event->output().

Saving happens automatically. You do not need to call $resource->save().

Anything you return from this event is written to the error log.

Event Parameters

Name Description
mode Either new or upd (modSystemEvent::MODE_NEW / MODE_UPD).
id The ID of the Resource, even for new Resources.
resource A reference to the modResource object.
reloadOnly Only on creation: true when the processor just reloads the form after a validation error.
object Only on update: alias of resource — a reference to the same object.

Examples

Read the Resource ID

// Log all available properties of the $resource
$modx->log(modX::LOG_LEVEL_ERROR, print_r($resource->toArray(),true) );
// Get the page id
$page_id = $resource->get('id');
// or simply
$page_id = $id;

if ($mode == 'new') {
    // resource created
}
else {
   // existing resource was updated
}

Set a TV value

<?php
$eventName = $modx->event->name;
switch($eventName) {
    case 'OnDocFormSave':
        //if the resource has a template=5
        if ($resource->get('template') == 5) {
            // after saving the TV value price=128
            if(!$resource->setTVValue('price', '128')) {
                $modx->log(modX::LOG_LEVEL_ERROR, 'Having problems setting the TV value.');
            }
        }
        break;
}

Whether the price TV was filled in before saving does not matter: the plugin writes 128.

See Also

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.

Backers

  • modmore
  • STERC
  • Digital Penguin
  • Jens Wittmann – Gestaltung & Entwicklung
  • CrewMark
  • Fabian Christen
  • Sepia River Studios
  • Dannevang Digital
  • Alex
  • A. Moreno
  • Chris Fickling
  • Stéphane Jäggi
  • Murray Wood
  • Anton Tarasov
  • JT Skaggs
  • deJaya
  • Lefthandmedia
  • eydolan
  • Following Sea
  • Guido Gallenkamp
  • YJ
  • Raffy
  • Snow Creative
  • Nick Clark
  • Guest
  • Helen
  • krisznet
  • Yanni
  • Richard

Budget

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

Learn more