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.

Data Model Questions /

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

Help Returning Current Page ID


Go to End


3 Posts   3740 Views

Avatar
ShadeFrozen

Community Member, 10 Posts

14 October 2010 at 5:13pm

Edited: 14/10/2010 10:24pm

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

Avatar
JonoM

Community Member, 130 Posts

15 October 2010 at 6:34pm

Edited: 15/10/2010 6:42pm

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); 
	}

Avatar
ShadeFrozen

Community Member, 10 Posts

16 October 2010 at 2:27pm

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