Jump to:

23455 Posts in 18904 Topics by 2877 members

General Questions

SilverStripe Forums » General Questions » Holder Pages showing limited number of Child Page Elements

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: 4171 Views
  • edk
    Avatar
    Community Member
    39 Posts

    Holder Pages showing limited number of Child Page Elements Link to this post

    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

  • Webbower
    Avatar
    Community Member
    10 Posts

    Re: Holder Pages showing limited number of Child Page Elements Link to this post

    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.

  • edk
    Avatar
    Community Member
    39 Posts

    Re: Holder Pages showing limited number of Child Page Elements Link to this post

    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

  • Webbower
    Avatar
    Community Member
    10 Posts

    Re: Holder Pages showing limited number of Child Page Elements Link to this post

    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.

  • edk
    Avatar
    Community Member
    39 Posts

    Re: Holder Pages showing limited number of Child Page Elements Link to this post

    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

  • Webbower
    Avatar
    Community Member
    10 Posts

    Re: Holder Pages showing limited number of Child Page Elements Link to this post

    $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.

  • MonkeyBen
    Avatar
    Community Member
    25 Posts

    Re: Holder Pages showing limited number of Child Page Elements Link to this post

    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.

  • MonkeyBen
    Avatar
    Community Member
    25 Posts

    Re: Holder Pages showing limited number of Child Page Elements Link to this post

    Aram first example of getChildren that is

    4171 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.