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

Holder Pages showing limited number of Child Page Elements


Go to End


11 Posts   7198 Views

Avatar
edk

Community Member, 39 Posts

29 January 2011 at 9:09am

Edited: 29/01/2011 9:17am

Here is my setup/dilemma

ImageGalleryHolder
ImageGalleryPage
ImageGalleryPageImage [a dataobject - a has many relationship of ImageGalleryPage]

Here is the current state [in development]http://crankygranny.com/flpr/about-the-river/photos/

On the the Image Gallery Holder Page I want to display all of the ImageGalleryPages, *but* in that display only show say 3 images from that ImageGalleryPage. Right now in the holder page i just do <% control Children %> and it iterates and shows thumbs for *all* of Images belonging to a ImageGalleryPage. I would just like call a method that returns only 3 of this Images, or in the template just limit the iteration in the Child to 3.

I have tried multiple ways to create my own method in ImageGalleryHolder which is called once I start iterating, but I think I run into the scope issue of $Top and $Children. Once I get down into control ImageGalleryPageImages I cannot seem to get that Page's ID in a method to then get those files.

Here is the code I have in my Holder.ss

	
	<% if Children %>
			<ul class="imageGalleries">
			<% control Children %>
				<li>
					<h3><a href="$Link" title="$GalleryTitle">$GalleryTitle</a></h3>
					<p>{$GalleryExcerpt} <a href="$Link" title="$GalleryTitle">View the complete gallery &raquo;</a></p>
					<ul>
					<% control ImageGalleryPageImages %>
						<li class="imageGalleryItem">
							<a rel="imageGallery" title="$Caption" href="$Attachment.Filename">
								<% control Attachment %>
							    	<img src="$CroppedImage(160,160).URL" alt="$Title" />
								<% end_control %></a>
							<span class="caption">$Description</span>
						</li>
					<% end_control %>
					</ul>
				</li>
			<% end_control %>
			</ul>
				
		<% end_if %>
	

** I know there is the Image_Gallery Module from Uncle Cheese, but this requirement is a bit of an edge case. So it not a solutin to just use that module.

Thanks in advance for any help.
Ed

Avatar
Webbower

Community Member, 10 Posts

29 January 2011 at 9:28am

The obvious answer than comes to mind is that you should be able to limit the number of returned entries in a control loop. If you set your ImageGalleryPageImages() method to limit 3, it should only loop 3 times. Alternatively, if you want the flexibility to call that method other places in the site and return a different number of images, you can set the method to accept a parameter for the limit then call it like

<% control ImageGalleryPageImages(5) %>

in the template and you should get 5 items to loop through in this case.

Avatar
edk

Community Member, 39 Posts

29 January 2011 at 9:47am

Hi Matt,

Thanks for your response. I actually was trying to do exactly as you say, but the issue I run into is that the method I call for some reason does not seem to recognise the curent ImageGalleryPage in that iteration.

So if I have that method and place it in the ImageGalleryHolder_Controller I tried the following

	function ImageGalleryPageImages($num = 5 ) {
		return DataObject::get("ImageGalleryPageImage", "`ImageGalleryPageID`" = $this->ID", "SortOrder DESC", "", $num);
	}

This just doesn't return anything, because I am not sure if the method knows what the $this->ID is referring to. I have tried moving this emthod to the ImageGalleryPage and also to the Page.php but have not had any luck. I am hoping I am just missing something really simple. I am thoinking it may be nesting thing in th eiteration as I am getting deeper and looking for Images of teh Page, but I am not really sure.

Thanks,
Ed

Avatar
Webbower

Community Member, 10 Posts

29 January 2011 at 10:37am

If the method you're calling is in the Controller and the controller has the $has_many relation to the images, then you should be able to call

$this->getComponents('HasManyRelationName', '', '', '', $limit);

or

$this->data()->getComponents('HasManyRelationName', '', '', '', $limit);

if the first one doesn't work.

Avatar
edk

Community Member, 39 Posts

29 January 2011 at 12:15pm

Edited: 29/01/2011 12:15pm

Thanks for the tip. Even though it didn't work in this case, I have learnt something new that I didn't know before.

So back to the issue, I tried adding the following to the Holder (and later the Page) but it still did not return anything.

function ImageGalleryPageImage($num = 5 ) { 
    $results = $this->getComponents('ImageGalleryPageImages', '', '', '', $num);
    return $results;
}		

I think it is something do do with the scope of $this in the depth of the control loops, but I have no idea how to reach in and get the $this for the current ImageGalleryPage. I think if I can get the ID or just get at current object then I will be all set, but to get at it is the challange.

Thanks for all the help to date.
Ed

Avatar
Webbower

Community Member, 10 Posts

29 January 2011 at 12:20pm

$this should apply to the current scope in the PHP. It has no bearing on the template. $this refers to the class you're writing code in.

Is that ImageGalleryPageImage() method in the Controller or the Page subclass? Try moving it to the Page subclass if it's in the Controller right now.

Avatar
monkeyben

Community Member, 25 Posts

29 January 2011 at 3:41pm

Sorry if this is rubbish and/or typo ridden but its 2:37am and I'm on my tiny android phone with rubbish cut and paste...

If you put a custom getter in the GalleryPage class, like the get getChildren() function in this thread: -
http://silverstripe.org/template-questions/show/9244
But replace 'children' with 'GalleryPageImages' that should work.

Avatar
monkeyben

Community Member, 25 Posts

29 January 2011 at 3:46pm

Aram first example of getChildren that is

Go to Top