We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 11838 ☆ A M B ☆
    • 79 Posts
    Hello i'm on revo 2.2 and i'd like to find a way to automatically tick parent container checkbox when a subpage is created. Is there a way to do that ? And then untick this checkbox when there is no subpage anymore. Anyone has an idea ?
      • 3749
      • 24,544 Posts
      It would be a somewhat complex plugin because when you delete or undelete a resource, the children are also deleted or undeleted. The plugin could be attached to OnDocFormSave (with a check for $mode == modSystemEvent::MODE_NEW) and some of the resource delete/undelete events OnResourceDelete OnResourceUndelete.

      When resources are created, deleted, or removed in code, though, they may or not be caught depending on how the developer has written the code.
        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
        • 11838 ☆ A M B ☆
        • 79 Posts
        Ok my approach might not be the good one. In fact i use the same js as modx flexibility to display a little arrow on the first level page when there is a subpage. And then i use firstchildredirect to redirect the first level page to his first subpage. The arrow display only if the container's checkbox is ticked. I would like to make It user friendly by automatically display the arrow and rederict to the first child without ticking the container's checkbox. Anyone has an idea ?
          • 3749
          • 24,544 Posts
          This plugin connected to OnDocFormSave, OnResourceDelete, OnResourceDuplicate, and OnResourceUndelete would do most of what you want (untested):

          <?php
          /* SetContainerStatus Plugin */
          
          /* Connect to OnDocFormSave, OnResourceDelete, onResourceDuplicate, and OnResourceUndelete */
          /* @var $modx modX */
          /* @var $parent modResource */
          /* @var $newParent modResource */
          /* @var $doc modResource */
          
          switch ($modx->event->name) {
              case 'OnDocFormSave':
          
                  $children = $resource->getMany('Children');
                  if (!empty($children)) {
                      $resource->set('isfolder', true);
                      $resource->save();
                  }
                  $parent = $resource->getOne('parent');
          
                  if ($parent) {
                      $parent->set('isfolder', true);
                      $parent->save();
                  }
                  break;
          
              case 'OnResourceDelete':
                  /* get parent object (if any) */
                  $parent = $resource->getOne('Parent');
                  if ($parent) {
                      /* check for siblings */
                      $children = $parent->getMany('Children');
                      if (empty($children)) {
                          $parent->set('isfolder', false);
                          $parent->save();
                      }
                  }
                  break;
          
              case 'OnResourceUnDelete':
                  $parent = $resource->getOne('Parent');
          
                  if ($parent) {
                      if (! $parent->get('isfolder')) {
                          $parent->set('isfolder', true);
                          $parent->save();
                      }
                  }
                  break;
              case 'OnResourceDuplicate':
                  $newChildren = $newResource->getMany('Children');
                  if (!empty($newChildren)) {
                      $newResource->set('isfolder', true);
                      $newResource->save();
                  }
                  $newParent = $newResource->getOne('Parent');
                  if ($newParent) {
                      if (! $newParent->get('isfolder')) {
                          $newParent->set('isfolder', true);
                          $newParent->save();
                      }
                  }
          
                  break;
          }


          Here's a snippet to set all existing resources. You could run it periodically to fix anything the plugin above misses:

          <?php
          /* Utility snippet to set container status for all existing resources */
          
          $docs = $modx->getIterator('modResource');
          
          foreach ($docs as $doc) {
              $children = $doc->getMany('Children');
          
              if ($children) {
                  if (! $doc->get('isfolder')) {
                      $doc->set('isfolder', true);
                      $doc->save();
                  }
              }
          }



          Note that the plugin will not catch changes due to dragging things around in the tree. I think there's an event for that, but I couldn't find it.

          Let me know if it works. If it does, I'll release it as an extra.
            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
            • 36996
            • 211 Posts
            Is this for a Wayfinder menu?
            If yes then you can use &parentRowTpl.
              • 11838 ☆ A M B ☆
              • 79 Posts
              Thank you BobRay (sorry for the late reply) it works, the only thing is that when i delete all subpages i'd like my parent page to be normal again (see attachement). Thx !
                • 3749
                • 24,544 Posts
                <?php
                /* Utility snippet to set container status for all existing resources */
                 
                $docs = $modx->getIterator('modResource');
                 
                foreach ($docs as $doc) {
                    $children = $doc->getMany('Children', array('deleted' => 0));
                    $setting = ! empty($children)? true : false;
                    if ($setting != $doc->get('isFolder')) {
                        $doc->set('isFolder', $setting);
                        $doc->save(); 
                    }
                    unset($children);
                }


                :)


                This could be speeded up quite a bit if you have a big site and it runs slowly. Let me know.
                [ed. note: BobRay last edited this post 13 years, 6 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
                  • 11838 ☆ A M B ☆
                  • 79 Posts
                  It breaks the website
                    • 3749
                    • 24,544 Posts
                    Sorry, missing closing parentheses -- the bane of my existence. My code editor puts them in for me, so whenever I type code without it, I miss at least one.

                    Fixed above.
                      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
                      • 11838 ☆ A M B ☆
                      • 79 Posts
                      On more time thanks, but my parent page is still considerate as a container.