We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 35322
    • 20 Posts
    Hi guys,

    I’d like to access properties about the parent of a page in the template. I’ve discovered [[*parent]], which kinda gives a link to the parent. However, it’s not perfect, as instead of getting "/parent/child", I get "/parent/4". Is there a way to get the correctly resolved SEO URL for the parent? Can I also get properties of the parent? For instance [[*parent.description]] or [[*parent.someTV]]???

    Thanks loads,
    Lee
      • 33968
      • 863 Posts
      Quote from: Lazarus404 at Jul 13, 2011, 09:13 AM

      I’d like to access properties about the parent of a page in the template. I’ve discovered [[*parent]], which kinda gives a link to the parent. However, it’s not perfect, as instead of getting "/parent/child", I get "/parent/4". Is there a way to get the correctly resolved SEO URL for the parent?
      How are you linking to the parent? This should output a friendly url for you:
      [[~[[*parent]]]]
      


      Can I also get properties of the parent? For instance [[*parent.description]] or [[*parent.someTV]]???
      Install the getResourceField extra, then do this:
      [[getResourceField? &id=`[[*parent]]` &field=`description`]]   // for parent description
      [[getResourceField? &id=`[[*parent]]` &field=`myTV` &processTV=`1`]]   // to get parent TV
      
        • 35322
        • 20 Posts
        Wonderful, thanks smiley I started looking into writing a snippet to do this, but I’ll always opt for the easy option, if it’s available ;-)

        Thanks again.

        Lee
          • 3749
          • 24,544 Posts
          In case you want a snippet (which would be somewhat faster and more efficient), this should help:

          <?php
          $parentObj = $modx->getObject('modResource', $modx->resource->get('parent'));
          
          $parentPagetitle = $parentObj->get('pagetitle');
          $parentPublished = $parentObj->get('published');
          // etc. for any field except content, use $parentObj->getContent() for that.


          You can set placeholders for all parent fields with this (though it may be slower because it will set a bunch of placeholders you don’t need):

          $modx->toPlaceholders($parentObj->toArray(), 'parent');


          With the above line, the placeholders will have the prefix ’parent.’ to avoid collisions, so they’d show with:

          [[+parent.pagetitle]]
          [[+parent.content]]
          
          etc.

            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
            • 33968
            • 863 Posts
            Quote from: BobRay at Jul 13, 2011, 07:05 PM

            You can set placeholders for all parent fields with this (though it may be slower because it will set a bunch of placeholders you don’t need):

            $modx->toPlaceholders($parentObj->toArray(), 'parent');

            In that case, I guess you could just use setPlaceholders to just output the ones you need:
            <?php
            $parentObj = $modx->getObject('modResource', $modx->resource->get('parent'));
            $modx->setPlaceholders(array(
               'pagetitle' => $parentObj->get('pagetitle'),
               'published' => $parentObj->get('published'),
            ),'parent.');
            


            I often use a similar technique which allows me to set a comma-delimited list of required placeholders in my snippet call, which is then exploded into an array and then fed to setPlaceholders as above. Works fine smiley
              • 22303 MODX Staff
              • 10,725 Posts
              FWIW, I would use...

              <?php
              if ($modx->resource->getOne('Parent')) {
                  $modx->toPlaceholders($modx->resource->Parent->get(array('pagetitle', 'published')), 'parent');
              }
              


              This would use the Parent relation if it was loaded during the request already (avoiding an additional query where possible), and you can use an array as the first parameter of the get() method to retrieve an array of specific attributes from the Parent object to pass to the toPlaceholders() method.
                • 33968
                • 863 Posts
                I love getting these kind of tips from you guys on the dev team. That looks super efficient... cheers Jason!
                  • 3749
                  • 24,544 Posts
                  I can’t believe I didn’t think of getOne(’Parent’). I’ve written about it dozens of times. I guess I need more sleep. tongue
                    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