We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17667
    • 108 Posts
    The MODX Collections extra is a great tool for managing anything from blogs to inventory lists. The grid view displays columns you customize to fit the most important item information. A thumbnail image column often fits the bill for an item list. The Collections documentation shows you how to set one up and use the built in image renderer for display. The built-in method is a great start but has two shortcomings (as of Collections v3.4.1):
    1) The full-image is grabbed. It is displayed at a constrained width but still can take time to load and cannot be manipulated.
    2) If the image is from a TV that uses a Media Source the image will not display because the full file path is not used.

    But with the help of pThumb and the snippet renderer option in Collections it's easy to fix these issues. Here is what you can do:
    1) Make sure Collections and pThumb are listed as installed under Extras > Installer (I didn't want to list this as a step but I did).
    2) Go to Extras > Collection views. Right-click your view and select Update View.
    3) At the bottom of the page select Add Column.
    4) Fill in the Add Column window (note that this assumes our resources have an image TV called "myImg", change to reflect your TV name):
    Label: image
    Name: tv_myImage
    Width: 60
    Position: 2
    Editor: {blank}
    Renderer: {blank}
    Snippet Renderer: customThumbnailRenderer
    Hidden: {unchecked}
    Sortable: {unchecked}

    Click Save and close the Add Column window.
    5) Click Save on the Update View page.
    6) On the Elements tab choose New Snippet. Fill in:
    Name: customThumbnailRenderer
    Snippet code:
    $imgName = $modx->getOption('value', $scriptProperties, '');
    $imgPath = ''; //Leave this as an empty string if your TV does not use a Media Source. Otherwise fill in Media Source path (e.g. 'assets/content/images/')
    $thumb = $modx->runSnippet('pthumb', [ 
        'input' => $imgPath . $imgName, 
        'options' => '&w=60&h=60&zc=C' 
    ]);
    return '<img src="' . $thumb . '" width="60">';


    Click Save.

    That's it! If you have large images it may take a while for the Collection view to load the first time while it caches the thumbnails. Manipulate dimensions and other pThumb goodies as you see fit.

    If it is not working comment out last line and instead put "return $imgPath . $imgName;", then in your Collection view see if the string being returned is the proper path for the image.
    • Is there no way in the snippet to get that TV's media source assignment so that can be handled dynamically in the snippet rather than having to hard-code it?
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 17667
        • 108 Posts
        Quote from: sottwell at Jan 30, 2016, 02:55 AM
        Is there no way in the snippet to get that TV's media source assignment so that can be handled dynamically in the snippet rather than having to hard-code it?

        The Snippet Renderer function of Collections does not (as far as I can find) return the media source assignment for TVs.

        You could get the name of the TV by using this in the snippet:
        $column = $modx->getOption('column', $scriptProperties, '');
        $tv_name = substr($column, 3);


        From there it might be possible to perform a query that returns the media source assignment for the TV of that name. But I cannot find documentation for getting TV properties by TV name.

        Ideally Collections will be updated to pull Media Source paths for TVs. There is a discussion of this on GitHub:
        https://github.com/modxcms/Collections/issues/125
          • 17667
          • 108 Posts
          Here is an updated version if you want to be able to click on the thumbnail to edit the resource:
          $imgName = $modx->getOption('value', $scriptProperties, '');
          $imgPath = ''; //Leave this as an empty string if your TV does not use a Media Source. Otherwise fill in Media Source path (e.g. 'assets/content/images/')
          $thumb = $modx->runSnippet('pthumb', [ 
              'input' => $imgPath . $imgName, 
              'options' => '&w=60&h=60&zc=C'
          ]);
          $row = $modx->getOption('row', $scriptProperties, '');
          return '<a href="/manager/?a=resource/update&id=' . $row['id'] . '" title="' . $row['pagetitle'] . '"><img src="' . $thumb . '" width="60"></a>';
            • 52534
            • 13 Posts
            Is this possible using Image+ as my image TV?
              Front End Designer + Developer for Eternity
              • 17667
              • 108 Posts
              Quote from: rob_liberty at Jul 22, 2016, 01:32 AM
              Is this possible using Image+ as my image TV?

              I have not used the Image+ custom TV. Depending on how Image+ outputs the image path it should work.