We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 16430
    • 217 Posts
    still doesnt work...
      • 3749
      • 24,544 Posts
      OK, this works (tested). I tweaked it a little to only change the parent's hidemenu field if the child is published and not hidden. It also works on Quick Create:

      /* only act on published, non-hidden, non-root docs */
      if ($resource->get('parent') && $resource->get('published') && (!$resource->get('hidemenu'))) {
          $parentObj = $resource->getOne('Parent');
          if ($parentObj) {
              /* turn off parent's hidemenu field if set */
              if ($parentObj->get('hidemenu')) {
                  $parentObj->set('hidemenu', 0);
                  $parentObj->save();
              }
          }
      }
        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
        • 16430
        • 217 Posts
        This works perfect! Thank you, thank you, thank you....
          • 16430
          • 217 Posts
          But... found a problem. This only works if I create new resource via manager. If I create resource via snippet formit2resource. This doesnt work. There must be something missing, because if parent for new resource is set something that is not folder, in manager it automatically set isfolder=1 but snippet formit2resource doesnt. So it store new resource under specific parent but this parent has still set isfolder=0

          Problem No.2: This plugin set to hidden=0 only direct parent of created resource. But in structure: folder-subfolder-child, "folder" stays hidden... [ed. note: kudykam last edited this post 13 years, 7 months ago.]
            • 3749
            • 24,544 Posts
            For the first problem, you have several options
            1. Put a copy of the plugin code in formit2resource (modified for a snippet)
            2. Add code invoking OnDocFormSave in formit2resource, something like this:

            $modx->invokeEvent('OnDocFormSave',array(
                'mode' => modSystemEvent::MODE_NEW,
                'id' => $resource->get('id'),
                'resource' => & $modx->resource,
            ));


            3. Refactor formit2resource to use the resource/create processor

            4. Use NewsPublisher to create the resources.


            For the second problem, you'd want to either have a recursive function to set the ancestor's hidemenu fields or something like this (untested)


            /* only act on published, non-hidden, non-root docs */
            if ($resource->get('parent') && $resource->get('published') && (!$resource->get('hidemenu'))) {
            
                $ids = $modx->getParentIds($resource->get('id'));
                if (!empty($ids)) {
                    foreach ($ids as $id) {
                        if ($id) {
                            $doc = $modx->getObject('modResource', $id);
                            if ($doc->get('hidemenu')) {
                                $doc->set('hidemenu', 0);
                                $doc->save();
                            }
                        }
                    }
                }
            }
            
              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
              • 16430
              • 217 Posts
              First I tried new plugin and created resource via manager (which works with the old plugin) into folder->subfolder (both hidden) but nothing happened.

              So I reverted it back to old plugin and put a invoke code into formit2resource snippet. Have to changed it to:

              $modx->invokeEvent('OnDocFormSave',array(
                  'mode' => modSystemEvent::MODE_NEW,
                  'id' => $doc->get('id'),
                  'resource' => & $modx->resource,
              ));


              Using DOC instead of RESOURCE, to make it run without server error. But still it didnt changed the parent.
                • 3749
                • 24,544 Posts
                I don't think you want to set resource to $modx->resource, that's not your new resource.

                You might try 'resource-> & $doc,


                Actually, I think the best approach would be to leave out the invokeEvent() code find the save() code in formit2resource and change it to something like this (where $fields holds the resource fields):


                $modx->runProcessor('resource/create', $fields);



                The create processor will invokd OnDocFormSave for you.
                  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
                  • 16430
                  • 217 Posts
                  Ok thats super complicated for me...
                  I tried, but get server error, somethin must be wrong..
                  Including my upload snippet (combinatinon of formit2resource and formit2file)

                  [ed. note: kudykam last edited this post 13 years, 7 months ago.]
                    • 3749
                    • 24,544 Posts
                    Yes, it's difficult if you're not experienced in PHP code. I think it was written before the MODX processors were fully implemented.

                    The code would have to be rewritten, but we're pretty far past the amount of time I can afford to put in helping for free. wink
                      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
                      • 16430
                      • 217 Posts
                      I dont know why , but the code in reply#25 doesnt generate list of parent of current id. Only direct parent of current id. getParentIds returns empty array. So my working workaround is this...

                      if ($doc->get('parent')) {
                          
                          $res = $modx->getObject('modResource', $doc->get('parent'));
                                      if ($res->get('hidemenu')) {
                                          $res->set('hidemenu', 0);
                                          $res->save();
                      					}
                          //set only first parent above actual parent
                          $ids = $modx->getParentIds($doc->get('parent'));
                          if (!empty($ids)) {
                                      $res = $modx->getObject('modResource', $ids[0]);
                                      if ($res->get('hidemenu')) {
                                          $res->set('hidemenu', 0);
                                          $res->save();
                                      }
                          }
                      }

                      Its not really optimized but works...