Jump to:

7940 Posts in 1543 Topics by 946 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » displaying random child in homepage

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1 2
Go to End
Author Topic: 1938 Views
  • Rishi
    Avatar
    Community Member
    97 Posts

    displaying random child in homepage Link to this post

    I have a holder page and its child page where i am inserting text and images.
    Tt the moment i am displaying only the images from child page in the holder page.
    this much is working fine.

    Things need to be done
    I want to randomly display image and first line of the text inserted in three child pages in my homepage
    my code are
    Holder page name is Horses.php

    <?php
    class Horses extends Page {
    static $db = array(
    );
    static $has_one = array(
    );

    static $allowed_children = array('HorsesPage');
    }

    class Horses_Controller extends Page_Controller {

    public function GetHorsesPage(){
    return DataObject::get("HorsesPage");
    }
    }

    ?>

    Child page name is HorsesPage.php

    <?php
    class HorsesPage extends Page {
    static $db = array(
    );
    static $has_one = array(
    'Photo' => 'Image'
    );

    function getCMSFields() {
    $fields = parent::getCMSFields();
    $fields->addFieldToTab("Root.Content.Images", new ImageField('Photo'));
    return $fields;
    }

    static $defaults = array(
    'ShowInMenus' => false
    );
    static $allowed_children = array('none');
    }

    class HorsesPage_Controller extends Page_Controller {

    }
    ?>

    Example Code will be of great help i am new for silverstripe.
    Thank you in advance

  • UncleCheese
    Avatar
    4085 Posts

    Re: displaying random child in homepage Link to this post

    You're writing a little too much code.. The "GetHorsesPages" function is useless because what you're really interested in is the Children pages. GetHorsesPages is too inclusive and has the possibility of returning orphaned HorsePages.

    In your Horse_Controller, add this:

    public function RandomChildren($max = 3)
    {
    return DataObject::get("HorsePage","ParentID = $this->ID","RAND()",null,"0,$max");
    }

    <% control RandomChildren %>
    Random title: $Title
    <% end_control %>

  • Rishi
    Avatar
    Community Member
    97 Posts

    Re: displaying random child in homepage Link to this post

    Thank you UC
    i tried the code and it is giving me an error

    [User Error] Bad class to singleton() - HorsePage
    showing error in this line
    if(!class_exists($className)) user_error("Bad class to singleton() - $className", E_USER_ERROR);

  • UncleCheese
    Avatar
    4085 Posts

    Re: displaying random child in homepage Link to this post

    HorsesPage... whatever you called it.

  • Rishi
    Avatar
    Community Member
    97 Posts

    Re: displaying random child in homepage Link to this post

    UC sorry for my mistake,your code works great,
    another silly question hope you dont mind
    i want to display the images uploaded in the child page.though i have displayed them in the parent page but i am not been able to set the height and width,is it possible to set the and width of images dispalyed.
    my template page contain this code

    <% control RandomChildren %>
    $Title
    $Photo

    <% end_control %>

    Another thing i want to do is calling 3 random pages in my homepage i tried with this code but it is giving me error,i think i ned to write the page parent page but dont no how and where
    public function RandomChildren($max = 3)
    {
    return DataObject::get("HorsePage","ParentID = $this->ID","RAND()",null,"0,$max");
    }
    thanks for helping me so much

  • UncleCheese
    Avatar
    4085 Posts

    Re: displaying random child in homepage Link to this post

    What do you mean it gave you an error? What error?

    There are a bunch of things you can do for image resampling. You should really read the docs..

    $Photo.SetWidth(100)

    $Photo.CroppedImage(50,50)

    <% control Photo %><img src="$URL" class="class" / ><% end_control %>

    etc...

  • Rishi
    Avatar
    Community Member
    97 Posts

    Re: displaying random child in homepage Link to this post

    thank you UC that works great for me the image size is being adjusted nicely.
    the code which you have posted works great for a child pages
    but i am trying to display random child page on my home page.let me b clear using the site tree

    -HOME
    -Universities
    --University1
    --university2
    --university3
    --university4
    --university5
    -Student
    contact us

    Now i was trying to display 3 random university pages on my home pages and i tried the following code

    public function RandomChildren($max = 3)
    {
    return DataObject::get("HorsePage","ParentID = $this->ID","RAND()",null,"0,$max");
    }

    university1 university 2 etc have page type as HorsePage.

    regarding error -i am sorry i was not been able to make you understand
    error means nothing is showing on homepage
    if i make child of homepage with page type as horsepage then only it get displayed .
    but i dont want that i want to get random university on my homepage

    thank you in advance.
    and thanks for the suggestion about the documents.i am going through the documents which i am able to find,i got a link about the test which is
    http://doc.silverstripe.org/doku.php?id=text
    but i have not been able to find any document about pictures.it will b really great of you if you can guide me on finding the document s of silverstripe regarding the programming
    thank you again you are really a great help.without you i would have been bangining my head on wall

  • UncleCheese
    Avatar
    4085 Posts

    Re: displaying random child in homepage Link to this post

    If the page "Universities" is the only one of its kind, you can just get that and return random children.. or you can control it by URLSegment

    <% control Page(Universities) %>
    <% control RandomChildren %>

    <% end_control %>
    <% end_control %>

    Or, just write a function in your Page_Controller that will be accessible by all your pages.

    public function ChildrenOfUniversities($num = 3)
    {
    if($page = DataObject::get_one("Horse")) return $horse->RandomChildren($num);
    return false;
    }

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