Jump to content

Automatically delete repeater fields


countchocula
 Share

Recommended Posts

Is there a way to automatically remove repeater fields after a certain date?

My homepage has a repeater field on which has several items which link to different pages on my site. Each page is for a different concert so after the concert has passed then those pages are automatically set to hidden.

The issue is that the repeater field item on the homepage that links to each concert page is not automatically hidden when the concert passes so unless it is manually removed then you get a dead link from the homepage. That wouldn't be a problem but the site isn't looked at every day and so occasionally things get left on the homepage that have passed.

Let me know if that isn't clear, I am not the best at explaining sometimes. 

Link to comment
Share on other sites

I think it's a matter of how you are going to output the repeater's contents. One possibility: add a date field to the repeater's items. If this date has reached, the item is not displayed anymore. Some untested code:

<?php

$home = $pages->get('template=homepage');

if (count($home->myRepeater)) {
    foreach ($home->myRepeater as $concert) {
        if ($concert->date < time()) {
            continue;
        }
        // output any other concert information from here on
    }
} else {
    echo 'Sorry, no concerts, pal.';
}

Another idea: You could check if the page to which the repeater links to (I suppose via page field) is still published. Some code:

<?php

$home = $pages->get('template=homepage');

if (count($home->myRepeater)) {
    foreach ($home->myRepeater as $concert) {
        $concert_page = $concert->link;
        if ($concert_page->id && $concert_page->is(Page::statusUnpublished)) {
            continue;
        }
        // output any other concert information from here on
    }
} else {
    echo 'Sorry, no concerts, pal.';
}

I hope I did get you right and the above code works and is of any help for you. Had no time to set it up and test it out properly.

Link to comment
Share on other sites

Thank you, your post really helped. I couldn't seem to get the $checkstatus->is(Page::statusUnpublished) to work (even when changing it to statusHidden) but what I did was this:

foreach (...) {
$checkstatus = $homeblock->PageLink->status;

if ($checkstatus == 1) {
echo 'show stuff';
}

else {
echo 'nothing to show'; 
}

}

Not sure if that is the most economical way to do it but it seems to work. $homeblock->PageLink->status outputs a number for each repeater item (1 = published) and doesn't show anything that isn't published.

Now i just need to figure out a way to tidy up the backend

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...