We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17895
    • 209 Posts
    Where can I find the explanation of the events for the plug-ins? I read a simple example in the MODx example, but it uses only an event and I cannot find anywhere an explanation about when the event happends and which variables are available, what can you modify, etc.

    Do I miss something?
      Daniele "MadMage" Calisi
      • 18397
      • 3,250 Posts
      Documentation for plugin events is not yet complete but most of the events do exactly what their name implies.

      For example, ondocformrender fires right when the Edit/Create document form comes up.
        • 17895
        • 209 Posts
        ok, but... I need something that redirects the browser in another page...

        I need to check a field of the site_content table of the current document and then do a redirect...

        I know how to do the redirect, but, as you know, it has to happend in the header part, so BEFORE anything is sent to the browser

        So, I need an event that happends when I have the $modx->documentObject[] stuff and BEFORE it writes something...

        which event should I use?
          Daniele "MadMage" Calisi
        • I think a snippet would do the job; have it check with $modx->documentObject[’field’] and redirect if it finds what it’s looking for. Just drop the snippet into your template somewhere, probably in the head.

          I suppose you can use the same thing in a plugin; I’m not exactly sure at what system event the documentObject array is available. You can always do it like I do; trial and error! It will be one of the top group of events for sure. Try "onWebPagePreRender"; that won’t be the most efficient since it occurs after all the parsing is done, and you probably would like to do it as soon as the array is available.
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 32241
            • 1,495 Posts
            Or you can use onWebPageInit.

            I believe that MODx is using cache from PHP, which doesn’t send the ouput until the last execution of document parser.

            You might wanna give it a try.
              Wendy Novianto
              [font=Verdana]PT DJAMOER Technology Media
              [font=Verdana]Xituz Media
              • 32963
              • 1,732 Posts
              Here’s some more info on event calling order:

              1. OnWebPageInit - Very first event when a page is initialized.
              2. OnLoadWebPageCache - If page is cached then this event is next. The cached documentObject is available here.
              3. OnLoadWebDocument - if page is not (yet) cached then this event is triggered next. The documentObject is available here
              4. OnWebPagePrerender - This is the final event that’s triggered before the page is sent to the browser.

                xWisdom
                www.xwisdomhtml.com
                The fear of the Lord is the beginning of wisdom:
                MODx Co-Founder - Create and do more with less.
                • 17895
                • 209 Posts
                I think that I could redirect simply using the header Location... but the redirection enver occurs... I made a plug-in for the OnWebPagePrerender event with a simple

                header("Location: http://xxxxx");
                return;

                but it doesn’t work...
                  Daniele "MadMage" Calisi
                  • 32963
                  • 1,732 Posts
                  can you display the source of your plugin?
                    xWisdom
                    www.xwisdomhtml.com
                    The fear of the Lord is the beginning of wisdom:
                    MODx Co-Founder - Create and do more with less.
                  • I always add a bunch of troubleshooting "echo" statements to my code while developing. If you’re looking for a certain value in the documentContent, put an echo ’found it’; in the conditional block. If you know it’s there, but it’s just not finding it, you can refine the conditional to return more information until you figure out why.

                    This way you can narrow down the issues until you find exactly where the problem is. If it’s finding your conditional, but the redirect isn’t working, then you know it’s a problem with your redirect code.
                      Studying MODX in the desert - http://sottwell.com
                      Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                      Join the Slack Community - http://modx.org
                      • 17895
                      • 209 Posts
                      it’s just this!
                      (only to try, I removed the switch event stuff)

                        header("Location: http://www.mindreamz.net/modx/index.php?id=1");
                        return;
                      


                      the code was (that did not work)

                      switch ($modx->Event->name) {
                      case "OnLoadWebDocument":
                        if (isset($_GET['changeLang'])) $_SESSION['desiredContentLang'] = $_GET['changeLang'];
                        $tbl = $modx->getFullTableName("site_content");
                        $redirect = false;
                        if ($modx->documentObject['contentLang'] == '' && isset($_SESSION['desiredContentLang']) {
                          // this page is in default language, but the preferred language is another one
                          $sql = "SELECT id FROM $tbl WHERE $tbl.published = 1 AND $tbl.deleted = 0 ";
                          $sql .= "AND $tbl.refId = " . $modx->documentObject['id'] . " ";
                          $sql .= "AND $tbl.contentLang = '" . $_SESSION['desiredContentLang'] . "'";
                          $rs = $modx->dbQuery($sql);
                          if ($row = $modx->fetchRow($rs)) {
                            $redirectId = $row['id'];
                            $redirect = true;
                          }
                        }
                        else if ($modx->documentObject['contentLang'] != '' && !isset($_SESSION['desiredContentLang'])) {
                          // this page is not in the default language, but the preferred language is the default
                          $redirectId = $modx->documentObject['refId'];
                          $redirect = true;
                        }
                        else if ($modx->documentObject['contentLang'] != '' && isset($_SESSION['desiredContentLang'])
                        && $modx->documentObject['contentLang'] != $_SESSION['desiredContentLang']) {
                          // neither the page is in the default language, nor the desired language is the default
                          // but the desired language is different from that of this page
                          // this page is in default language, but the preferred language is another one
                          $sql = "SELECT id FROM $tbl WHERE $tbl.published = 1 AND $tbl.deleted = 0 ";
                          $sql .= "AND $tbl.refId = " . $modx->documentObject['refId'] . " ";
                          $sql .= "AND $tbl.contentLang = '" . $_SESSION['desiredContentLang'] . "'";
                          $rs = $modx->dbQuery($sql);
                          if ($row = $modx->fetchRow($rs)) {
                            $redirectId = $row['id'];
                            $redirect = true;
                          }
                          else {
                            $redirectId = $modx->documentObject['refId'];
                            $redirect = true;
                          }
                        }
                        if ($redirect) header("Location: http://www.mindreamz.net/modx/index.php?id=$redirectId");
                      }
                      
                        Daniele "MadMage" Calisi