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 have a ClassExtender extUser field into which users can type a comma separated list of aliases (just the last part of the url from the Resource alias field).

    I want to display any aliases entered into this field as a list of clickable urls.

    I have a snippet called signatoriesServiceProviders which takes the commas separated list, removes any white space, places quote marks around each item and returns the list as I want it to be displayed.

    signatoriesServiceProviders snippet
    <?php
    $serviceProviderCSV = $scriptProperties['list']; 
    explode(",", $serviceProviderCSV);
    $serviceProviderCSV = str_replace(' ', '', $serviceProviderCSV); //remove spaces from the users input
    function addQuotes($string) {
        return '"'. implode('","', explode(',', $string)) .'"';
    }
    $serviceProviders = str_getcsv($serviceProviderCSV);
    echo "<ul>";
    foreach ($serviceProviders as $serviceProvider) {
        echo "<li><a href=\"[[++site_url]]services/$serviceProvider\">[[++site_url]]services/$serviceProvider</a></li>";
    }
    echo "</ul>";


    But it only works if I send the comma separated list to the snippet as text like this

    [[signatoriesServiceProviders? &list=`SP16-C08, SP16-C09,SP16-C07`]]


    This results correctly in:
    <ul>
    <li><a href="https://domain.com/services/SP16-C08">https://domain.com/services/SP16-C08</a></li>
    <li><a href="https://domain.com/services/SP16-C09">https://domain.com/services/SP16-C09</a></li>
    <li><a href="https://domain.com/services/SP16-C07">https://domain.com/services/SP16-C07</a></li>
    </ul>


    If I use the placeholder (which contains the same data) like this...

    [[signatoriesServiceProviders? &list=`[[+serviceOrganisationsList]]`]]
    


    ...the data is picked up but the snippet treats it as one item and produces a single url with all the aliases and commas on the end.

    This goes wrong
    <ul>
    <li>https://domain.com/services/SP16-C08, SP16-C09,SP16-C07</li>
    </ul>


    I have tried using another snippet to pick up the string and then pass it to this snippet, but the result is the same.

    Can anyone advise me if I can amend the snippet to work as I want it to?

    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
      I don't see why that wouldn't work (assuming that the call that sets the placeholder is above the tag you show in your post).

      BTW, lines 3 and 5 though 7 appear to be dead code. Line 3 doesn't make an assignment and the function is never called. I think I would do it this way (untested):

      $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";


      (That's the entire snippet. PHP will apply trim() to each element to remove any leading or trailing spaces and optimize the loop for you.) Note that this won't work if the individual list items have internal spaces that also need to be removed.

      Another approach would be to make sure the call that sets the placeholder is above your snippet call, eliminate the property, and get the list inside the snippet with $modx->getPlaceholder('serviceOrganisationsList') -- replacing line 2 in the code above. If it works, I think it would be significantly faster than having MODX replace the placeholder tag and parse the snippet tag for the property value.

        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
        That worked. Thank you 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
          I'm glad it worked for you. smiley
            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