We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 23571
    • 223 Posts
    I just want a plugin that happens onBeforeDocFormSave and sends an alert. My end goal is to check if the nesting is too deep and alert the user.

    I can’t seem to get any output to happen at all.

    I have tried every form of this I can think of. This is the modx revolution install currently on the homepage.

    <?php
    global $modx;
    return $modx->error->success(’My Custom Message’);
    ?>

    Nothing happens. At all.

    If I put an exit or die in there, when I try to save a document it just spins indefinitely, So I know the event is firing.

    MODX document is horrible. Any help would be greatly appreciated.
      • 27708 MODX Staff
      • 2,502 Posts
      Greg,

      I need more details but writing plugins requires a little more than this.

      Please describe what you are trying to do and what you expect to be the result.

      Returning a value to a plugin I don’t think will work as it would need to output to something or fire a snippet that is called in a document to output to. Plugins don’t output values to a document since they aren’t in the document. If you look at this WordFilter plugin example it is using the output object of the document to place the content into: http://rtfm.modx.com/display/revolution20/Plugins#Plugins-WordFilter

      There are other ways to output the return from a plugin by performing an operation on a tvfield etc.

      Please share more details here.

      Cheers,

      Jay
        Author of zero books. Formerly of many strange things. Pairs well with meats. Conversations are magical experiences. He's dangerous around code but a markup magician. Blog ✦ Twitter ✦ LinkedIn ✦ GitHub
        • 23571
        • 223 Posts
        I may be a total noob here. I come from a mainly drupal module development background.

        In drupal, I can hook into whatever event I want, print_r out all my debug info and exit if I want.

        My end goal is to have a plugin that checks when someone tries to create a document how deeply it is nested (what nth child it is) and if its past a certain number (say 3) it gives them an error message.

        I thought I could do that with $modx->success or $modx->error or $modx->event->error or something...

        How do you guys get debug output when your doing plugin development?
          • 27708 MODX Staff
          • 2,502 Posts
          Plugins and Snippets are separate types of PHP scripts. Snippet output will output to themselves but they are inserted like a php include into a document, chunk or template. Plugins don’t output to the view unless you give them somewhere to output to. You are triggering on event but the output doesn’t occur since there is no "include" on the manager view.

          I looked at the API reference and it looks like you are on the right track but I think the errors just get logged into the error record.

          I mentioned on twitter I am not much of a dev so I defer to the expertise of others. I think you may need to wait it out.

          Cheers,

          Jay
            Author of zero books. Formerly of many strange things. Pairs well with meats. Conversations are magical experiences. He's dangerous around code but a markup magician. Blog ✦ Twitter ✦ LinkedIn ✦ GitHub
            • 3749
            • 24,544 Posts
            It’s tricky to get output from a plugin, especially since any syntax error gets you no output at all.

            If possible, the best way is to implement it as a snippet first, then paste the code into a plugin once it’s working.

            In a snippet, you can pile everything into an $output variable and put return $output at the end of the snippet.

            You probably know this, but if you want the output of print_r(), you need a second argument:

            $output .= print_r($someVariable, true);
              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
              • 23571
              • 223 Posts
              So for example, this works perfectly in a snippet (doc 5 is just a doc that says "you nested too deep") and the ip address is my local dev environment.

              But when I put it in a plugin, and attach it to onbeforedocformsave it fails.

              <?php
              global $modx;
              $parent = array();
              $depth = 2;
              $parent = $modx->getParent(-1, 0);

              if(is_array($parent)) {
              $depth++;
              $parent = $modx->getParent($parent[’id’], 0);
              if(is_array($parent)) {
              $depth++;
              $parent = $modx->getParent($parent[’id’],0);
              }

              }

              if($depth > 3) {
              $modx->sendRedirect(’http://192.168.1.102/modx/index.php?id=5’, 0);
              }
              ?>
                • 23571
                • 223 Posts
                I have also tried things like this:

                return $modx->error->failure($modx->lexicon(’access_denied’));

                which just cause the Saving Document spinner to spin forever.

                I really just want to output a modal that says "You can’t do that" and stop the saving process. I wouldn’t think that would be complicated, but for some reason it has turned out to be.
                  • 23571
                  • 223 Posts
                  Ok so in: /core/model/modx/processors/resource/update.php

                  on line: 240
                  /* invoke OnBeforeDocFormSave event */
                  $modx->invokeEvent(’OnBeforeDocFormSave’,array(
                  ’mode’ => modSystemEvent::MODE_UPD,
                  ’id’ => $resource->get(’id’),
                  ’resource’ => &$resource,
                  ));

                  This loops through and runs the plugin code for each plugin attached to that in an eval.

                  At the bottom of the entire update.php page, it has a : return $modx->error->success(’’,$returnArray);

                  If I change this to : return $modx->error->failure($modx->lexicon(’access_denied’));

                  It says "Access Denied" etc.

                  How do I stop the update.php execution from within the eval?

                  What I want:

                  When you try to save a document, I check what nth level child you are trying to make, if its past 4 levels, I stop you from saving, and give you an error message like "You nested too deep".

                  I have the code working in a snippet, but can’t make it work in a plugin.
                    • 3749
                    • 24,544 Posts
                    As far as I can tell, update.php invokes the event, but doesn’t care what happens with it. Some event invocations check the return value from the event, but I don’t see that here, so the best you can do is to redirect as you’re doing.

                    The getParent() method, btw, is deprecated and getParentIds() would be a lot faster and easier anyway:

                    getParentIds([integer $id = null], [integer $height = 10]) — Returns an array containing the ancestor Resource IDs up the tree. $height is the number of levels (not resources) to search. The immediate parent will be the first member, the grandparent will be the second, and so on.

                    if ( count($modx->getParentIds($id)) > 3) {
                         $modx->sendRedirect($modx->makeUrl(5), 0);
                    }
                      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
                      • 23571
                      • 223 Posts
                      That would be fine, and that was basically what I had concluded, the plugin runs in an eval and nothing gets returned from it, so not much can be done there.

                      Trying to do :
                      $modx->sendRedirect($modx->makeUrl(5), 0);

                      on event "onBeforeDocFormSave"

                      Just causes the progress bar to infinite loop.

                      Any ideas why you can’t use sendRedirect?

                      so all I have right now is this:

                      <?php

                      global $modx;
                      $modx->sendRedirect($modx->makeUrl(5), 0);

                      ?>

                      And it just causes the progress bar to loop over and over again.