We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 39437
    • 13 Posts
    Hi,

    I am on the Way to write my first plugin in MODx and have some uncertainties.
    First I will describe what I want to do:
    My Plugin should work like an output filter without having to append it to the the tag.
    I would like to alter the values of resource fields before they are used for generating the parsed resource.
    If the resulting resource is cached afterwards, it would not be necessary to alter those values again if they resource is accessed again.

    I had troubles finding the right Event I should bind my Plugin to, but I think I finally found a good one: "OnLoadWebDocument" is this right?
    Does someone know, is there a graph, that shows in which order the events are loaded so that it is easier to find the right one?

    At this Event I have no problem accessing the Resource and its fields:

    $modx->resource->_fields['content']

    I alter the Content there and it will be shown on the page as intended. It will be cached too. Cool.
    But now, as the resource is cached, I will not have to rerun my code on every request. How can I find out, if the Resource is pure and I have to act or the resource was already taken from the cache and I will do nothing.

    if(resource is pure){run Plugin code}

    All the resource Cache fields only give information if the resource is cacheable, but none of it tells me if it was already cached.

    Now it comes into my mind, I could look if the file of cacheKey exists. If yes, this resource is already cached. Should I do it this way?

    As I wrote my question I found out two things:
    First, it is always good to write a question in many cases after writing the question I can answer it on my own (see above). Now I only ask am I right?

    Second, while writing the question I find a new perspective to the Problem and maybe that the task in question is not well thought. What I have overseen is, that of cause I would also like to alter those Values whenever a resource is accessed, and not only the one that was called by this request. So maybe the Event is not the right one. Maybe there is not even a solution for the way I wanted to do this. Maybe the outputfilter is the better solution.

    thank you for your thoughts,
    Hannes
      MODX Rocks I am so glad that I have found it.
      • 3749
      • 24,544 Posts
      How about altering those values when the Resource is saved, using OnDocFormSave? That way you only have to do it once and the change is in both the cache and the database. Once all the changes have been made, you could restrict the action of the plugin to new resources, so it wouldn't modify existing ones.

        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
        • 39437
        • 13 Posts
        Thank you for your suggestion. I already read about that possibility too, but I do not want to confuse the User by presenting him the altered content in the manager, or giving him the possibility to "destroy" what I have changed.
        I already thought about creating a shadow copy of the content, that would be created on every save. But that seemed too much effort for me ans is out of my scope (still).
          MODX Rocks I am so glad that I have found it.
          • 3749
          • 24,544 Posts
          Your original method seems fine to me, you could do a quick strstr() call to find out if the changes have already been applied before modifying the content.

          You could check the cache key, but it's tricky and I think you'd still have to do the strstr() to make sure the cached version was correct, so I'm not sure it's worth the trouble.
          [ed. note: BobRay last edited this post 13 years, 2 months ago.]
            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
            • 39437
            • 13 Posts
            I decided to implement it as a filter.
            This means, that it is not workling like magic, without having to adjust the Templates and Chunks.
            Somehow this is even a feature, because so it can be decided if it should be applied or not. That's good too.
            Thanks again for your thought, Hannes
              MODX Rocks I am so glad that I have found it.