Jump to:

22977 Posts in 11742 Topics by 2826 members

General Questions

SilverStripe Forums » General Questions » Get Random Image

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1 2
Go to End
Author Topic: 1022 Views
  • socks
    Avatar
    Community Member
    190 Posts

    Get Random Image Link to this post

    I knew how to do this in SS2.X, but having problem in SS3. The background image is added in the CMS via SiteConfig.

    I have BackgroundImage.php

    class BackgroundImage extends DataObject {

    public static $has_one = array(
          'BackgroundImage' => 'Image',
          'SiteConfig' => 'SiteConfig'
    );
    ...
    }

    I have SiteConfigCustom.php

    class SiteConfigCustom extends DataExtension {
    public static $has_many = array(
       'BackgroundImages' => 'BackgroundImage'
    );
    ...

    }

    my old way:
    Page.php

    function RandomImage() {
    return dataobject::get_one("BackgroundImage","SiteConfigCustomID",true,"Rand()");
    }

    A couple I've tried (but the function makes the page break):
    Page.php

    function RandomImage() {
    return BackgroundImage::get()->sort('RAND()');
    and
    return BackgroundImage::get()->filter('SiteConfigCustomID')->sort('RAND()');

    }

    Any help much appreciated.

  • prawnstar
    Avatar
    Community Member
    116 Posts

    Re: Get Random Image Link to this post

    On my site I'm grabbing some random quotes from a class called Quote. In my Quotes class I have a checkbox for active quotes so I filter on that, sort by random and limit 1.

    public function getQuote() {
    $quotes = Quote::get()->filter(array('Active' => True))->sort('RAND()')->limit(1);
    return $quotes;
    }

    Also, in your example it looks like you are missing a single quote after "SiteConfigCustomID"?

    return BackgroundImage::get()->filter('SiteConfigCustomID;)->sort('RAND()');

    Hope that helps.

    Chris

  • socks
    Avatar
    Community Member
    190 Posts

    Re: Get Random Image Link to this post

    Hmmm...Still no joy. I get method 'fortemplate' does not exist on 'DataList'. That semi-colon wasn't actually there, just in the post.

    I assume SiteConfig is making things harder than it should be.

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: Get Random Image Link to this post

    Have you tried...

    BackgroundImage::get()->sort('RAND()')->First();

    The key is the First() call at the end.

    Sean

  • socks
    Avatar
    Community Member
    190 Posts

    Re: Get Random Image Link to this post

    Sorry for the slow response, I just finished up a 5,000 mile road trip.

    Looks like this works (if that's not the best way, let me know)...

    Page.php

       function SiteConfig() {
          return BackgroundImage::get()->sort('RAND()')->First();
       }

    Page.ss

       <% loop SiteConfig %>
          $BackgroundImage
       <% end_loop %>

    Thank you!

  • socks
    Avatar
    Community Member
    190 Posts

    Re: Get Random Image Link to this post

    Ok, not quite working. It kills all of my other content being pulled from SiteConfig.

    I tried changing it to the code below but that didn't seem to help.

       function SiteConfig() {
          parent::SiteConfig();
          return BackgroundImage::get()->sort('RAND()')->First();
       }

    Any suggestions?

  • IOTI
    Avatar
    Community Member
    187 Posts

    Re: Get Random Image Link to this post

    change the name of your SiteConfig() function or even better move it to your SiteConfigCustom class. That's killing your other content from SiteConfig. Change it to something like getRandomImage(). So in your SiteConfigCustom class

    public function getRandomImage(){
    return $this->BackgroundImages()->sort('RAND()')->First();
    }

    and then in your templates use

    $SiteConfig.RandomImage()

    or

    <% loop $SiteCongig %>
    $RandomImage
    <% end_loop %>

  • socks
    Avatar
    Community Member
    190 Posts

    Re: Get Random Image Link to this post

    Hi IOTI,

    Still not working. Not showing an image at all.
    Don't know if this is the proper way to extend the Controller on a custom SiteConfig.

    SiteConfigCustom.php

    class SiteConfigCustom extends DataExtension {

    }

    class SiteConfigCustom_Controller extends Page_Controller {
       
       public function getRandomImage() {
          return $this->BackgroundImages()->sort('RAND()')->First();
       }
    }

    Page.ss

       <% loop SiteConfig %>
        $RandomImage
       <% end_loop %>

    1022 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.