Jump to:

3397 Posts in 978 Topics by 854 members

Template Questions

SilverStripe Forums » Template Questions » How to get only this year's child pages in list

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

Page: 1
Go to End
Author Topic: 628 Views
  • Bronwyn
    Avatar
    Community Member
    16 Posts

    How to get only this year's child pages in list Link to this post

    Hi, I'm not very experienced at Silverstripe, and the guy who helped me build our uni department's site was a student here and has gone. This is probably really easy but it's driving me nuts.

    We have news articles, which are held in a news holder page, but the three most recent news articles are also displayed on our homepage. Because of this, and because we want the articles to be displayed in order of time (most recent first) instead of the order they appear in the CMS , we are not using <% control Children %> to display the news articles. Instead, we have a function which lives in the NewsHolder Controller:
       
    function LatestNews() {
    $news = DataObject::get_one("NewsHolder");
    return ($news) ? DataObject::get("NewsArticle", "ParentID = $news->ID", "Date DESC", "") : false;
    }

    This was all very good, and worked well until I decided I needed to archive some of the articles into separate newsholder pages. I created the new pages and moved the articles into the appropriate parents, and then of course, each parent displayed every news article. I solved this problem, mostly, by making a new page type for archives which just uses control children instead of control LatestNews. But, I am STILL getting one article from one of the archive pages displaying at the bottom of my list. Here is a link. It's a cached page, but looks the same even if you look at the non-cached version. http://biochem.otago.ac.nz/news/

    So I need help. I can't understand why I am getting one article from 2009 at the bottom of this newsholder page. It is definitely a child of the 2009 archive page, and there is only one copy of it. I've save-and-published numerous times and the bloody thing still keeps turning up.

    A second issue is functions. I tried to work around the above by making a function that would return true if the date on the news article was from the current year, but it's just not working and I'd like to know why - even if I can get rid of that article in a more sensible fashion.

    This is the function I made:

    function ThisYear() {
    $date = $this->Date;
    if ($date->Year() == ($now->Year())){
    return true;
    } else {
    return false;
    }
    }

    I wasn't sure where to put it, so I tried it in the controller part of my news article page. The database rebuilds OK, but I don't get any articles showing up when I use <% if ThisYear %> in my template page. Can someone please tell me what I'm doing wrong? I do have a Date field in my news articles.

  • Howard
    Avatar
    Community Member
    215 Posts

    Re: How to get only this year's child pages in list Link to this post

    I may be gone but you're not forgotten.

    Sounds like the "Grants a delight to researchers" page may have had some sort of anomaly in the database when it was moved and so there is a Live version somewhere in the table which has the old parent still set. You can see that there is some sort of duplication as both of these URIs work: http://biochem.otago.ac.nz/grants-a-delight-to-researchers/ and http://biochem.otago.ac.nz/grants-a-delight-to-researchers-2/

    I had a look in the CMS (yes my login still works) and the original definitely isn't in the sitetree. I don't know how you feel about having a look at the actual database and looking for the duplicate, the one without the '-2' on the end is the one that you want. If you delete other one then it will disappear. Make a backup of the database before you go messing with it.

    In terms of your function question it depends on where you put it and where you call it in the template. Where it is now you need to make sure that when you put it in the template it is in the scope of the NewsArticle not the HolderPage i.e.

    <% control Children %>
    <% if ThisYear %>
    $Title Whatever
    <% end_if %>
    <% end_control %>

    If I was still there we would have done this a different way - we could have just made a function that gets that pages children and sorts them in order by Date.
    Something like

    function ThisPagesLatestNews() {
    return DataObject::get("NewsArticle", "ParentID = $this->ID", "Date DESC", "");
    }

    Let me know how you go.
    Howard

  • Howard
    Avatar
    Community Member
    215 Posts

    Re: How to get only this year's child pages in list Link to this post

    Hah found it - the navigation label had changed. Look for the title "Dr Stephanie Hughes gains Neurological Foundation grants" under the news page. I dont have the permissions to be able to delete it but if you delete it, its saves you messing with the database.

  • Bronwyn
    Avatar
    Community Member
    16 Posts

    Re: How to get only this year's child pages in list Link to this post

    Thank you Howard. It's very comforting to know you are still there watching over me.

    I did call that function from inside the control, but it was control LatestNews (using the other function). It didn't find anything.

    <% control LatestNews %>
    <% if ThisYear %>
    Rest of stuff
    <% end_if %>
    <% end_control %>

    I hate to say it Howard, but it was you who made the current function for getting latest news and putting it on the page in order of time. I could never have done it. I might try with the function you've made though. See how it goes.

    What are you up to these days? Not Biochemistry I assume?

  • Howard
    Avatar
    Community Member
    215 Posts

    Re: How to get only this year's child pages in list Link to this post

    Did you manage to get it working?

    I'm over in Aus now studying med, keeping me busy

  • Bronwyn
    Avatar
    Community Member
    16 Posts

    Re: How to get only this year's child pages in list Link to this post

    Haven't tried it yet. Am stuck on something else, which you might be able to help me with, but it shouldn't be part of this thread so move it or send me your email so I can communicate directly. I'm trying to get the ImageGallery thing going in the Dept site. It wouldn't work with the downloaded module, but it did when I copied over the one you got working on the polar research theme site. So that's all good, but there are a couple of issues. You did something on the polar research site to get the pretty pics popping up when you click on a gallery. It can't have been part of the ImageGallery module or it would work with the one I copied over. Must have been somewhere else. At present instead of getting a nice popup pic you get sent to an ordinary webpage with the pic on it. I remember that this did happen before until you fixed it. You must have fixed it anyway, because I don't know how to.
    The other issue is that this image gallery wants to be a child of a Lab page. So it wants to have the custom banner. Can I get it to work? No I can't. I've got the banner-choosing tab in the CMS working, I've made the image gallery page an extension of the GenericLabPage, so it SHOULD be doing all the things that does, but no dice. Can you help?

    Update - I got the banner working by making top-level ImageGalleryPage.ss and ImageGalleyPage_album.ss in my theme folder, pasting the contents of the original module pages into copies of the top level GenericLabPage.ss. Clumsy, but at least it works. A side effect of this is that all of a sudden the pretty pop-up photo starts wanting to work too. When you click on a thumbnail on an album page a pretty black mist descends as if it's all going to work beautifully, but then it can't find the photo it's supposed to be superimposing. I've tried moving all of the contents of the Image Gallery (except the code folder) module into my theme folder but that doesn't fix it. I'm guessing one or two of those folders wants to be elsewhere - do you know where?

    Update again - it works. Changed the configuration from prettyphoto to lightbox. That works fine.

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