We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 10107
    • 24 Posts
    I’ve been struggling with this one all day, I decided to try and write a snippet to call the getResources snippet and it worked except it returned the 2 expected results as a pre-formatted string representation of the actual arrays that I need?

    All I need is to work out how to return an actual array with modx->runSnippet(’getResources’, $params)

    for example this is a small part of what I get when I use print_r:


    <pre>Array
    (
    [tpl] =>
    [tplOdd] =>
    [tplFirst] =>
    [tplLast] =>
    [sortby] => menuindex
    [sortbyAlias] =>
    [sortbyEscaped] => 0
    [sortdir] => ASC
    [limit] => 0
    [offset] => 0
    ...
      • 6228
      • 249 Posts
      Within the ’getResources’ snippet, set its output as:
      return base64_encode(serialize($output));


      And then you should be able to restore the array like so:
      $output = $modx->runSnippet('getResources', $params);
      $output = unserialize(base64_decode($output));
        lo9on.com

        MODx Evolution/Revolution | Remote Desktop Training | Development
        • 10107
        • 24 Posts
        Thanks just one question will this effect where I have used the snippet using the normal snippet call method?
          • 6228
          • 249 Posts
          Yes it will.

          What you can do is add an array key to the output and call it something like ’_encoded’, then assign the serialized output data to it. That way you still have the original string, with an extra array key (that will be ignored if a snippet doesn’t need it) -- but with a key and value you can decode (if your snippet needs it). Here’s an example:

          From your ’getResources’ snippet, add the additional array key:
          $output['_serialized'] = '|' . base64_encode(serialize($output)) . '|'; // pipes will help extract the data later

          The snippet output will be the same, with an additional array key:
          Array
          (
          [tpl] =>
          [tplOdd] =>
          [tplFirst] =>
          [tplLast] =>
          ...
          [_serialized] => (here is where your serialized data is)
          ...
          )
          And then in another snippet, you can read the serialized data like so:
          $output = $modx->runSnippet('getResources', $params);
          $output = explode('|', $output); // extract serialized data
          $output = unserialize(base64_decode($output[1])); // expand data


          I don’t know exactly how your snippets work, so you may have to tailor this to your snippet code. I hope it helps.
            lo9on.com

            MODx Evolution/Revolution | Remote Desktop Training | Development
            • 3440
            • 17 Posts
            Hi,

            I encountered the same problem trying to build a publishedon time based menu...

            cyclissmo’s solution set me on the right track but needed some modifications to function properly, here is the final code I came up with and that works for me:

            Please note that this was applied on getResources v 1.2.0-pl

            1- Modify the getResources snippet code, adding the following line at line 256 (in v1.2.0-pl):
            $properties['_serialized'] = '|' . base64_encode(serialize($properties)) . '|'; // pipes will help extract the data later


            To locate this in the getResources code, here is the resulting chunk (lines 255 to 262)
            if (empty($resourceTpl)) {
                    $properties['_serialized'] = '|' . base64_encode(serialize($properties)) . '|'; // pipes will help extract the data later
                    $chunk = $modx->newObject('modChunk');
                    $chunk->setCacheable(false);
                    $output[]= $chunk->process(array(), '<pre>' . print_r($properties, true) .'</pre>');
                } else {
                    $output[]= $resourceTpl;
                }


            You probably noticed that the serialized data will only be made available when no template is specified in the getResources call parameters, which seems to be a logical behavior since recovering the raw data is opposed to templating it.

            2- create your own snippet code with the following code to recover an array of the complete getResources data:
            $gR_output = $modx->runSnippet( 'getResources', array(
                  parents => '2,3,4,5,6,7,8,9,10,11',
                  limit => '1000',
                  sortby => 'publishedon',
                  hideContainers => '1'
                  /* adapt those parameters to your need of course ;-) */
                ));
            
              $gR_output = explode('|', $gR_output); // extract serialized data
              foreach( $gR_output as $item ) {
                $unserialized = unserialize( base64_decode( $item )); // expand data
                if ( is_array( $unserialized )) {
                  $gR_array[] = $unserialized;
                }
            
              /* add your custom code logic here */
              }



            You can get an output of what is inside the $gR_array like this:
            echo ( '<pre>' . print_r( $gR_array, true ) . '</pre>' );


            Hope it can help someone...

            Cheers.