Jump to:

22977 Posts in 11747 Topics by 2826 members

General Questions

SilverStripe Forums » General Questions » Limit Control Pain **SOLVED**

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

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

Page: 1 2
Go to End
Author Topic: 617 Views
  • ambient
    Avatar
    Community Member
    113 Posts

    Limit Control Pain **SOLVED** Link to this post

    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?

  • IOTI
    Avatar
    Community Member
    187 Posts

    Re: Limit Control Pain **SOLVED** Link to this post

    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 %>

  • ambient
    Avatar
    Community Member
    113 Posts

    Re: Limit Control Pain **SOLVED** Link to this post

    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 %>

  • ambient
    Avatar
    Community Member
    113 Posts

    Re: Limit Control Pain **SOLVED** Link to this post

    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??

  • IOTI
    Avatar
    Community Member
    187 Posts

    Re: Limit Control Pain **SOLVED** Link to this post

    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.

  • ambient
    Avatar
    Community Member
    113 Posts

    Re: Limit Control Pain **SOLVED** Link to this post

    IOTI, I think maybe I love you!

    Thanks so much, it worked perfectly

  • IOTI
    Avatar
    Community Member
    187 Posts

    Re: Limit Control Pain **SOLVED** Link to this post

    Great! Happy to hear it worked

  • ambient
    Avatar
    Community Member
    113 Posts

    Re: Limit Control Pain **SOLVED** Link to this post

    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?

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