Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions

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

Get Random Image


Go to End
Reply

14 Posts   1318 Views

Avatar
socks

19 October 2012 at 2:23pm (Last edited: 31 October 2012 10:53am), Community Member, 190 Posts

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.

Avatar
prawnstar

20 October 2012 at 8:53am Community Member, 116 Posts

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

Avatar
socks

20 October 2012 at 9:19am Community Member, 190 Posts

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.

Avatar
Sean

25 October 2012 at 2:53pm (Last edited: 25 October 2012 2:54pm), Forum Moderator, 921 Posts

Have you tried...

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

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

Sean

Avatar
socks

31 October 2012 at 11:13am Community Member, 190 Posts

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!

Avatar
socks

31 October 2012 at 1:17pm Community Member, 190 Posts

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?

Avatar
IOTI

1 November 2012 at 12:28am Community Member, 189 Posts

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 %>

Avatar
socks

1 November 2012 at 5:52am (Last edited: 1 November 2012 5:54am), Community Member, 190 Posts

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 %>

Go to Top