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   1482 Views

Avatar
socks

Community Member, 190 Posts

19 October 2012 at 2:23pm

Edited: 31/10/2012 10:53am

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

Community Member, 127 Posts

20 October 2012 at 8:53am

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

Community Member, 190 Posts

20 October 2012 at 9:19am

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

Forum Moderator, 922 Posts

25 October 2012 at 2:53pm

Edited: 25/10/2012 2:54pm

Have you tried...

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

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

Sean

Avatar
socks

Community Member, 190 Posts

31 October 2012 at 11:13am

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

Community Member, 190 Posts

31 October 2012 at 1:17pm

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
copernican

Community Member, 189 Posts

1 November 2012 at 12:28am

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

Community Member, 190 Posts

1 November 2012 at 5:52am

Edited: 01/11/2012 5:54am

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