Jump to:

3373 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Help Returning Current Page ID

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

Page: 1
Go to End
Author Topic: 1868 Views
  • ShadeFrozen
    Avatar
    Community Member
    10 Posts

    Help Returning Current Page ID Link to this post

    I've been working on getting a random banner going and after wading through several tutorials all is working. In the Page_Controller of Page.php I have:

    public function RandomBanners() {
    $foldername = "bannerimgs";
    $folder = Folder::findOrMake($foldername);
    return DataObject::get("Image", "ParentID = $folder->ID", "RAND()", '', 3);
    }

    Under the init function I added:

    Requirements::javascript("mysite/javascript/jquery-1.3.2.min.js");
    Requirements::javascript("mysite/javascript/jquery.cycle.lite.1.0.js");

    Requirements::customScript("
    jQuery(document).ready(function(){
    jQuery('#bannerimages').cycle({ delay: 5000, speed: 4000 });
    jQuery('#bannerimages').show();
    });"
    );

    In Page.ss:

    <div id="bannerimages">
    <% control RandomBanners %>
    <img src="$URL" width="$width" height="$height" alt="" />
    <% end_control %>
    </div>

    And no problems, however if there are better ways to do the above please feel free to let me know. But now I would like to be able to select a different folder based on the current page ID. For example, if I'm on the homepage and it has a page ID of 1 I might want to select the random images from a different folder. I've tried adding:

    public function RandomBanners() {
    if($this ->ID = 1) {
    $foldername = "newfolder";
    }
    else {
    $foldername = "bannerimgs";
    }
    $folder = Folder::findOrMake($foldername);
    return DataObject::get("Image", "ParentID = $folder->ID", "RAND()", '', 3);
    }

    And what it appears to be doing is checking to see if any of the pages using the Page class have an ID of 1, and not the CURRENT page. So of course, if I use the ID of any real page it always uses the same folder.

    Basically, all I want to be able to do is to check inside a function if the page I am on has an ID of 1 and then proceed accordingly.
    Any ideas? All help appreciated.

    TIA

  • JonoM
    Avatar
    Community Member
    103 Posts

    Re: Help Returning Current Page ID Link to this post

    I think you're setting the ID to 1

    if($this ->ID = 1)

    Should be

    if ($this->ID == 1)

    You could add a database entry to the page class like 'BannerFolder' and fill it from a dropdown in the CMS that lets you choose a folder... if none is specified you could set a default in your function. I learned about that here http://ssorg.bigbird.silverstripe.com/widgets-2/show/253448

    So in Page class of Page.php

       static $db = array(
          'BannerFolder' => 'Int'
       );

       public function getCMSFields()
       {
          $fields = parent::getCMSFields();
          $fields->addFieldToTab("Root.BannerImages", new DropdownField('BannerFolder','Folder that contains Banner Images:', DataObject::get("Folder")->toDropdownMap()));
          return $fields;
       }

    In Page_Controller on Page.php

       function RandomBanners(){
          if ($this->BannerFolder) $BannerFolderID = $this->BannerFolder;
          else $BannerFolderID = 4 //ID of default folder
          return DataObject::get("Image", "ParentID = $BannerFolderID", "RAND()", '', 3);
       }

  • ShadeFrozen
    Avatar
    Community Member
    10 Posts

    Re: Help Returning Current Page ID Link to this post

    That's awesome, thanks JonoM! That makes sense and I have it all working as per your code example

    1868 Views
Page: 1
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.