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

Answered getTree

    • 44649
    • 68 Posts
    Hi,

    I'm trying to write a small plugin, the event is being fired but isn't returning anything I can work with
    $c = $modx->getTree(0, 10);
    print_r($c, true); // returns empty

    The same code as a snippet works as expected and returns the correct amount of resources, is there something I'm missing?

    Thanks

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

      • 5430
      • 247 Posts
      Try sending the result to the error log instead of print_r

      Don't think print_r produces on-screen results from plugins, or at least that's been my experience.
        • 3749
        • 24,544 Posts
        When you use true as the second argument, print_r() returns the value directly rather than displaying the output.

        You need to return it yourself from the snippet:

        return print_r($c, true);


        or (in case you want to do something with the output before returning it):

        $output = print_r($c, true);
        return $output;
        
          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
          • 44649
          • 68 Posts
          Thanks for the reply, I've tried:
          $c = $modx->getTree(0, 10);
          $modx->log(modX::LOG_LEVEL_DEBUG, '[mySnippet] was called with the following properties: ' . print_r($c, true)); //returns nothing


          and
          $c = $modx->getTree(0, 10);
          return print_r($c, true);

          returns
          \core\model\modx\modx.class.php : 1611) [OnBeforeDocFormSave]Array
          (
          )
          


          When I did this is a snippet it returns an array as expected
          Array ( [1] => Array ( [11] => 11 [8] => 8 [5] => 5 [3] => 3 [9] => 9 [10] => Array ( [7] => 7 [2] => 2 [15] => 15 ) [4] => 4 ) [6] => 6 [13] => 13 [14] => 14 [16] => 16 [17] => 17 [18] => 18 [19] => 19 [20] => 20 [21] => 21 [22] => 22 [23] => 23 [24] => 24 [25] => 25 [26] => 26 [27] => 27 [28] => 28 [29] => 29 [30] => 30 [31] => 31 [32] => 32 [39] => 39 [33] => 33 [40] => 40 [34] => 34 [41] => 41 [35] => 35 [42] => 42 [36] => 36 [43] => 43 [37] => 37 [44] => 44 [38] => 38 [45] => 45 [46] => 46 [47] => 47 [48] => 48 [49] => 49 [50] => 50 [51] => 51 )

          which I'm then counting, and ideally want to limit the amount of resources someone can make. This is confusing me greatly, is there any difference/limitations between coding in snippets and plugins with php?
          • discuss.answer
            • 3749
            • 24,544 Posts
            Yes, you can't use the return value of a plugin (though the plugin still needs a return statement). It's essentially thrown away.

            If you use echo, print, or print_r() (with one argument), you may see output, though it's difficult to control its placement. In other cases, the output won't appear of the plugin will just crash.

            Depending on the System Event involved, you can sometimes set one or more placeholders in the plugin, for example in OnWebPagePrerender. You can also modify the content of a resource in OnWebPagePrerender. Plugins are not really designed to produce output. They're mainly designed to modify MODX's behavior behind the scenes. The 'Prerender' events are an exception to this.
              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
              • 44649
              • 68 Posts
              Thanks for the info Bob, I've managed to achieve what I was after using
              $c = $modx->getChildIds(0,$set,array('context' => 'web'));


              I'm not sure why but $modx->getTree(0,10); just wouldn't return anything, possibly linked to this https://github.com/modxcms/revolution/issues/8430

              Anyways, all working now! Thanks again smiley
                • 3749
                • 24,544 Posts
                I'm glad you got it sorted. smiley
                  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