Jump to:

10928 Posts in 2612 Topics by 1809 members

All other Modules

SilverStripe Forums » All other Modules » Random content display - Newbie

Discuss all other Modules here.

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

Page: 1 2
Go to End
Author Topic: 2988 Views
  • skywalkerdk
    Avatar
    Community Member
    3 Posts

    Random content display - Newbie Link to this post

    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

  • UncleCheese
    Avatar
    4085 Posts

    Re: Random content display - Newbie Link to this post

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

    <% control RandomQuotes %>

    html..

    <% end_control %>

  • skywalkerdk
    Avatar
    Community Member
    3 Posts

    Re: Random content display - Newbie Link to this post

    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

  • UncleCheese
    Avatar
    4085 Posts

    Re: Random content display - Newbie Link to this post

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

  • skywalkerdk
    Avatar
    Community Member
    3 Posts

    Re: Random content display - Newbie Link to this post

    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

  • UncleCheese
    Avatar
    4085 Posts

    Re: Random content display - Newbie Link to this post

    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.

  • Rishi
    Avatar
    Community Member
    97 Posts

    Re: Random content display - Newbie Link to this post

    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

  • becca
    Avatar
    Community Member
    5 Posts

    Re: Random content display - Newbie Link to this post

    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!

    2988 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.