Zahari M. Posted April 22, 2014 Share Posted April 22, 2014 Hi Guys! Hope you can help! I have a repeater. Lets call it repeater_a. repeater_a has some fields attached to it. It has one image field called image_1. This is set to be a single image field. It has two text fields, text_a and text_b. In use, one entry of this repeater may have an image and one text field, another entry may not have an image but may have say 2 text fields... This repeater is not for a photo gallery or meta data. Think of it as ingredients of a recipe! The ingredients may include an entry that might have a picture. Now to add some further complication / clarification to the desired usage.. the pages that this repeater appear on are effectively child pages of a parent. So on the parent page, we wish to loop thru all the child pages and extract the first repeater image that appears (if any) for each child page. Conceptually I'm doing this: foreach ($childpages as $childpage) { foreach ($childpage->repeater_a->image_1->first() as $img) { echo $img; } } And Im ending up with this error: Error: Call to a member function first() on a non-object (line 50 of /Applications/MAMP/htdocs This question may been asked before here, but it was suggested that the usage there wasnt quite applicable to repeaters and their wasn't quite a clear cut answer to the problem! Any suggestions what Im doing wrong / how to do it right would be very much appreciated! Cheers guys! Link to comment Share on other sites More sharing options...
DaveP Posted April 22, 2014 Share Posted April 22, 2014 It has one image field called image_1. This is set to be a single image field. I think that's your problem. You don't need to call first() on a field set to a single image. Link to comment Share on other sites More sharing options...
Zahari M. Posted April 22, 2014 Author Share Posted April 22, 2014 Hey DaveP Thanks for chiming in. Much appreciated. After trying many permutations, in the end, it doesn't look like image->first() is usable with repeaters. And so by referring to the thread I linked to earlier, I adopted the loop and counter approach and that gets me my first image! Thanks again Cheers Link to comment Share on other sites More sharing options...
adrian Posted April 22, 2014 Share Posted April 22, 2014 You can definitely use first within repeaters. Try this: foreach ($childpages as $childpage) { foreach ($childpage->repeater_a as $ra) { echo $ra->image_1->first()->url; } } Link to comment Share on other sites More sharing options...
Zahari M. Posted April 22, 2014 Author Share Posted April 22, 2014 Hi Adrian Ah! Good to know. I will give it a try and see! What I ended up doing was modifying the loop I referred to earlier such that it iterates through the repeater and finds the first populated image field. This is what my solution looks like thus far: if ($entry->media_repeater) { // Check to see if there is an actual repeater $i = 0; foreach ($entry->media_repeater as $fields) { if ($fields->repeater_image) { $i++; if ( $i == 1 ) { $img = $fields->repeater_image; $content .= "<div class='img-featured'>"; $content .= "<img class='img-style2' src='{$img->url}' alt='{$img->description}' title='{$img->description}'>"; $content .= "</div>"; } } } } I will try out your approach and see. Always learning! Thanks again. Cheers! Link to comment Share on other sites More sharing options...
Zahari M. Posted April 22, 2014 Author Share Posted April 22, 2014 Hi Adrian Tried what you suggested but not much luck..... My earlier examples were named with conceptual variables . My actual variables are as below and I am omitting the child pages loops: foreach ($entry->media_repeater as $fields) { $img = $fields->repeater_image->first()->url; $content .= "<img src='{$img}' >"; } I believe the code above reflects the spirit and intent of your example, yet I end up with this error: Exception: Method Pageimage::first does not exist or is not callable in this context I've been juggling and trying to use ->first() all day and had come across this error before. Anyways... it's probably me not doing something right. But at least I worked out how to use a counter to get done what I needed. This is the first counter I have worked on / created so kinda thrilled I figured it out. Thanks again so much for looking!! Cheers Link to comment Share on other sites More sharing options...
adrian Posted April 22, 2014 Share Posted April 22, 2014 Firstly, you shouldn't use "$fields" as this is a PW variable (http://processwire.com/api/variables/fields/) Even so, that shouldn't actually stop things working. The error you are getting with that code does point to the issue that Dave mentioned regarding the max files allowed setting for the repeater_image field. If that is set to "1" then you don't need, and can't use first(). Does that fix things? Link to comment Share on other sites More sharing options...
Zahari M. Posted April 22, 2014 Author Share Posted April 22, 2014 Hi Adrian! Thanks for catching the $fields bit! Now I know! I did mention that it was a single image field. And so as you point out this image field was set to "1". And this in turn was creating the error. As I desire my repeater to use an image field set to "1", the loop and counter does exactly what I need. Phew! So I guess it might be best to think of it that ->first() is indeed available for us in repeaters... just as long as the image field is not set to "1"... Thanks so much again! Cheers! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now