We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 49132
    • 7 Posts
    When resource is temporairly marked as unpublished via manager panel, I need 302 redirect to parent resource, instead of default 301.
    I'm using MODX Revo 2.2.11
    Maybe call to $modx->sendRedirect(), by where and when? Any system event?
    How can I achieve this? Thanks!
    • To begin with, please upgrade your installation to 2.2.15.

      You should be able to use the OnPageNotFound event to see if the resource does in fact exist, just isn't published. Unfortunately it doesn't appear as if any information about the page that was requested is provided, so you'd need to process the GET to look for the resource ID yourself.

      With friendly URLs on, you need to deal with the 'q=page.html' GET value, which can get interesting if it's using the full path, and if it's using a non-standard suffix. Once you get a resource ID returned, then you can do whatever you want with it in the way of headers.
        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
        • 49132
        • 7 Posts
        Quote from: sottwell at Oct 06, 2014, 12:43 PM
        To begin with, please upgrade your installation to 2.2.15.

        You should be able to use the OnPageNotFound event to see if the resource does in fact exist, just isn't published. Unfortunately it doesn't appear as if any information about the page that was requested is provided, so you'd need to process the GET to look for the resource ID yourself.

        With friendly URLs on, you need to deal with the 'q=page.html' GET value, which can get interesting if it's using the full path, and if it's using a non-standard suffix. Once you get a resource ID returned, then you can do whatever you want with it in the way of headers.

        Thank you for response, but I tried and OnPageNotFound event not fired when resource exists but unpublished.
          • 3749
          • 24,544 Posts
          I think best place to catch this before the 404 is thrown would be in a plugin attached to OnHandleRequest. The ID of the resource should be available there as $modx->resourceIdentifier.

          This should get you started. If the resources are all in the default context ('web'), you should be able to do this:

          $id = $modx->resourceIdentifier;
          
          $query = $modx->newQuery("modResource", array('id' => $id));
          $query->select(array(
              'id' => 'id',
              'parent' => 'parent',
              'published' => 'published'
          ));
          if ($query->prepare() && $query->stmt->execute()) {
              $results = $query->stmt->fetchAll(PDO::FETCH_ASSOC);
          }
          if (empty($results)) {
             $modx->log(modX::LOG_LEVEL_ERROR, '[Forward Plugin] Resource not found with id: ' . $id );
             return '';
          }
          
          if (isset($results['published'] && (! empty($results['published']))) {
             return '';
          }
          
          if (isset($results['parent'] && (!empty($results['parent'])))
              $url = $modx->makeUrl($results['parent'], "", "", "full");
              $modx->sendRedirect($url);
          }
          return '';
          


          Be aware that this will ignore resources at the root of the tree, since their parent field contains a 0.

          Saving the resource after unpublishing it should clear the cache, but it might not clear the resource map, which could contain the published status. In that case, you might have to call $modx->reloadContext() at the top or your code, which would slow down all page loads.

          Let us know if it works. wink
          [ed. note: BobRay last edited this post 9 years, 5 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
            • 49132
            • 7 Posts
            Quote from: BobRay at Oct 07, 2014, 06:58 AM
            I think best place to catch this before the 404 is thrown would be in a plugin attached to OnHandleRequest. The ID of the resource should be available there as $modx->resourceIdentifier.

            This should get you started. If the resources are all in the default context ('web'), you should be able to do this:

            $id = $modx->resourceIdentifier;
            
            $query = $modx->newQuery("modResource", array('id' => $id));
            $query->select(array(
                'id' => 'id',
                'parent' => 'parent',
                'published' => 'published'
            ));
            if ($query->prepare() && $query->stmt->execute()) {
                $results = $query->stmt->fetchAll(PDO::FETCH_ASSOC);
            }
            if (empty($results)) {
               $modx->log(modX::LOG_LEVEL_ERROR, '[Forward Plugin] Resource not found with id: ' . $id );
               return '';
            }
            
            if (isset($results['published'] && (! empty($results['published']))) {
               return '';
            }
            
            if (isset($results['parent'] && (!empty($results['parent'])))
                $url = $modx->makeUrl($results['parent'], "", "", "full");
                $modx->sendRedirect($url);
            }
            return '';
            


            Be aware that this will ignore resources at the root of the tree, since their parent field contains a 0.

            Saving the resource after unpublishing it should clear the cache, but it might not clear the resource map, which could contain the published status. In that case, you might have to call $modx->reloadContext() at the top or your code, which would slow down all page loads.

            Let us know if it works. wink


            I tried this and it doesn't seems to be working =(
            All requests handled with empty id, I think $modx->resourceIdentifier() isn't available before request handled...

            I correct some typos and edd extra logging:

            <?php
            $id = $modx->resourceIdentifier;
            $query = $modx->newQuery("modResource", array('id' => $id));
            $query->select(array(
                'id' => 'id',
                'parent' => 'parent',
                'published' => 'published'
            ));
            if ($query->prepare() && $query->stmt->execute()) {
                $results = $query->stmt->fetchAll(PDO::FETCH_ASSOC);
            }
            if (empty($results)) {
                $modx->log(modX::LOG_LEVEL_ERROR, '[Forward Plugin] Resource not found with id: ' . $id );
                return '';
            }
            
            if (isset($results['published']) && (!empty($results['published']))) {
                $modx->log(modX::LOG_LEVEL_ERROR, '[Forward Plugin] Resource published');
                return '';
            }
                
            if (isset($results['parent']) && (!empty($results['parent']))) {
                $modx->log(modX::LOG_LEVEL_ERROR, '[Forward Plugin] Redirect 302');
                $url = $modx->makeUrl($results['parent'], "", "", "full");
                $modx->sendRedirect($url);
            }
            $modx->log(modX::LOG_LEVEL_ERROR, '[Forward Plugin] Nothing');
            return '';
            


            ... but in log I see only this.

            ...
            [2014-10-07 15:19:13] (ERROR @ /index.php) [Forward Plugin] Resource not found with id: 
            [2014-10-07 15:19:13] (ERROR @ /index.php) [Forward Plugin] Resource not found with id: 
            [2014-10-07 15:19:13] (ERROR @ /index.php) [Forward Plugin] Resource not found with id: 
            [2014-10-07 15:19:13] (ERROR @ /index.php) [Forward Plugin] Resource not found with id: 
            [2014-10-07 15:19:13] (ERROR @ /index.php) [Forward Plugin] Resource not found with id: 
            [2014-10-07 15:19:14] (ERROR @ /index.php) [Forward Plugin] Resource not found with id: 
            [2014-10-07 15:19:14] (ERROR @ /index.php) [Forward Plugin] Resource not found with id: 
            [2014-10-07 15:19:14] (ERROR @ /index.php) [Forward Plugin] Resource not found with id: 
            [2014-10-07 15:19:14] (ERROR @ /index.php) [Forward Plugin] Resource not found with id: 
            [2014-10-07 15:19:17] (ERROR @ /index.php) [Forward Plugin] Resource not found with id: 
            ...
            
            [ed. note: wolf6969 last edited this post 9 years, 5 months ago.]
              • 3749
              • 24,544 Posts
              Sorry, you're correct. $modx->resourceIdentifier is not set until after OnHandleRequest.

              It's possible to get the ID in your plugin, but try switching the event to OnWebPageInit first. It's definitely set there, but I'm not sure if you'll reach that event for an unpublished doc.
                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
                • 49132
                • 7 Posts
                No, it's not available when document is unpublished. I'm little dissappointed, have no idea what to try. Is it possible to retreive document id from request and send custom redirect if it's unpublished?
                • Yes, you would need to get the alias as passed via the URL. If you are using Friendly URLs, it's 'q=alias.html', although if you are also using Alias Paths it might be more like 'q=path/to/alias.html'. It would also vary depending on what suffix you are using, the default being .html.

                  In any case, you need to get the actual alias of the resource, then you can check for its published status and proceed accordingly.

                  If you have these resources set to auto-publish, you would also need to check whether or not the resource in question is due to be published.

                  You can use the findResource function in the core/model/modx/modx.class.php as well as several useful functions in core/model/modx/modrequest.class.php.
                    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
                    • 3749
                    • 24,544 Posts
                    I think this may do it (untested). Connect it to OnPageNotFound so it won't slow down all page loads.

                    $uri = $_SERVER['REQUEST_URI'];
                    /* Use the next line if a subdirectory name is part of the uri */
                    /* (echo $_SERVER['REQUEST_URI'] in a test snippet to find out) */
                    // $uri = str_replace('/subdirectory/', '', $uri);
                    
                    $id = $modx->findResource($uri);
                    
                    if empty($id) {
                       /* True page not found, do nothing */
                       return '';
                    }
                    
                    /* See if it's published */
                    $c = $modx->newQuery('modResource', $id);
                    $query->select('published');
                    $published = $modx->getValue($query->prepare());
                    
                    if (empty($published)) {
                       /* URL of temporarily unpublished page (I think 302 is the default code) */
                       $modx->sendRedirect('http://mysite.com/somepaget.html');
                    }
                    return '';
                    



                      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