We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 28215
    • 4,149 Posts
      shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
      • 23571
      • 223 Posts
      That is exactly what I wanted!



      Any chance this could be put into evo? We are concerned about our upgrade path in the future since we have about 60 sites on evo right now.
        • 23571
        • 223 Posts
        I actually got the plugin functional, but I don’t think the code is very efficient. For some reason I can’t get $modx->getParentIds or $modx->getParent to return anything except empty arrays, even if I hardcode a known resource ID into it.

        So I ended up doing this, which I"m sure is inefficient but it works.

        <?php
        global $modx;
        $id = $resource->get("id");
        $parent = $resource->get(’parent’);
        $depth = 1;
        while($parent != 1) {
        $r1 = $modx->getObject(’modResource’,$parent);
        $parent = $r1->get(’parent’);
        $depth++;
        }

        if ($depth > 3) {
        $modx->event->output("The dwarves dug too deep and awoke something in the dark!");
        }
        ?>
          • 3749
          • 24,544 Posts
          Quote from: pleth at Sep 07, 2010, 05:17 PM

          I actually got the plugin functional, but I don’t think the code is very efficient. For some reason I can’t get $modx->getParentIds or $modx->getParent to return anything except empty arrays, even if I hardcode a known resource ID into it.

          So I ended up doing this, which I"m sure is inefficient but it works.

          <?php
          global $modx;
          $id = $resource->get("id");
          $parent = $resource->get(’parent’);
          $depth = 1;
          while($parent != 1) {
          $r1 = $modx->getObject(’modResource’,$parent);
          $parent = $r1->get(’parent’);
          $depth++;
          }

          if ($depth > 3) {
          $modx->event->output("The dwarves dug too deep and awoke something in the dark!");
          }
          ?>

          $modx->getParentIds() has always worked for me. Are you sure you spelled it right? It’s easy to do it as getParentIDs by mistake.
            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
            • 23571
            • 223 Posts
            I don’t know why but for whatever reason I have tried both of these:


            This does nothing.
            <?php
            
            global $modx;
            $id = $resource->get("id");  
            $out = count($modx->getParentIds($id));
            $modx->event->output($out);
            
            ?>


            This outputs "Array()"
            <?php
            
            global $modx;
            $id = $resource->get("id");  
            $out = print_r($modx->getParentIds($id), true);
            $modx->event->output($out);
            
            ?>

              • 28215
              • 4,149 Posts
              You don’t need the global $modx command there.

              This might be related to access permissions for the context. A wild guess, but try this:
              $id = $resource->get('id');  
              $modx->switchContext($resource->get('context_key'));
              $out = print_r($modx->getParentIds($id),true);
              $modx->switchContext('mgr');
              $modx->event->output($out)
              
                shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
                • 23571
                • 223 Posts
                That works perfectly.

                I couldn’t find much explanation on the switchContext function and why it is needed in this instance.

                (I did look over this http://rtfm.modx.com/display/revolution20/modX.switchContext , but I don’t have a good grasp on our use of it here)

                Can you explain a couple things?

                1. Why does global $modx not need to be declared? Does the eval have scope like an anonymous function (closure) and since $modx and $resource are already defined before the invokeEvent is called, it has scope to see them?

                2. Why do we have to switch context, and how does that change the output of getParentIds and getParent.


                You guys have been awesome and I really appreciate the help that the modx community provides.

                  • 28215
                  • 4,149 Posts
                  Quote from: pleth at Sep 08, 2010, 09:26 AM

                  1. Why does global $modx not need to be declared? Does the eval have scope like an anonymous function (closure) and since $modx and $resource are already defined before the invokeEvent is called, it has scope to see them?
                  Well, it’s already done in the cached Element:
                  elements_modsnippet_110($scriptProperties= array()) {
                  global $modx;
                  if (is_array($scriptProperties)) {
                  extract($scriptProperties, EXTR_SKIP);
                  /* code of element here */
                  }


                  So you already have scope of $modx. Doing it in your snippet is redundant.

                  2. Why do we have to switch context, and how does that change the output of getParentIds and getParent.
                  Well, Access Permissions are context-specific, and you probably don’t have access to browse ’web’ Resources while in the ’mgr’ Context. The switchContext loads the correct permissions so that can be done.
                    shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com