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

    I have a snippet (formit2Resource) creating a document from a form.

    After it is executed it redirects the user to a different page

    I want to

    - have snippet execute
    - have it return the ID of the document that it just created
    - Make the url of the redirect page contain this doc ID as a query string

    IE www.domain.com/redirecturl?newdocid=235

    Any guidance on how to have a snippet return the ID of a document that it creates would be appreciated.

    Stuart
      • 36906
      • 34 Posts
      TheNewManifesto Reply #2, 15 years ago
      Hi,
      I haven't used the Formit script, but from what I understand it may have 'hooks' that allow you to call 'post process' scripts.

      If not, you might want to look at something like:
      $res = $modx->newObject('modResource');
      $res->fromArray($properties);
      $res->save();
      $resource_id = $modx->lastInsertId();
      

      Then you could use:
      header("Location: htp//example.com?newdocid=$resource_id");
      exit();
      

      Depending on what you need to do at the redirect juncture, this might also be an option:
      $modx->sendForward($resource_id);
      


      I think sendFroward() would keep you on the same URL (as your initial form submission), but would display the resource content as though viewing the page normally.


      In the 'formit2Resource' documentation are there any references to 'hooks', or 'post processing'?
      If not, you could probs look at the formit2Resource code, and see if it can be amended to include the above. [ed. note: thenewmanifesto last edited this post 15 years ago.]
        • 3749
        • 24,544 Posts
        You might look at NewsPublisher, which creates resources in the front end and lets you specify the fields to show.

        You'd have to modify the NewsPublisher class forward() method to include the ID parameter, but that should be very simple.

        You could also borrow the code NP uses to get the resource ID, although NP calls the MODX processor to create the doc -- not sure if FormIt2Resource does that or not.

        I don't think FormIt2Resources does postHooks, but I could be wrong.
          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
          • 27716
          • 126 Posts
          Hi Guys,

          Thanks so much for the code. Turned out to be much easier than anticipated. I just edited the formit2resource script and after
          $doc->save();

          Added the line
          $resource_id = $modx->lastInsertId(); 


          Then at the end of the script replaced return true;

          with

          return header("Location: http://example.com/page?id=$resource_id");


          What do you think about possibility of collisions with the script being executed by multiple uses at close to the same time?

            • 36906
            • 34 Posts
            TheNewManifesto Reply #5, 15 years ago
            Hi,

            I think I get your concern - it looks as though another quick succession call to the script could 'jump in the middle' and get the ID wrong...

            I haven't looked at that particular bit of XPDO code, but I'm assuming its just a bridge to the PDO equivalent, in which case you'll be fine.
              • 33976 ☆ A M B ☆
              • 571 Posts
              Hey guys,

              To prevent collisions, why not just use
              if ($doc->save() == false) {
                  return 'An error occurred while saving the document';
              }
              $resource_id = $doc->get('id');