We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • I mean display random image from any album?
      palma non sine pulvere
      • 37286
      • 160 Posts
      @Bruno,
      I am calling the snippet from my template, but I also tried calling it from a chunk. Neither worked.
      [[!Gallery? &album=`3` &sort=`rand` &toPlaceholder=`0` &limit=`1` &thumbWidth=`210` &thumbHeight=`190` &thumbTpl=`myRandomThumb`]]
      

      I did notice that the getlist call made by gallery has a lot of caching calls. I'm not up on how all the caching works, maybe that's the problem. It's not modx, it's gallery?

      @Last Of The Romans,
      Maybe try something custom like a snippet named randomGalleryItemID:
      $gallery = $modx->getService('gallery','Gallery',$modx->getOption('gallery.core_path',null,$modx->getOption('core_path').'components/gallery/').'model/gallery/',$scriptProperties);
      if (!($gallery instanceof Gallery)) return '';
      $q = $modx->newQuery('galItem');
      $q->where(array(
          'active:=' => true
      ));
      $q->sortby('RAND()','');
      $q->limit(1);
      $image = $modx->getObject('galItem',$q);
      return $image->get('id');
      


      Then call the snippet from your "id" field in your GalleryItem call:
      [[!GalleryItem? &id=`[[!randomGalleryItemID]]` &toPlaceholders=`0`]]
      
      • i get:

        Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' in /home/user/public_html/core/cache/includes/elements/modsnippet/68.include.cache.php on line 11
          palma non sine pulvere
        • hm, now i get:

          
          Warning: Invalid argument supplied for foreach() in /home/user/public_html/core/cache/includes/elements/modsnippet/24.include.cache.php on line 106
          
          Fatal error: Cannot use string offset as an array in /home/user/public_html/core/cache/includes/elements/modsnippet/24.include.cache.php on line 165
          
            palma non sine pulvere
            • 37286
            • 160 Posts
            If you run randomGalleryItemID by itself, what does it give you?
            What version of Gallery are you using? I am testing with 1.5.2.
            • insomnix,
              first thank you once again for your help! When i try to run just randomGalleryItemID its return random ID`s - it`s work...so strange for me...

              this is my call:

              [[!Gallery? &album=`[[!randomGalleryItemID]]` &thumbTpl=`prettyGalleryImageTpl` &thumbWidth=`250` &thumbHeight=`150` &useCss=`0` &toPlaceholders=`0` &limit=`1`]]
              


              version: 1.5.2
                palma non sine pulvere
                • 37286
                • 160 Posts
                You are trying to pass an image id to the album field. Which means you will probably get an id that is out of range. Check out docs at: http://rtfm.modx.com/display/ADDON/Gallery.GalleryItem

                Use GalleryItem instead of Gallery:
                [[!GalleryItem? &id=`[[!randomGalleryItemID]]` &toPlaceholders=`0`]]
                
                • yes...and now i have caching issue smiley when i refresh the page (2,5 or..10 times) i have no new image...
                    palma non sine pulvere
                    • 42339
                    • 5 Posts
                    I tried doing something very similar to insomnix from the post 1 year 11 months ago, reply 15 in this thread.

                    In my template:
                    [[!Gallery? &album=`1` &thumbTpl=`sidebarImageRowTpl` &sort=`rand` &limit=`1`]]

                    sidebarImageRowTpl Chunk code:
                    <img src="[[!+image_absolute]]" alt="[[!+description]]" />

                    Only when I clear the cache, I get a new sort, which in my scenario displays a new image. I can see there is a file being generated in core/cache/default/gallery/item/list/. If I delete that file, I get a new image (as long as it doesn't, by chance, load the same one).

                    http://modx.com/extras/package/gallery
                    Changelog
                    ---------
                    New in 1.5.1
                    - Add DB caching to Gallery and GalleryAlbums snippets, reducing page load times

                    I'm wondering if this change had anything to do with it, and why it was giving insomnix problems after upgrading from Gallery 1.4.0 to 1.5.2

                    Any ideas anyone? Could it be that &sort=`rand` is no longer effective with the change to DB caching? Is it something else? Or am I just doing something wrong?

                    Thanks in advance.
                      • 42339
                      • 5 Posts
                      Here's what I did to make it work, going forward with my implementation above for displaying a random image.

                      Snippet called in the template
                      [[clearGalleryCache]]

                      Snippet code:
                      <?php
                      $myCacheManager = $modx->getCacheManager();
                      $myCacheManager->delete('gallery/item/list/');

                      That deletes the file in core/cache/default/gallery/item/list/. It regenerates itself, of course, but with the new image info.

                      Changing the permissions for the core/cache/default/gallery/item/list/ folder so that it was not writable also worked. I'm not sure if that is better than deleting the cache file with each page load.

                      Disclaimer - I'm a MODX and PHP novice... so... yeah. But, seems to be working great, no errors in the MODX Reports > Error Log at least.