Jump to main content Jump to doc navigation

Event: OnDocFormSave

Fires after a Resource is saved in the manager via the editing form.

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

TVs are best Modified Here If you need to modify TV values, it's best to modify them here and not during OnBeforeDocFormSave.

Unlike OnBeforeDocFormSave, this event does not support the $modx->event->output() method.

Event Parameters

Name Description
mode Either 'new' or 'upd', depending on the circumstances.
resource A reference to the modResource object.
id The ID of the Resource (even for new resources)

Examples

To do something with the page ID (e.g. to update a related custom table), you can read this out of the $resource object (even if you are creating a new resource):

// 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
}

Anything you return from this event will be written to the logs, e.g.

return "Help I'm a bug!";

Will result in a log message like the following:

 [2012-06-22 13:00:28] (ERROR @ /connectors/resource/index.php) [OnDocFormSave]Help I'm a bug!

Calculating a TV Value

switch ($modx->event->name) {

        // Documents
        case 'OnDocFormSave':
            if ($resource->get('template') == 8) {  
                if(!$resource->setTVValue('my_tv', 'Some Value')) {
                    $modx->log(modX::LOG_LEVEL_ERROR, 'There was a problem setting the TV value.');
                }
            }

        break;
}

Such a plugin will display an array of the saved resource in the "Error log":

<?php
$eventName = $modx->event->name;
switch($eventName) {
    case 'OnDocFormSave':
        $modx->log(MODX_LOG_LEVEL_ERROR, print_r($resource->toArray(),true) );
        break;
}

Such a plugin will set the value of the TV price of the current resource equal to 128, in case of an error an entry will be added to the "Error log":

<?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;
}

It doesn't matter if the price field is filled in before saving or not. The plugin will write 128.

Saving Happens Automatically No need to run the $resource->save() method as that happens automatically.

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
  • Jens Wittmann – Gestaltung & Entwicklung
  • Fabian Christen
  • Digital Penguin
  • Dannevang Digital
  • Sepia River Studios
  • CrewMark
  • Chris Fickling
  • deJaya
  • Following Sea
  • Anton Tarasov
  • eydolan
  • Raffy
  • Lefthandmedia
  • Murray Wood
  • Snow Creative
  • Nick Clark
  • Helen
  • JT Skaggs
  • krisznet
  • YJ
  • Yanni
  • Richard

Budget

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

Learn more