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

    I am working on a plugin which only edits the frozen URI.
    I can do everything I need to do when the document is only being updated, because the internal value of the URI has already been generated and I can do what I want with it.

    The problem is when my plugin runs for a NEW resource. I am using the OnBeforeDocFormSave event because I'd like to change the value before it gets saved to the DB. However, when a NEW resource is being saved for the first time, the auto-generated URI has not been set yet, it is blank, so I can't run my plugin code to change it.

    I have look high and low for an answer, API function, resource method, something, to be able to generate this myself by I can't find it. I can use the cleanAlias method but that only generates an alias string and doesn't check if the alias already exists. Generating the URI is more complex because of FURLs and FURL path and parents/containers, suffixes and all that.

    I know there is the makeUrl API function but this requires the ID, which is not created yet for a new resource when it is first being saved. Or is it? I tried using
    $resource->id
    but it didn't work so I assume there is no ID yet.

    The only solution I can think of is use OnDocFormSave so that (I'm guessing), both the ID, and the auto-generated URI would be available. My complaint here is that once my plugin makes it's changes to the resource, it has to then save it immediately a second time. I guess that is how it would work, since the document already saved, and I would change the URI, I would be forced to save it again. This seems like it would take more time and resources to save a document twice, instead of editing values before it gets saved.

    So my question is basically, am I able to access the MODX API to generate the exact same URI that MODX would create anyway when a document is saved so that I can BOTH turn on the frozen URI checkbox, AND set the value, BEFORE the document is first saved?
    OR will I be forced to use the OnDocFormSave event and have to save all my documents TWICE when my plugin runs?

    Thanks!

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

    • discuss.answer
      • 3749
      • 24,544 Posts
      I think you'll find that using OnDocFormSave and saving the resource again will make no noticeable difference in the time it takes to save a resource, especially since it will eliminate the need to call extra functions to get the information you would need in OnBeforeDocFormSave.

      I use it often and have never been aware of any perceptible delay.

      You could attach the plugin to both System Events and have a different section of code for new resources with a switch statement, but I'm not sure it would make much difference, and it could actually be slower since it will always be called twice.

      switch ($modx->event->name) {
          case 'OnBeforeDocFormSave':
              if ($mode == modSystemEvent::MODE_UPD) {
                  /* code */
              }
              break;
      
          case 'OnDocFormSave':
              if ($mode == modSystemEvent::MODE_NEW) {
                  /* code */
              }
              break;
      
      }
      
      
      [ed. note: BobRay last edited this post 11 years, 11 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