Jump to content

Repeatable Fields


ryan

Recommended Posts

Got it :)

When creating a page in the API its default status is published, whereas items entered into a repeater in the API seem to be unpublished and hidden. As such, you need to do the following to each item in the repeater:

$pageid = [your page generated via the API that contains the repeater] - must be saved before adding the repeater items in order to get the page ID
$repeaterpage = wire('pages')->get($pageid)->repeater_field->first();
$repeaterpage->removeStatus(Page::statusUnpublished);
$repeaterpage->removeStatus(Page::statusHidden);
$repeaterpage->save();

The code above only does it for the first item, but for my needs each repeater is only being imported with a single item for now and other items will be added over time - if you had more items in the repeater you would need to iterate through them of course.

Just needed some sleep and a morning coffee for this one ;) I should have 600+ files imported from a third-party file management script by the end of the weekend now - just got a wedding to go to in the meantime else I'd have this all wrapped up by lunchtime I reckon :)

  • Like 1
Link to comment
Share on other sites

Pete, sorry I didn't catch your message before you'd figured this out yourself. But it looks to me like you got it right. As you found, repeater pages are kept unpublished+hidden until set otherwise. This is because repeaters may keep one or more 'ready' pages, that are ready to be populated in the admin. If there weren't any ready pages, then your repeater_field->first(); would return null rather than a Page. So if your ready pages were set at 0 for that field, then you'd want to start a new repeater page like this:

$field = $fields->get('repeater_field_name'); 
$newRepeater = $field->type->getBlankRepeaterPage($page, $field); 
$newRepeater->status = Page::statusOn; // same as removing unpubished+hidden
// ...populate any values to repeater...
$newRepeater->save();

But you don't need to do this since you appear to have one or more ready pages already being created by the repeater, so your first() method works just fine. I still need to make a simpler API to the repeater field...

Link to comment
Share on other sites

No problem ryan - it was easy enough for me to work out in the end as I knew the repeater page was being created so there was only going to be one reason why it wasn't showing.

$newRepeater->status = Page::statusOn;

That's a bit easier - thanks!

Link to comment
Share on other sites

It took me awhile to figure it out, but think I've finally fixed the issue that was occurring when pages with repeaters were cloned. I was wondering if anyone else that is using repeaters in a test environment would mind testing it out too? The fix is in the latest commit. To clone, you install the ProcessPageClone module (comes with PW core) and then click "copy" on any of the pages in the page tree that are using repeaters. I just want to confirm that the fix works for other people in addition to me.

Thanks,

Ryan

Link to comment
Share on other sites

Tested before and after updating. Before updating all the repeater fields were deleted when i cloned the page. After the update it didn't happen anymore.

Link to comment
Share on other sites

@Diogo: Thanks for testing! Just to confirm, it's working how it should? It sounds to me like it is, but let me know if any of the behavior was unexpected.

@Pete: I've updated the API for adding repeater items so that it is now simpler. Lets say that you've got a repeater field called 'buildings', and you want to add a new building. Here's how you can do it now:

$building = $page->buildings->addNew(); 
$building->title = 'Sears Tower';
$building->height = 1400; 

$page->save();

That addNew() method can now be called on any repeater value. If there is a ready page waiting to be populated it returns that. If not, it will create a new page right there. It will be saved when you save your main $page. This hopefully makes the repeaters API a lot simpler.

  • Like 1
Link to comment
Share on other sites

Ryan, just tested it and it seems like I found a bug, though I'm not sure. Maybe it's just me doing something wrong) Cloning is working fine, but when I add new repeater item, then, for example, select image file and fill other fields, after I hit save I can't see my just added unit untill I click "Add item". I have ready-to-edit (unpublished) items set to 1. Then if I add second item, after saving I see only my second item. Haven't tested it further, don't have much time right now. Is it a bug? Can you reproduce this, guys?

  • Like 1
Link to comment
Share on other sites

Thanks for finding the bug. I was able to reproduce that with a repeater having just an images field, and then dragging in an image. The reason it wasn't working is because no changes to the page were actually made when you hit save. The changes were made before that via ajax (where the image was added). As a result, InputfieldRepeater didn't know to publish the item. I have fixed this and just pushed in the latest commit. Can you confirm that it fixes it on your end too? There was a JS update as well, and I forgot to update the module version, so you may need to hit 'reload' in your browser the first time you edit the page, just to be sure the new JS loads.

For the google maps field, are you talking about FieldtypeMapMarker? I wasn't sure, because there was another one made a long time ago that I think was literally called GoogleMaps. Just wanted to be sure I had the right one.

Link to comment
Share on other sites

When using search on a site, it shows up the repeater pages as well - is there a way to stop this as I suspect it's not supposed to happen?

I assume they show up only for superusers or do others see them as well?

Link to comment
Share on other sites

I've added a repeater to a website - unfortunately local - and the fields won't show up for guests. It works fine if I'm logged in as admin, but not for guest.

$quoteList = $pages->find("employee_quote!=")->shuffle();
$quoteList = $quoteList->shift();
$employeeQuote = $quoteList->employee_quote;
if ($employeeQuote) {
 $mainContent .= "<blockquote>{$employeeQuote}</blockquote>";
}

I've looked at all the options, but I must be missing something. Any ideas? Thanks for your thoughts!

Link to comment
Share on other sites

I have a repeater field called: "employee_quotes". I have a plain text field called: "employee_quote". The employee_quote lives within employee_quotes.

I was thinking this might be a rights issue, because when I'm logged in it works fine. As soon as I log out it doesn't work. Is it not possible to call direct a field within a repeater?

Link to comment
Share on other sites

It works fine, I'm using repeaters and haven't run into this issue.

I assume it's something with the permission on templates/pages that does not find the pages for guests.

$quoteList = $pages->find("employee_quote!=")->shuffle();

This is not on page but doing a find over ALL pages there is. If the pages with the repeater on it is somwhere the guest doesn't have access it won't find them. To test this you could try adding a ",include=all" to your selector. Does it change anything?

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...