We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 5683
    • 96 Posts
    Many plugins are triggered by the "OnWebPagePrerender" event, and do their job on a page every time that page is loaded. However, in most cases the result of their job will not change until the document content is modified. Therefore, it makes sense to apply these plugins BEFORE caching. I would like to do that, for example, for the Geshi plugin, which is *very* slow.

    The question is: (without reverse engineering the parser code) which object should I modify so that it gets cached AND outputted correctly in a plugin that is invoked BEFORE caching?

    I made an experiment: I wrote a plugin triggered by the "OnBeforeSaveWebPageCache" event, which modified the $modx->documentObject[’content’], but it did not work...

    Thanks smiley
      • 22303 MODX Staff
      • 10,725 Posts
      Try using $modx->documentContent to manipulate the content to be cached.
        • 5683
        • 96 Posts
        Ok -- done, with some reverse engineering of the parser grin
        (btw, very neat code!!)

        The trick was to use the event "OnLoadWebDocument", which is invoked as soon as a page is load from the DB: then you can modify $modx->documentObject[’content’].

        It works very well, and I believe it should be a sort of guideline for designing efficient plugins. I’m updating my Geshi plugin shortly.

        Thank you!
          • 25663 MODX Staff
          • 12,272 Posts
          Would love to see your thoughts and ideas in this regard. Did you change any of the events or just use alternate ones... man, we really need some documentation on events tongue
            Ryan Thrash, MODX Co-Founder
            Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
            • 5683
            • 96 Posts
            No, I only used the alternate event "OnLoadWebDocument". And, instead of parsing the documentOutput, I parsed the documentObject[’content’] field.

            The difference from the "traditional" approach is that, while the "OnWebPagePrerender" event is invoked after the parsing process, i.e. immediately before sending the HTML output (and thus when the template has been applied, the snippets evaluated etc.), I had to work with the "raw" document object, as it is stored on the DB. The documentObject[’content’] field is just the content field as edited in the Manager.

            I think that working on the content field is fine in most cases, but not always (for example if you want to apply a plugin also to TVs, to evaluated snippets and chunks, it is easier to work with the documentOutput... but maybe there is another easy solution that preserves efficiency).
              • 22303 MODX Staff
              • 10,725 Posts
              Just FYI. The $modx->documentObject[’content’] is just the raw content from the db, and modifying this will only affect the output if called before the parser uses it to establish $modx->documentContent. The $modx->documentContent is the content as it is to be cached and used from cache; the documentObject[’content’] is not used after the initial content is loaded from db or cache except by snippets that specifically use documentObject[’content’]. The documentOutput is copied, either as generated from the db or from cache, from documentContent, before final processing and output. The documentOutput is discarded after being rendered to the viewer. So, you might want to be operating on $modx->documentContent, if you want the plugin effects to be cached. But since I don’t exactly what you are doing, kind of hard to say; just thought I’d mention it.
                • 2734
                • 165 Posts
                Hi.
                I know, this is an old Topic. But i think this is the right place to post my question:
                First of all, i’d like to know if the described process is still valid for MODx Version 0.9.6
                If yes, i wonder why Plugins that resize Images don’t use this procedure? Images have to be recalculated only when the site is being changed, right?

                Another question regarding the Events:
                Is there a event that fires upon saving a Page in the Manager? That would be the ideal place to generate dynamically created Images. Or do i overlook something important?

                Cheers // banal