We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 23018
    • 353 Posts
    Perhaps I have missed a simpler solution, but this is how I merge a dozen MIGx grids into a single grid.

    1. Get the JSON content of all grids with getresources:

    [[!getResources? 
    &parents=`21` 
    &tpl=`@INLINE [[+MIGx - Items]]`
    &includeTVs=`1` 
    &processTVs=`1` 
    &tvPrefix=`` 
    &showUnpublished=`1` 
    &showHidden=`1` 
    &limit=`99` 
    &toPlaceholder=`items` 
    &sortby=`menuindex` 
    &sortdir=`ASC`
    ]]


    Now we have the JSON content of several grids inside a single chunk. Before we can further process these values we have to stich them together and give each result a unique id.

    2. Use the mergeJSON Snippet to fix things.

    <?php
    // mergeJSON Snippet:
    // Usage: [[mergeJSON? &json=`[[+items]]` ]]
    $fixJSON = !empty($json) ? $json : "";
    
    // Remove carriage returns and line-breaks
    $fixJSON = str_replace( array("\r\n", "\r", "\n"), ' ', $fixJSON );
    
    // Stich individual JSON strings together by replaceing "] "[ with ","
    $fixJSON = str_replace( array("] [") , "," , $fixJSON);
    
    // Convert JSON into array
    $decode = json_decode($fixJSON,true);
    
    // Overwrite MIGS_id with unique numbers
    if(is_array($decode)) {
        $MIGX_id = 0;
        foreach($decode as $key => $value) {
            $MIGX_id = $MIGX_id + 1;
            $decode[$key][MIGX_id] = $MIGX_id;
        }
    // Convert array to JSON string and return result
        return json_encode($decode);
    }
    else {return;}


    After we have fixed our JSON code, we can move on.

    3. Now we use getImageList to output the whole thing. Instead of docid and tv name we use the value parameter to pass the JSON code returned by mergeJSON.

    [[!getImageList?
        &value=`[[mergeJSON? &json=`[[+items]]` ]]`
        &where=`{ "name:=" : "Ajax" }`
        &tpl=`MIGx items tpl`
        &sortBy=`name`
        &sortDir=`ASC`
    ]]


    Note: sortyBy and sortDir are custome features I have added to my personal getImageList snippet. They are not available for the standard snippet. [ed. note: pepebe last edited this post 14 years, 5 months ago.]
      Homepage: pepebe.de | MODX snippets (and other stuff) at github: https://gist.github.com/pepebe
      • 23018
      • 353 Posts
      I just noticed that under certain circumstances an imagepath will be broken If you use this solution. I'll post a fix for this problem as soons as I have the time to cook something up.
        Homepage: pepebe.de | MODX snippets (and other stuff) at github: https://gist.github.com/pepebe
        • 4172
        • 5,888 Posts
        did you see this one (a little mergeMIGX - snippet, which merges MIGX-items of multiple resources to one array):

        https://forums.modx.com/thread/41006/migx---multiitemsgridtv-for-modx---revolution?page=18#dis-post-395927

        but with that much items, collected from different resources and sorted by php, don't you think, it would be better to have them in a custom-table and use MIGXdb instaed?

          -------------------------------

          you can buy me a beer, if you like MIGX

          http://webcmsolutions.de/migx.html

          Thanks!
          • 23018
          • 353 Posts
          Quote from: Bruno17 at May 20, 2012, 12:28 PM
          did you see this one (a little mergeMIGX - snippet, which merges MIGX-items of multiple resources to one array):

          https://forums.modx.com/thread/41006/migx---multiitemsgridtv-for-modx---revolution?page=18#dis-post-395927

          Sorry, but I can't find the mentioned snippet in this part of the thread.

          Quote from: Bruno17 at May 20, 2012, 12:28 PM

          but with that much items, collected from different resources and sorted by php, don't you think, it would be better to have them in a custom-table and use MIGXdb instaed?

          At the moment fetching data from 9 Documents with a total of 160 items as well as stiching and fixing the results takes much less than a second.

          The majority of the workload is done by getImageList which does a wonderful job templating the results. For all 160 items and a fairly complex template it only takes another 2 seconds. Much less if I reduce the number of results with a where condition.

          Regarding MIGXdb I have to admint that I haven't used it any of my projects so far. I expect this to change in the near future, but this is another story...
            Homepage: pepebe.de | MODX snippets (and other stuff) at github: https://gist.github.com/pepebe
            • 4172
            • 5,888 Posts
            Sorry, but I can't find the mentioned snippet in this part of the thread.

            its on page 18 reply 175

              -------------------------------

              you can buy me a beer, if you like MIGX

              http://webcmsolutions.de/migx.html

              Thanks!
              • 23018
              • 353 Posts
              Quote from: Bruno17 at May 20, 2012, 03:34 PM
              Sorry, but I can't find the mentioned snippet in this part of the thread.

              its on page 18 reply 175


              Oh, I get it. On page 18 I only see reply 191 to 200. I've sorted the answers the other way around, so the latest posts of each thread are always on page one. As a result reply 175 for me it is right in the middle of page 20. Sorry forgot about that.

              Tomorrow I'll have a look at your snippet and see how it fits into my work. Sometimes I wonder if there is anything I can think of you haven't done before...

              Again, thanks for your patience smiley

              pepebe [ed. note: pepebe last edited this post 14 years, 4 months ago.]
                Homepage: pepebe.de | MODX snippets (and other stuff) at github: https://gist.github.com/pepebe
                • 40045
                • 534 Posts
                I still have problems with a similar scenario (I have several MIGX grids in one resource though, and one is within another...MIGX in MIGX) and I would like to know if the mergeJSON snippet would be able to merge these grids because the mergeMIGX snippet (see below) seems "only" to be able to merge the same grid but form different resources.

                <?php
                // copied from here https://forums.modx.com/thread/?thread=41006&page=20
                // [[mergeMIGX? &tvname=`mymigxtv` &docids=`10,11,12` &tpl=`mytpl` &debug=`1`]]
                 
                $docids = explode(',', $docids);
                if ($tv = $modx->getObject('modTemplateVar', array('name' => $tvname))) {
                    $allitems = array();
                    foreach ($docids as $docid) {
                        $outputvalue = $tv->renderOutput($docid);
                        $items = $modx->fromJSON($outputvalue);
                        if (is_array($items)) {
                            $allitems = array_merge($allitems, $items);
                        }
                        elseif ($debug) {
                            echo 'Fehler in '.$docid.':'.$outputvalue;
                        }
                 
                    }
                    $scriptProperties['value'] = $modx->toJSON($allitems);
                }
                 
                return $modx->runSnippet('getImageList', $scriptProperties);
                ?>
                


                I would like to merge different grids within one resource to one JSON string and thus maybe able to bypass problems I have with nested getImageList calls...(questionmark problem https://github.com/Bruno17/MIGX/issues/52 and performance are the main ones...), you think this is possible with the mergeJSON snippet? What I dont get is what pepebe is doing with the getResources Call, why is the MIGX grid TV called as tpl there?

                I have a structure like this

                resource(1)
                    MIGX-TV(outer)
                        somenormalTVs
                        MIGX-TV(inner)
                            somenormalinnerTVs
                        someothernormalTVsinoutermigxtv
                


                So I would need to merge the TV values of the outer MIGX TV with the TV Values of the inner MIGX TV^^...I'm just not so good in php to see through that jungle...any ideas how to do that?
                  • 4172
                  • 5,888 Posts
                  I will try to reproduce your scenario these days, or better, would you have manager-access for me?
                    -------------------------------

                    you can buy me a beer, if you like MIGX

                    http://webcmsolutions.de/migx.html

                    Thanks!
                    • 36926
                    • 701 Posts
                    @exside did you find a solution for the Question mark bug as i've just come across this. Seems to be only an issue on a nested migx call.

                      • 40045
                      • 534 Posts
                      Not really, have a look at https://github.com/Bruno17/MIGX/issues/52 and the part with
                      :replace='$$==?'
                      ...it's just an ugly workaround