We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 21419
    • 84 Posts
    Hi folks. I’m having trouble trying to figure out how to determine the id of a child document.

    My basic structure is:

    Our Restaurants
    Restaurant Name
    Restaurant Locations (all)
    Location A
    Location B
    Location C
    Restaurant Name
    Restaurant Name

    On "Restaurant Name" I am pulling the data from the three individual locations, but want the link for each of those locations to go to "Restaurant Locations (all)". I have everything working if I put the ID of "Restaurant Locations (all)" into my Ditto string. But I want to make it dynamic. I’ve been all over the place with Ditto and Ultimate parent and just can’t figure out how to get that child element.

    The actual code I’m using is

    [!Ditto?parents=`62` &depth=`2` &display=`all` &orderBy=`ASC` &tpl=`locations-some`!]


    Where 62 is the ID of "Restaurant Locations (all)". There are going to be multiple instances of this page (all with different parents) so I don’t want to have to set up a bunch of hard-coded elements.

    I know this can’t be that hard, but my brain is completely knotted.

    Thanks.
      • 20413
      • 2,877 Posts
      If I understand you:
      In your locations-some tpl
      link to parent like this:
      <a href="[~[+parent+]~]">

        @hawproductions | http://mrhaw.com/

        Infograph: MODX Advanced Install in 7 steps:
        http://forums.modx.com/thread/96954/infograph-modx-advanced-install-in-7-steps

        Recap: Portland, OR (PDX) MODX CMS Meetup, Oct 6, 2015. US Bancorp Tower
        http://mrhaw.com/modx_portland_oregon_pdx_modx_cms_meetup_oct_2015_us_bancorp_tower
        • 21419
        • 84 Posts
        I am doing that.

        What I’m trying to do is replace the parents=`62` with parents=ID of "Restaurant Locations (all)", which is the child of "Restaurant Name" and the parent of "Location A, B, C".

        Does that make sense?
          • 20413
          • 2,877 Posts
          Restaurant Name (61) -> here you have the ditto listing of 71-73!
          --Restaurant Locations all (62)
          ----Location A (71)
          ----Location B (72)
          ----Location C (73)


          If you use on "Restaurant Name" (61):
          [!Ditto?parents=`[*id*]` &filter=`pagetitle,all,7` &tpl=`locations-some` ...

          It should exclude "Restaurant Locations all" (62) !untested!

          Then have another Ditto listing on "Restaurant Locations all (62)" with another tpl -> [~[+id+]~]
          ...and no need to filter on that one.
            @hawproductions | http://mrhaw.com/

            Infograph: MODX Advanced Install in 7 steps:
            http://forums.modx.com/thread/96954/infograph-modx-advanced-install-in-7-steps

            Recap: Portland, OR (PDX) MODX CMS Meetup, Oct 6, 2015. US Bancorp Tower
            http://mrhaw.com/modx_portland_oregon_pdx_modx_cms_meetup_oct_2015_us_bancorp_tower
            • 21419
            • 84 Posts
            That won’t work because it’s still dependent on my knowing which specific IDs to filter. I want this to be able to be applied to 4 different restaurants, all with different locations. So the first Ditto string from the "Restaurant Name" level needs to be able to choose the child document - The Locations (All) page, which is the parent of the Location page - without knowing its ID.

            [!Ditto?parents=`ID of Child Document` &depth=`2` &display=`all` &orderBy=`ASC` &tpl=`locations-some`!]


            It just seems that there must be a method for determining that ID without having to hard code the ID #.

            Isn’t there a way to get a list of all children and then filter by level? In this particular case, I’d imagine it would be GetChild&level=`1` or something along those lines (if that function actually existed).
              • 21419
              • 84 Posts
              Okay. I’m getting closer, but my PHP is crap.

              I’ve created a snippet called GetChild which looks like this
              <?php
                  $thisid=$modx->documentIdentifier;
                  $children= $modx->getChildIds($thisid, 1);
                  return print_r($children);
              ?>
              


              It returns:
              [[Ditto?parents=`Array ( [62] => 62 ) 1` &depth=`3` &orderBy=`ASC` &tpl=`locations-some`]]
              


              So it is finding the right child ID, ’62’ in this case, but I have no idea how to get this array to return the number ’62’ instead of "Array ( [62] => 62 ) 1". And I’m not sure where the 1 is coming from. I know I used it defining the $children variable, but I thought that was to set a limit. Without it, or with a value of 0, it returns all of the child documents.
                • 4172
                • 5,888 Posts
                Do you have this locations-folder in each of your restaurant-folders?
                You can try this:

                <?php
                    $childids =  $modx->getChildIds($modx->documentObject['id'], 1);//
                    
                    if (count($childids)>0){
                        foreach ($childids as $childid){
                        return $childids;//return the first childID
                        }
                    }
                    return 0;
                ?>
                  -------------------------------

                  you can buy me a beer, if you like MIGX

                  http://webcmsolutions.de/migx.html

                  Thanks!
                  • 24495
                  • 407 Posts
                  If Location X are the last items (i.e. documents and not folders) try this
                  [[Ditto?&depth=`2` &hideFolders=`1` &display=`all` &tpl=`locations-some`]]

                  No need to use parents parameter, Ditto uses the current document as parent.
                    • 21419
                    • 84 Posts
                    Bruno - thanks, that seems right, but it returns 0 instead of the ID.

                    Rasc - The current document is not the parent. The child of the current document is the parent of the documents I’m pulling the data from.

                    The Locations folder is inside of the Restaurant folder. I am trying to pull data from document inside the Locations folder from the Restaurant folder without having to hard code the id of the Locations folder.

                    Bruno has that correct. I’m just not sure why his php is returning 0 instead of the actual ID.
                      • 26931
                      • 2,314 Posts
                      anonymized-26931 Reply #10, 17 years ago