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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

All other Modules /

Discuss all other Modules here.

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

Random content display - Newbie


Go to End


9 Posts   4985 Views

Avatar
skywalkerdk

Community Member, 3 Posts

19 June 2009 at 3:11am

Hi everyone,

This is my first, ever, post in these forums. (- So be gentle with me.) :o)

I've been trying to setup SilverStripe and have spent about 2 full days now Googling, reading forums and trial-and-erroring my way along. - I decided it was finally time to ask somebody who knews the answer to my question... :o)

The setup:
-------------

I'm using a basic SilverStripe installation with PixelGreen theme (including a righthand sidebar, that came with the theme).

I'm building a VERY basic site, that needs this function:

I need to the site to show a 'quote'. The quotes are plain-text comments basically something like this:
"This is quote number 1 - By number one", and
"This is quote number 2 - By number two", and
"This is quote number 3 - By number three" - You get the idea. :o)

- I need them to show up in random order, one at a time, in the righthandside-sidebar.
- Also, i need all of them listed on a single page.

What i've done / tried:
-----------------------------

I tried using the RandomContentWidget extention, which handles the quote-entries as child-pages. This works rather fine! - Thus, i can't get them displayed in the sidebar the single-page-display works great. I've read every written line in the readme, and Googled the problem thoroughly, still without a solution.

I have 10 years of experience in webdesign but i'm mint-new at SilverStripe, so if i may ask, please be very specific about details and files in your response. :o)

I think i need a very basic solution - either a work-around for the RandomContentWidget to also display in the sidebar, - or another easier solution entirely.

Looking forward to hopefully solving this issue..! :o)

Kind regards
// Thomas, Denmark

Avatar
UncleCheese

Forum Moderator, 4102 Posts

19 June 2009 at 3:13am

public function RandomQuotes()
{
return DataObject::get("Quote",null,"RAND()");
}

<% control RandomQuotes %>

html..

<% end_control %>

Avatar
skywalkerdk

Community Member, 3 Posts

19 June 2009 at 3:25am

Thank you for the swift reply! :o)

Where do i put the code? - Does the function and call go in the "sidebar.ss" or in another file?

What do i put in the HTML section between the two control-tags you wrote, what $variable stores the quotes content?

Will it automatically pull the page-childs of the same page that RandomContentWidget uses? OR
- Is it a database-query that makes a random pick? - if so, in which table to do i enter the data?

...I'm veeery new at this, please bear with me. ;o)

Cheers
// Thomas

Avatar
UncleCheese

Forum Moderator, 4102 Posts

19 June 2009 at 5:07am

Have you created a Quote object yet? Where are you managing these Quotes?

Avatar
skywalkerdk

Community Member, 3 Posts

19 June 2009 at 5:14am

The quotes (some 40 or so) are plain text pieces in a Word document right now, so i haven't created any object yet.

I just put a few of them into the RandomContentWidget to take it for a spin and see how it handled.

// Thomas

Avatar
UncleCheese

Forum Moderator, 4102 Posts

19 June 2009 at 7:01am

You need to go through Tutorial #5 on DataObject management. Here's a quick example that will allow you to add Quotes to your HomePage, and have them pulled in anywhere on your site. You will need to install the DataObjectManager module.

mysite/code/Quote.php

<?php
class Quote extends DataObject
{
static $db = array (
'Author' => 'Varchar(100)',
'Quote' => 'Text'
);

static $has_one = array(
'HomePage' => 'HomePage'
);

}
?>

mysite/code/HomePage.php

class HomePage extends Page
{
static $has_many = array ('Quotes' => 'Quote');

public function getCMSFields()
{
$f = parent::getCMSFields();
$f->addFieldToTab("Root.Content.Quotes", new DataObjectManager(
$this,
'Quotes',
'Quote',
array('Author' => 'Author', 'Quote' => 'Quote'),
new FieldSet(
new TextField('Author'),
new TextareaField('Quote')
)
));
}
}

class HomePage_Controller extends Page_Controller
{
}

mysite/code/Page.php


// Add to the Page_Controller class:

public function RandomQuotes()
{
$limit = 3;
return DataObject::get("Quote",null,"RAND()",null,$limit);
}

mysite/templates/Layout/SomeTemplate.ss

<% if RandomQuotes %>
<dl>
<% control RandomQuotes %>
<dt>$Author</dt>
<dd>$Quote</dd>
<% end_control %>
</dl>
<% else %>
There are no quotes.
<% end_if %>

Can't promise you that's free of syntax errors cause i did it in like 90 seconds, but you get the idea.

Avatar
Rishi

Community Member, 97 Posts

3 March 2010 at 7:28am

Edited: 21/03/2010 3:10am

Hello UC as usual your code is almost correct except one return statement.and yes thank for the code

mysite/code/Quote.php

<?php
class Quote extends DataObject
{
static $db = array (
'Author' => 'Varchar(100)',
'Quote' => 'Text'
);

static $has_one = array(
'HomePage' => 'HomePage'
);

}
?>

mysite/code/HomePage.php

class HomePage extends Page
{
static $has_many = array ('Quotes' => 'Quote');

public function getCMSFields()
{
$f = parent::getCMSFields();
$f->addFieldToTab("Root.Content.Quotes", new DataObjectManager(
$this,
'Quotes',
'Quote',
array('Author' => 'Author', 'Quote' => 'Quote'),
new FieldSet(
new TextField('Author'),
new TextareaField('Quote')
)
));
//ADDED LINE
return $f;
}
}

class HomePage_Controller extends Page_Controller
{
}

mysite/code/Page.php

// Add to the Page_Controller class:

public function RandomQuotes()
{
$limit = 3;
return DataObject::get("Quote",null,"RAND()",null,$limit);
}

mysite/templates/Layout/SomeTemplate.ss

<% if RandomQuotes %>
<dl>
<% control RandomQuotes %>
<dt>$Author</dt>
<dd>$Quote</dd>
<% end_control %>
</dl>
<% else %>
There are no quotes.
<% end_if %>

Note: make sure that DataObject module is installed

Avatar
becca

Community Member, 5 Posts

14 March 2010 at 11:10am

This was great help! The only thing I was missing (that only took me a few minutes to figure out) was the DataObjectManager module.

Very easy!

Go to Top