We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 42560
    • 49 Posts
    Hello, if i have a symlinks resource in my site, how to find the original version? and viceversa

    i mean , in order to create a symlink, you first need to create the original(modDocument) version.

    i need would like to find from a symlink, the original resource. :/

    and from and original document, all the symlinks created from that original.
    is there any table that i can find all symlinks from an original resource?


    thanks for your help.

    This question has been answered by BobRay. See the first response.

      • 42560
      • 49 Posts
      Quote from: jacielluve at Aug 03, 2018, 05:13 PM


      i would like to find from a symlink, the original resource. symlinks have [[*content]] id, so i guess this id is the original document XD

      thanks for your help.
      • discuss.answer
        • 3749
        • 24,544 Posts
        Yes, that's correct. There is no table connecting symlinks to original resources.

        There is no easy way to go from the original to the symlinks, but you can always find the original in the Resource Content field of the symlink.

        If you have many symlinks to one original, I think this utility snippet would find the Symlinks for that original:

        $originalId = 12;
        
        $symlinks = $modx->getCollection('modSymLink', array('content' => $originalId));
        
        $output = "Symlinks for Doc " . $originalId;
        $output .= "<ul>";
        foreach ($symlinks as $doc) {
            $output .= '<li>ID: ' . $doc->get('id') . '</li>';
        }
        
        $output .= "</ul>";
        
        return $output;


        [UPDATE this actually works]
        This version will find and report all symlinks with links to edit them:

        $symlinks = $modx->getCollection('modResource', array('class_key' => 'modSymLink'));
        $originals = array();
        $output = '';
        
        $targets = array();
        foreach ($symlinks as $symlink) {
            $symlinkId = $symlink->get('id');
            $targetId = $symlink->get('content');
            $targets[$targetId][$symlinkId] = $symlink->get('pagetitle');
        }
        
        foreach ($targets as $targetId => $symlinks) {
            $indent = '    ';
            $original = $modx->getObject('modResource', $targetId);
            $originalPagetitle = $original->get('pagetitle');
            $ctx = $original->get('context_key');
            $originalId = $original->get('id');
            $output .= "\n" . '<br><br>Original: <a href="' . MODX_MANAGER_URL . '?a=resource/update&id=' .
                $originalId . '">' . $originalPagetitle . ' (' . $originalId . ')</a>';
            $output .= "\n<br>Symlinks:";
            foreach ($symlinks as $id => $pagetitle) {
                $output .= "\n<br>{$indent}" . '<a href = "' . MODX_MANAGER_URL .
                    '?a=resource/update&id=' . $id . '" > ' . $pagetitle . ' (' . $id . ')</a>';
            }
        }
        
        return $output;
        
        [ed. note: BobRay last edited this post 5 years, 9 months ago.]
          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
          • 42560
          • 49 Posts
          Quote from: BobRay at Aug 03, 2018, 07:00 PM
          Yes, that's correct. There is no table connecting symlinks to original resources.

          There is no easy way to go from the original to the symlinks, but you can always find the original in the Resource Content field of the symlink.

          If you have many symlinks to one original, I think this utility snippet would find the Symlinks for that original:

          $originalId = 12;
          
          $symlinks = $modx->getCollection('modSymLink', 'content' => $originalId));
          
          $output = "Symlinks for Doc " . $originalId;
          $output .= "<ul>";
          foreach ($symlinks as $doc) {
              $output .= '<li>ID: ' . $doc->get('id') . '</li>';
          }
          
          $output .= "</ul>";
          
          return $output;


          This version should find and report all symlinks (untested):

          $allDocs = $modx->getCollection('modResource');
          $originals = array()
          
          foreach ($allDocs as $doc) {
              $id = $doc->get('id');
              if ($doc->get('class_key' == 'modSymLink') {
                  if (isset($originals[$id])) {
                     $originals[$id] .= ',' . $doc->get('content');
                  } else {
                     $originals[$id] = $doc->get('content');
                  }
              }
          }
          $output = '<pre>';
          
          foreach ($originals as $original => $symlinks) {
             $output .= '
          
          Original: ' . $original . '   '
              ' . $symlinks;
          }
          
          $output .= '</pre>';
          
          return $output;
          

          thanks bob , i will give it a try