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

Limit Control Pain **SOLVED**


Go to End


10 Posts   1984 Views

Avatar
ambient

Community Member, 130 Posts

24 April 2013 at 10:42pm

Edited: 24/04/2013 10:43pm

Hi guys, I had this in the wrong forum I think so Ill see if anyone here can help :)
I'm using ss 2.4

I have a gallery of 9 images as dataobjects displaying fine in the gallery page.

GalleryPage.ss
[code[
<% if GalleryImages %>
<% control GalleryImages %>
//image here
<% end_control %>
<% end_if %>

I want to show the first 3 images of the gallery on the home page. But...

HomePage.ss

<% control GalleryImages(3) %> 

         //image here
<% end_control %>

Shows all 9 images instead of limiting it to only 3

I thought this would be straight forward but I've been pulling my hair out trying different things.
Is what I want even possible?

Avatar
copernican

Community Member, 189 Posts

25 April 2013 at 12:26am

Hey Ambient,

what you'll need to do is create a custom function for your homepage in HomePage.php.

class HomePage extends Page {


public function HomePageGalleryImages($limit=3){
    $galleryPage = DataObject::get_one('GalleryPage');
    $images = DataObject::get('GalleryImage', 'ParentID =' .$galleryPage->ID', NULL, NULL, $limit); //I'm assuming the DataObjects are called GalleryImage
    return $images;
}

}

The above code could be improved (should be using if statements to make sure the GalleryPage and the Images exist) but should help you get started.

On your HomePage.ss template, change the <% control %> block to use the new function

<% control HomePageGalleryImages %>
//image here 
<% end_control %>

Avatar
ambient

Community Member, 130 Posts

25 April 2013 at 2:19am

Hi IOTI,

I feel I've confused matters as my home page is actually a ProductGroup pagetype. Each 'product' has 9 gallery images(GalleryImage.php) and I'm trying to show only 3 on the ProductGroup page.

I have tried your suggestion and variations of it but it doesn't display any images.

Here is more of my code, I can't tell where I'm going wrong

GalleryImage.php

<?php
class GalleryImage extends DataObject 
{ 
   static $db = array ( 
      'GalleryImageTitle' => 'Text' 
   ); 
   static $has_one = array ( 
      'MyGalleryImage' => 'Page_Image', //relation needed for this DataObject 
      'BelongToProduct' => 'Product' //relation needed to point back to your pagetype, my pagetype is Product. 
   ); 
    
   public function getCMSFields_forPopup() 
   { 
      return new FieldSet( 
         new TextField('GalleryImageTitle'), 
         new FileIFrameField('MyGalleryImage') 
      ); 
   } 
    
    
}
?>

ProductGroup.ss (Now my home page)

<% control Products %>
  
    <!-- product info-->
    
    					
	<% control GalleryImages(3)  %> <!--show 3 images -->
                   //image here
        <% end_control %> 
                        
   
<% end_control %>

Avatar
ambient

Community Member, 130 Posts

25 April 2013 at 3:07am

Slowly making progress here

I have the code below in Product.php and it showing the first three images only except it does the same three images for all the products instead of the individual product images?

Product.php

public function GImages($limit = 3){ 
	
		if($images = DataObject::get('GalleryImage', $filter= '', $sort='', $join='', $limit="0,$limit")){ 
		return $images; 
		}
	}

I'm sure it's something to do with ID but I can't figure it out??

Avatar
copernican

Community Member, 189 Posts

25 April 2013 at 3:36am

Try

public function GImages($limit = 3){ 
      $filter = 'BelongToProductID =' . $this->ID; //filter
      if($images = DataObject::get('GalleryImage', $filter, $sort='', $join='', $limit="0,$limit")){ 
      return $images; 
      } 
   }

Using the new $filter, the function should only return GalleryImages that belong to the current Product being viewed.

Avatar
ambient

Community Member, 130 Posts

25 April 2013 at 3:48am

IOTI, I think maybe I love you!

Thanks so much, it worked perfectly :)

Avatar
copernican

Community Member, 189 Posts

25 April 2013 at 4:13am

Great! Happy to hear it worked :)

Avatar
ambient

Community Member, 130 Posts

26 April 2013 at 12:53am

Ahh, the images won't link back to their Product page :(

I've tried $Top.Link $Parent.Link $Link.Link

Is a function needed to get this to work since I'm basically asking a DataObject to link to it's parent page or do I just need to add something to the current function?

Go to Top