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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

[SOLVED] How can I get the ID of parent pages in navigation.ss?


Go to End


6 Posts   1116 Views

Avatar
Harley

Community Member, 165 Posts

7 August 2012 at 12:27am

Edited: 07/08/2012 12:28am

Hello,

I want have a dropdown nav on my site which I am trying to populate with a featured item like so http://codecanyon.net/item/dropdown-mega-menu/2536632

The end result should be on each page within the admin panel I can add a 'featured' blurb and image, one of which will appear randomised in the dropdown for each section of the site.

This code decorates the sitetree - DataObject::add_extension('SiteTree', 'FeaturedItemDropDown');

My code for FeaturedItemDropDown.php

<?php

class FeaturedItemDropDown extends DataObjectDecorator{

	function extraStatics() {
        return array(
            'db' => array(
				'Summary' => 'Varchar(150)'
            ),
            'has_one' => array(
				'FeaturedImage' => 'Image'
            ),
        );
    }

	function updateCMSFields(FieldSet &$fields) {		
		$fields->addFieldToTab('Root.Content.Featured', new TextareaField('Summary', 'Summary (150 characters max)', '', 200));
		$fields->addFieldToTab('Root.Content.Featured', new ImageField('FeaturedImage', 'Upload your image (000 x 000)'));
	}
	public function RandomiseFeaturedItemDropDown($num = 1){
		return DataObject::get('SiteTree', 'ParentID = $this->ID AND Summary != "NULL"', 'RAND()', '$num');
	}
}

FeaturedItemDropDown.ss

<div class="featuredItem">
	<% control RandomiseFeaturedItemDropDown %>
	<h2>$FeaturedItemDropDown.Title</h2>
	$Summary
	$Image
	<% end_control %>
</div>

Then in Navigation.ss :-

<ul class="navDropdown">
 	<% control Menu(1) %>	  
  		<li><a href="$Link" title="Go to the $Title.XML page" class="$LinkingMode"><span>$MenuTitle.XML</span></a>
		<% if Children %>	
			<div class="navDrop<% if ExtraDropNavInfo %> $ExtraDropNavInfoClassName<% end_if %>"><!-- NavDrop container (library) -->
				<span class="navDropDivide"></span>
				<div class="navDropContent cf"><!-- NavDrop content -->
						<div class="navColTopic"> 
						<ul>
							<% control Children %>
							<li><a href="$Link" title="Go to the $Title.XML page" class="$LinkingMode"><span>$MenuTitle.XML</span></a>
							<% end_control %>
						</ul>
						<div class="navColFeatured">
						<% include FeaturedItemDropdown %>
						</div>
					</div>
				</div>
				</div>
		<% end_if %>
		</li>
   	<% end_control %>
</ul>

The main problem I am experiencing is getting the ParentID to match the ID of each page in the navigation. How can I get the current ID of each link in the nav? $this-ID doesn't seem to work.

Regards

Avatar
jak

Community Member, 46 Posts

7 August 2012 at 7:30am

Try replacing:

return DataObject::get('SiteTree', 'ParentID = $this->ID AND Summary != "NULL"', 'RAND()', '$num'); 

with
return DataObject::get('SiteTree', 'ParentID = '.$this->ID.' AND Summary != "NULL"', 'RAND()', '$num'); 

Avatar
Harley

Community Member, 165 Posts

7 August 2012 at 7:54am

Thanks Jak,

actually I've just discovered something here, I'm a dumbass! The function RandomiseFeaturedItemDropDown isn't doing anything. I've just written Debug::mesage('hiya') just to see and to no avail, I think the problem is how the function is called. Should this function be called somewhere from a controller?

Avatar
Harley

Community Member, 165 Posts

7 August 2012 at 12:37pm

Ok,

I have almost cracked it! It turns out the wy to call $this->ID is to write it like this as we are in a dataobject; ".$this->owner->ID."

But there is a new problem, here is my code:

<?php

class FeaturedItemDropDown extends DataObjectDecorator{

	function extraStatics() {
        return array(
            'db' => array(
				'Summary' => 'Varchar(150)'
            ),
            'has_one' => array(
				'FeaturedImage' => 'Image'
            ),
        );
    }

	function updateCMSFields(FieldSet &$fields) {		
		$fields->addFieldToTab('Root.Content.Featured', new TextareaField('Summary', 'Summary (150 characters max)', '', 200));
		$fields->addFieldToTab('Root.Content.Featured', new ImageField('FeaturedImage', 'Upload your image (000 x 000)'));
	}

	public function RandomiseFeaturedItem($num = 1){
		return DataObject::get("sitetree", "`ParentID` = ".$this->owner->ID." AND `Summary` != 'NULL'", "RAND()", "", $num);
	}
}

The problem I have now is that the column 'Summary' in my database is ambiguous

[User Error] Couldn't run query: SELECT "SiteTree_Live"."ClassName", "SiteTree_Live"."Created", "SiteTree_Live"."LastEdited", "SiteTree_Live"."URLSegment", "SiteTree_Live"."Title", "SiteTree_Live"."MenuTitle", "SiteTree_Live"."Content", "SiteTree_Live"."MetaTitle", "SiteTree_Live"."MetaDescription", "SiteTree_Live"."MetaKeywords", "SiteTree_Live"."ExtraMeta", "SiteTree_Live"."ShowInMenus", "SiteTree_Live"."ShowInSearch", "SiteTree_Live"."HomepageForDomain", "SiteTree_Live"."ProvideComments", "SiteTree_Live"."Sort", "SiteTree_Live"."HasBrokenFile", "SiteTree_Live"."HasBrokenLink", "SiteTree_Live"."Status", "SiteTree_Live"."ReportClass", "SiteTree_Live"."CanViewType", "SiteTree_Live"."CanEditType", "SiteTree_Live"."ToDo", "SiteTree_Live"."Version", "SiteTree_Live"."Summary", "SiteTree_Live"."ParentID", "SiteTree_Live"."FeaturedImageID", "SiteTree_Live"."SubsiteID", "SiteTree_Live"."MasterPageID", CASE WHEN "SiteTree_Live"."ClassName" IN ('HomePage') THEN "HomePage_Live"."ExtraDropNavInfo" WHEN "SiteTree_Live"."ClassName" IN ('LibraryHolder') THEN "LibraryHolder_Live"."ExtraDropNavInfo" ELSE NULL END AS "ExtraDropNavInfo", CASE WHEN "SiteTree_Live"."ClassName" IN ('HomePage') THEN "HomePage_Live"."ExtraDropNavInfoClassName" WHEN "SiteTree_Live"."ClassName" IN ('LibraryHolder') THEN "LibraryHolder_Live"."ExtraDropNavInfoClassName" ELSE NULL END AS "ExtraDropNavInfoClassName", "Library_Live"."LibraryHolderID", "BlogEntry_Live"."Date", "BlogEntry_Live"."Author", "BlogEntry_Live"."Tags", "BlogTree_Live"."Name", "BlogTree_Live"."InheritSideBar", "BlogTree_Live"."LandingPageFreshness", "BlogTree_Live"."SideBarID", "Calendar_Live"."DefaultEventDisplay", "Calendar_Live"."DefaultDateHeader", "Calendar_Live"."OtherDatesCount", "CalendarEvent_Live"."Recursion", "CalendarEvent_Live"."CustomRecursionType", "CalendarEvent_Live"."DailyInterval", "CalendarEvent_Live"."WeeklyInterval", "CalendarEvent_Live"."MonthlyInterval", "CalendarEvent_Live"."MonthlyRecursionType1", "CalendarEvent_Live"."MonthlyRecursionType2", "CalendarEvent_Live"."MonthlyIndex", "CalendarEvent_Live"."MonthlyDayOfWeek", "CalendarEvent_Live"."CalendarID", CASE WHEN "SiteTree_Live"."ClassName" IN ('ContactPage') THEN "ContactPage_Live"."Mailto" WHEN "SiteTree_Live"."ClassName" IN ('RegistrationForm') THEN "RegistrationForm_Live"."Mailto" ELSE NULL END AS "Mailto", "ContactPage_Live"."ContactSubmitText", "RegistrationForm_Live"."SubmitText", "GridPage_Live"."Template", "ErrorPage_Live"."ErrorCode", "RedirectorPage_Live"."RedirectionType", "RedirectorPage_Live"."ExternalURL", "RedirectorPage_Live"."LinkToID", "VirtualPage_Live"."VersionID", "VirtualPage_Live"."CopyContentFromID", "BlogHolder_Live"."TrackBacksEnabled", "BlogHolder_Live"."AllowCustomAuthors", "BlogHolder_Live"."OwnerID", "SubsitesVirtualPage_Live"."CustomMetaTitle", "SubsitesVirtualPage_Live"."CustomMetaKeywords", "SubsitesVirtualPage_Live"."CustomMetaDescription", "SubsitesVirtualPage_Live"."CustomExtraMeta", "SiteTree_Live"."ID", CASE WHEN "SiteTree_Live"."ClassName" IS NOT NULL THEN "SiteTree_Live"."ClassName" ELSE 'SiteTree' END AS "RecordClassName" FROM "SiteTree_Live" LEFT JOIN "HomePage_Live" ON "HomePage_Live"."ID" = "SiteTree_Live"."ID" LEFT JOIN "Library_Live" ON "Library_Live"."ID" = "SiteTree_Live"."ID" LEFT JOIN "LibraryHolder_Live" ON "LibraryHolder_Live"."ID" = "SiteTree_Live"."ID" LEFT JOIN "BlogEntry_Live" ON "BlogEntry_Live"."ID" = "SiteTree_Live"."ID" LEFT JOIN "BlogTree_Live" ON "BlogTree_Live"."ID" = "SiteTree_Live"."ID" LEFT JOIN "Calendar_Live" ON "Calendar_Live"."ID" = "SiteTree_Live"."ID" LEFT JOIN "CalendarEvent_Live" ON "CalendarEvent_Live"."ID" = "SiteTree_Live"."ID" LEFT JOIN "ContactPage_Live" ON "ContactPage_Live"."ID" = "SiteTree_Live"."ID" LEFT JOIN "RegistrationForm_Live" ON "RegistrationForm_Live"."ID" = "SiteTree_Live"."ID" LEFT JOIN "GridPage_Live" ON "GridPage_Live"."ID" = "SiteTree_Live"."ID" LEFT JOIN "ErrorPage_Live" ON "ErrorPage_Live"."ID" = "SiteTree_Live"."ID" LEFT JOIN "RedirectorPage_Live" ON "RedirectorPage_Live"."ID" = "SiteTree_Live"."ID" LEFT JOIN "VirtualPage_Live" ON "VirtualPage_Live"."ID" = "SiteTree_Live"."ID" LEFT JOIN "BlogHolder_Live" ON "BlogHolder_Live"."ID" = "SiteTree_Live"."ID" LEFT JOIN "SubsitesVirtualPage_Live" ON "SubsitesVirtualPage_Live"."ID" = "SiteTree_Live"."ID" WHERE (`ParentID` = 1 AND `Summary` != 'NULL') AND ("SiteTree_Live"."SubsiteID" IN (0)) ORDER BY RAND() LIMIT 1 Column 'Summary' in where clause is ambiguous

I have no idea what this could mean, I've searched the archives and forum and come across this problem but I'm none the wiser about what causes this, any ideas guys?

Regards

Avatar
jak

Community Member, 46 Posts

7 August 2012 at 11:40pm

Edited: 07/08/2012 11:45pm

It means MySQL has no idea what table Summaries is on.

Try something like:

return DataObject::get("sitetree", '"SiteTree_Live"."ParentID" = '.$this->owner->ID.' AND "SiteTree_Live"."Summary" IS NOT NULL', "RAND()", "", $num); 

In 3.0 you could probably use ->where('"ParentID" = '.$this->owner->ID.')->where('Summary" IS NOT NULL').

Edit: Question was also answered in the crosspost to http://www.silverstripe.org/general-questions/reply/20571

Avatar
Harley

Community Member, 165 Posts

8 August 2012 at 12:49am

Thank you for your help!

That is so much clearer to me now and works fine now. Apologies for the cross post, I thought it would be beneficial for a seperate thread for future reference. This response was a lot more helpful for me than more specific posts on the same topic that I've seen, hopefuly these posts will help someone else in the future.

Regards