We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38593
    • 129 Posts
    Excuse the nob question ... but can someone explain the difference between similarly named system events:
    OnChunkSave vs OnChunkFormSave
    OnTemplateSave vs OnTempFormSave
    OnSnippetSave vs OnSnipFormSave

    And why do resources have only: OnDocFormSave and not an OnResourceSave or OnDocSave??

    This question has been answered by BobRay. See the first response.

      Pedalers Bicycle Tours my website forged with MODX
      vandergraaf-M static website generator for MODX
      • 42562
      • 1,145 Posts
      1. OnChunkSave => future perfect tense - happens after the chunk shall have been saved.
      When I stupidly jump off the cliff, the splat aftereffect
      https://rtfm.modx.com/revolution/2.x/developing-in-modx/basic-development/plugins/system-events/onchunksave

      2. OnChunkFormSave => present tense - fires when the chunk is being saved - once the process has begun
      When I stupidly jump off the cliff, the very first dust off my first step
      https://rtfm.modx.com/revolution/2.x/developing-in-modx/basic-development/plugins/system-events/onchunkformsave

      Maybe resources used to be called documents, and you know how name-changing can be.
      Maybe there is alternative to OnDocSave or it is technically incongruous to have it.
        TinymceWrapper: Complete back/frontend content solution.
        Harden your MODX site by passwording your three main folders: core, manager, connectors and renaming your assets (thank me later!)
        5 ways to sniff / hack your own sites; even with renamed/hidden folders, burst them all up, to see how secure you are not.
        • 38593
        • 129 Posts
        Thanks Sir Bard of Avon, that helps.

        Is it possible to create an OnDocSave version of the system event? Something that fires after the save.

        The project I am working on is bringing a fetch and save script (saving website as static html rather than serving direct from MODX) into MODX as a plugin. I am using the OnDocFormSave event to trigger a rebuilding of existing html files when a resource is modified or creating a new html file when a new resource is created.

        BUT, as it seems the event is triggered before the save is done, Wayfinder does not add the new page into it output. The html page itself builds just fine, but the navigation system is missing the new page. If I trigger the snippet a second time, the new page appears in Wayfinder's output. [ed. note: mrcycling last edited this post 10 years, 7 months ago.]
          Pedalers Bicycle Tours my website forged with MODX
          vandergraaf-M static website generator for MODX
          • 42562
          • 1,145 Posts
          After occureth when precisely it doth. For to occur, in the vulgar tongue, is nothing else but to occur at the time it doth occur.

          After = after the chunk is completely saved

          Is Wayfinder called uncached? Wayfinder might have lost its way.

          Try clearing cache with your script (dangerous)

            TinymceWrapper: Complete back/frontend content solution.
            Harden your MODX site by passwording your three main folders: core, manager, connectors and renaming your assets (thank me later!)
            5 ways to sniff / hack your own sites; even with renamed/hidden folders, burst them all up, to see how secure you are not.
          • discuss.answer
            • 3749
            • 24,544 Posts
            Let me jump in with a few points. I'll use chunks as an example, but the principles apply to other elements as well.

            The main difference between the OnChunkSave and OnChunkFormSave events is where they take place. Both are fired after the chunk is saved to the DB, but the OnChunkFormSave event is fired in the Manager layer, so (with one exception -- see below) it will only fire if you save a chunk in the Manager.

            OnChunkSave is fired in the modChunk class itself, so it will always be fired when a chunk is saved no matter where or how it's done. For example, if in code you do this:

            $chunk = $modx->newObject('modChunk');
            $chunk->set('name', 'MyChunk');
            $chunk->setContent('Some Content');
            $chunk->save();


            OnChunkSave will fire, but OnChunkFormSave will not because you didn't save it from a form in the Manager.

            The exception is when you use a MODX processor. If you call the chunk create or update processor, the processor will fire the OnChunkFormSave event for you. The OnChunkSave event will also fire when the processor calls $chunk->save(). This is one reason why it's recommended to use the processors when possible.

            Similarly, for Resources, if you call the resource create or update processor, it will fire OnDocFormSave for you.

            @mrcycling: I don't know why there's no OnResourceSave or OnDocSave event, but if you save a resource in the Manager or call the resource create or update processor, OnDocFormSave will always fire.

            There's information here on using the processors and there's a full list of the available processors (I had completely forgotten that I created this page wink .)

            The most common reason a new resource won't show up is because Resources are heavily cached, and the new resource is not in the cache (more specifically, the resource map, which is used to list and find resources).

            One solution is to do this after creating the resource:

            $modx->reloadContext('web');  // or whatever the Resource's context is


              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting
              • 42562
              • 1,145 Posts
              mrcycling I told you!
                TinymceWrapper: Complete back/frontend content solution.
                Harden your MODX site by passwording your three main folders: core, manager, connectors and renaming your assets (thank me later!)
                5 ways to sniff / hack your own sites; even with renamed/hidden folders, burst them all up, to see how secure you are not.
                • 38593
                • 129 Posts
                Thanks to both for the help. For this use it seems that either version would work or simply check all the boxes to be safe ;-)

                @BobRay - that reloadContext did the trick.

                Now all I have to figure out is why mkdir() works beautifully in an external file, but fails when used in a MODX snippet .... if it ain't one thing it is another .... oh never mind, got that figured out too smiley [ed. note: mrcycling last edited this post 10 years, 7 months ago.]
                  Pedalers Bicycle Tours my website forged with MODX
                  vandergraaf-M static website generator for MODX
                  • 3749
                  • 24,544 Posts
                  You probably know this, but if you check all the boxes, your code will often run more than once.
                    Did I help you? Buy me a beer
                    Get my Book: MODX:The Official Guide
                    MODX info for everyone: http://bobsguides.com/modx.html
                    My MODX Extras
                    Bob's Guides is now hosted at A2 MODX Hosting
                    • 38593
                    • 129 Posts
                    Actually this newbie hadn't thought about that aspect directly sad

                    But was inadvertently addressing it anyway, as I am now working on system event detection and triggering the right response (rebuild one html file, delete one html file, rebuild all html files).
                      Pedalers Bicycle Tours my website forged with MODX
                      vandergraaf-M static website generator for MODX