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

    I would like to know if there is a way to redirect the user on another page, the dashboard for example, after a resource deletion.

    The first reason I want to do that is because I made a plugin that completely remove resources when resources are deleted (because I don't want to use the trash system and I didn't found another way to do that).
    The second reason is here : if I hide the resources tree for a group of users (because I don't need them to use it), it's not very understandable for them to still display the resource after the deletion.

    Cheers.

    Edit : See this post for the complete procedure to disable trash system.

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

    [ed. note: romainfallet last edited this post 8 years, 5 months ago.]
      • 3749
      • 24,544 Posts
      You can paste the URL of the page you want to send them to into this code and put it at the end of your plugin:

      $url = 'Paste URL here';
      $modx->sendRedirect($url);
        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
        • 47212
        • 40 Posts
        Quote from: BobRay at Nov 11, 2015, 08:29 PM
        You can paste the URL of the page you want to send them to into this code and put it at the end of your plugin:

        $url = 'Paste URL here';
        $modx->sendRedirect($url);

        I already tried but unfortunately, it did not work. I think it is because the plugin is called with an Ajax request from the manager.
          • 3749
          • 24,544 Posts
          Then it's not really a plugin. Plugins fire in response to System Events.

          It might work if you made it a real plugin tied to OnResourceDelete.

          Another option might be to use this:

          header('Location: ' . $url);


          You could also instantiate MODX in your code. Then sendRedirect() should work: http://bobsguides.com/blog.html/2013/07/13/using-modx-outside-of-modx/

            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
            • 47212
            • 40 Posts
            This really a plugin, with the onDocFormDelete event. Modx is fully initalized, it removes my resource as expected with the related api.

            That's why I though about ajax thing.

            I already tried the header fonction but it does nothing. I will try with onResourceDelete event. [ed. note: romainfallet last edited this post 8 years, 5 months ago.]
              • 3749
              • 24,544 Posts
              There's no way I know of that $modx->sendRedirect() could fail in your plugin as long as the $modx object exists -- assuming that the user is logged in and has the permissions necessary to go to the page you're sending them to.

              Could you have the URL wrong?

              Could it be that the redirect code is not being reached?

              If you're using Ajax, possibly the code being called is not returning.

              Feel free to post the code of your whole plugin.
                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
                • 47212
                • 40 Posts
                I don't use Ajax myself, the only thing I have if this small plugin called on the "OnResourceDelete" event :
                <?php
                $object = $modx->getObject('modResource', $id);
                $object->remove();
                $url = $modx->config['site_url'];
                $modx->sendRedirect($url);
                
                unset($object, $url);


                The resource deletion is made via the default resource form of any resource (my resources are not Collections or Articles, it's juste resources, 100% homemade by Modx). My point is that when we delete a resource from the "Delete" button on a resource, it makes an Ajax request, it's the way Modx handle it (it does not reload the page, the ajax request is send when we confirm deletion on the modal displayed after we clicked the "Delete" button on a resource).

                Here a short video of my screen that show the behavior : https://www.youtube.com/watch?v=iH70IvxEuf4

                What I think it's happening :

                • discuss.answer
                  • 3749
                  • 24,544 Posts
                  I see what you mean. I tried it and the plugin definitely fires and deletes the resource, but won't do the redirect. I've never seen sendRedirect() fail before. I tried OnBeforeDocFormDelete and OnDocFormDelete with no success.

                  This seems to work (connected to OnManagerPageBeforeRender), though I haven't tested it fully. It's kind of a goofy solution.

                  <?php
                  $docs = $modx->getCollection('modResource', array('deleted' => true));
                  
                  if (! empty($docs) ){
                     foreach( $docs as $doc) {
                         $doc->remove();
                     }
                     $url = $modx->config['site_url'];
                     $modx->sendRedirect($url);
                  }


                  I think this would redirect to the dashboard, but I haven't tested it.

                   $url = MODX_MANAGER_URL;
                    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
                    • 47212
                    • 40 Posts
                    Thanks, it does work ! But not when we use the right click for delete a resource (in the resource tree), but I think I can disable the "delete" option in the right-click menu, I will search something about that.

                    Anyway, I confirm that MODX_MANAGER_URL redirects on the dashboard.

                    Here is the full code, fired on the OnManagerPageBeforeRender event :
                    <?php
                    $docs = $modx->getCollection('modResource', array('deleted' => true));
                     
                    if (!empty($docs) ){
                       foreach( $docs as $doc) {
                           $doc->remove();
                       }
                       $url = MODX_MANAGER_URL;
                       $modx->sendRedirect($url);
                    }
                    
                    unset($docs, $doc, $url);
                      • 47212
                      • 40 Posts
                      I worked a little more on this subject and found a proper way to disable completely the trash system in Revo 2.4.2, taht works both with right click and resource form deletion, a bit different from the answers above.

                      Step 1 :
                      Create a new plugin "DeleteResources" that fires on the OnResourceDelete event :
                      <?php
                      $object = $modx->getObject('modResource', $id);
                      $object->set('deleted', '1');
                      
                      unset($object);


                      Step 2 :
                      Create a new plugin "RedirectAfterDeletion" that fires on the OnDocFormPrerender event :
                      <?php
                      if ($mode === 'upd') {
                          if ($resource->get('deleted')) {
                              $url = MODX_MANAGER_URL;
                              $modx->sendRedirect($url);
                          }
                      }
                      
                      unset($url);


                      Step 3 :
                      Create a new plugin "PurgeResources" that fires on the OnManagerPageAfterRender event :
                      $deletedResources = $modx->getCollection('modResource', array('deleted' => true));
                      if (!empty($deletedResources)) {
                          foreach ($deletedResources as $deletedResource) {
                                  $deletedResource->remove();
                          }
                      }
                      
                      unset($deletedResources, $deletedResource);
                      


                      Step 4 :
                      Create a new Manager template if not already done.
                      Add at the bottom of the "manager/templates/yourtemplate/css/index.css" file this line to prevent resources deleted from being displayed in the resources tree :
                      .x-tree-node > div.deleted { display: none; }


                      Step 5 :
                      Create a new access rule that you assign to the corresponding user group, and remove "purge_delete" permission.
                      Clear MODX cache and cache permissions after that.
                      This will hide the button that allows users to purge the deleted resources. That way, they will never see that there are deleted resources to purge.
                      See Bob's Guides Revolution Permissions for help about MODX access rules and permissions.

                      PS : I change the title of this post in "Disable/remove trash system in MODX Revolution." for a better SEO. [ed. note: romainfallet last edited this post 8 years, 5 months ago.]