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

    I was looking for a way to find a document’s path relative to the site url. So, for example, I have a document 3 deep and I want to prepend it’s parent’s alias’s, and pass it through a chunk for display. I somehow had this working, but then the MODx install got corrupted and I had to do this portion over. So, I’m guessing there is an easier way to do this.

    I created a snippet to find documents based on a TV and have the results gathered through this while loop:
      while($row = $modx->db->getRow( $results )) {
        $alias = $row['alias'];
        $pagetitle = $row['pagetitle'];
        $path = getDocumentPath($alias,$row['id']);
        $output .= $modx->parseChunk($tpl, array('alias' => $alias, 'pagetitle' => $pagetitle), '[+', '+]'  );
      }
    


    Here is the getDocumentPath function:
    function getDocumentPath(&$path,$docId, $separator = '/') {
      global $modx;
      $parent = $modx->getParent($docId, 1, 'alias,parent,id');
      $path = $parent['alias'] . $separator . $path;
      if($parent['parent'] != 0) {
        if(getDocumentPath($path,$parent['id'])) return true;
      }
      else {
        return true;
      }
    }
    


    I was just curious if this was already built into MODx, if something out there already exists to do this (non-core MODx), or if someone else needs a similar function to do the same thing.

    Cheers,
    Shawn
      • 26966
      • 2 Posts
      A co-worker reminded me of how I did this originally. In my chunk I had the id as [~[+id+]~]. Oh well, maybe someone will find this function useful.

      Shawn
        • 7231
        • 4,205 Posts
        [~id~] (or [~[+id+]~] which would be the way to use it in a snippet tpl) is the syntax for modx to create an internal link. The cool thing is that the link is created according to your SEO URL settings.
          [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

          Something is happening here, but you don't know what it is.
          Do you, Mr. Jones? - [bob dylan]
          • 22303 MODX Staff
          • 10,725 Posts
          A more efficient way to get the path in snippet or plugin code would be using $modx->aliasListing. Something like this will avoid any additional queries to the database:
          <?php
          function getDocumentPath($docId) {
          $path = ’’;
          $docId = intval($docId);
          $exists = array_key_exists($docId, $modx->aliasListing);
          if ($exists) {
          $path = $modx->aliasListing[$docId][’path’];
          }
          return $path;
          }
          ?>