We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38783
    • 571 Posts
    I would like to populate getResources with a comma separated list of resource ids to display.

    What I have access to is a comma separated list of aliases in a placeholder called [[+serviceOrganisationsList]]

    I have found a snippet called GetIDByAlias that will return a resource id from an alias.
    https://forums.modx.com/index.php?action=thread&thread=66442&i=1

    The snippet call
    [[GetIDByAlias? &alias=`myAliasName`]]

    The snippet
    $resources = $modx->getObject(modResource, array('alias' => $alias ));
    if ($resources != null) echo $resources->get('id');


    I also have access to a snippet that Bob Ray kindly rewrote for me yesterday. This took my comma separated list of aliases and turned them in to a list of fully formed urls.
    https://forums.modx.com/thread/102798/snippet-does-not-process-comma-separated-string-from-placeholder#dis-post-553718

    The snippet call
    [[!signatoriesServiceProviders? &list=`[[!+serviceOrganisationsList]]`]]

    The snippet
    $output = "\n<ul>\n";
    $serviceProviderCSV = $scriptProperties['list']; 
    $new_arr = array_map('trim', explode(',', $serviceProviderCSV));
    foreach($new_arr as $key => $serviceProvider) {
        $output .= "\n    <li><a href=\"[[++site_url]]services/$serviceProvider\">[[++site_url]]services/$serviceProvider</a></li>";
    }
    return $output . "\n</ul>\n";


    What I have been trying to do is use bits of these two snippets to produce a snippet that would take the comma separated list of aliases and return a comma separated list of resource ids. I will not humiliate myself here by displaying the Frankenstein of a snippet that has resulted from my efforts. At best it does nothing, occasionally it prevents the page from loading at all.

    Can anyone help?

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

      If I help you out on these forums I would be very grateful if you would consider rating me on Trustpilot: https://uk.trustpilot.com/review/andytough.com

      email: [email protected] | website: https://andytough.com
    • discuss.answer
      • 3749
      • 24,544 Posts
      Here you go (untested):

      $aliases = $modx->getOption('aliases', $scriptProperties, array(), true); // or wherever you get the comma-separated list from
      $docAliases = array_map('trim', explode(',', $aliases));
      
      $idArray = array();
      
      foreach ($docAliases as $docAlias) {
          $query = $modx->newQuery('modResource', array('alias' => $docAlias));
          $query->select('id');
          $docId = $modx->getValue($query->prepare());
      
          if ($docId) {
              $idArray[] = $docId;
          } else {
              $modx->log(modX::LOG_LEVEL_ERROR, 'Could not find resource with alias: ' . $docAlias);
          }
      }
      
      return (implode(',', $idArray));


      The first three lines of the foreach statement are just a faster version of what GetIDByAlias does. You can remove the 'else' section if you don't want the errors reported to the error log.

      Where are is the comma-separated list of aliases coming from? There may be a faster way to get them as well. [ed. note: BobRay last edited this post 6 years, 7 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
        • 38783
        • 571 Posts
        Hello Bob, thanks for this.

        I can't get it to work as it is, but I can't seem to track down where it is going wrong.

        If I pass a non existent alias to it the error is logged as in the 'else' statement, so that is working.

        If I use print_r ($docAliases); on line 5 it returns my list of aliases correctly.
        Array ( [0] => SP16-C08 [1] => SP16-C09 [2] => SP16-C07 )


        But return (implode(',', $idArray)); on the last line returns just two commas.

        If I replace most of the foreach with a slightly modified version of GetIDByAlias it returns my id numbers correctly, but, of course, not as a comma separated list. See below (to simplify things in the example I have added my aliases manually to the snippet, but the $scriptProperties line does retrieve the values correctly).

        <?php
        //$aliases = $modx->getOption('aliases', $scriptProperties, array(), true); 
        $aliases = "SP16-C08, SP16-C09, SP16-C07";
        // or wherever you get the comma-separated list from
        $docAliases = array_map('trim', explode(',', $aliases));
        print_r ($docAliases);
        $idArray = array();
        
        foreach ($docAliases as $docAlias) {
            $query = $modx->getObject('modResource', array('alias' => $docAlias ));
            if ($query != null) echo $query->get('id');
        }
         
        
        


        This returns
        129136128
        which are the correct correct as the ids are 129, 136 and 128

        However, it doesn't quite get to where I need to be as I want a comma separated list.

        Is there some minor tweak to your code that would get it working?

        To answer your question the alias values are stored as a list entered by users in an Class Extender extUser field in their profile.
          If I help you out on these forums I would be very grateful if you would consider rating me on Trustpilot: https://uk.trustpilot.com/review/andytough.com

          email: [email protected] | website: https://andytough.com
          • 38783
          • 571 Posts
          I have got it working!

          $idArray[] = $docArray;


          changed to

          $idArray[] = $docId;


          Thanks again Bob.
            If I help you out on these forums I would be very grateful if you would consider rating me on Trustpilot: https://uk.trustpilot.com/review/andytough.com

            email: [email protected] | website: https://andytough.com
            • 3749
            • 24,544 Posts
            Doh. Sorry about that. I did say "untested." wink
              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
              • 38783
              • 571 Posts
              If it had worked straight away I wouldn't have learned anything!

              Thanks again.
                If I help you out on these forums I would be very grateful if you would consider rating me on Trustpilot: https://uk.trustpilot.com/review/andytough.com

                email: [email protected] | website: https://andytough.com