kasperwf Posted May 20, 2014 Share Posted May 20, 2014 Hi all Well, I feel kind of stupid here, since the documentation is so perfect and there's lots of answers on this exact topic to be found out there, but somehow I just can't seem to get this thing to work. I have a repeater field called socialphoto with three fields in it: * image * date * pinned (checkbox) So what I would like to do through the API, is to loop through a local folder in the site and add a repeater item for each image found. (this is the simplified explanation). Nevermind the folder structure, my test with just one test image doesn't work, so I'm actually at square one here. This is my code: $page = $wire->pages->get("/"); $socialphoto = $page->socialphoto->getNew(); $page->save(); $socialphoto->date = '27/10/2014'; $socialphoto->image->add('http://localhost/pw/site/apitest/test.jpg'); $socialphoto->pinned = 1; $page->save(); So what happens is, the home page actually gets a repeater added... so yaay, so far so good.. however... "date", "image" and "pinned" does not resemble my API code at all. The date is just today's date, and no image nor a checked checkbox. Can anyone please help me? By the way, I haven't been playing around with PW for about two years or so before today, and let me just say I'm amazed at what have happened while I have been away. SO freaking awesome! Excellent job Ryan (and contributors of course!) Link to comment Share on other sites More sharing options...
adrian Posted May 20, 2014 Share Posted May 20, 2014 This is what you need: https://processwire.com/talk/topic/5397-assigning-fields-to-repeaters-with-the-api/?p=52123 Link to comment Share on other sites More sharing options...
kasperwf Posted May 20, 2014 Author Share Posted May 20, 2014 Hmm.. but that looks like an example of how to create a repeater? My repeater already exists in the admin -> fields -> socialphoto, so all I want to do is to add a new repeater to the page (a page which also has the repeater field assigned to the template) for each image in a folder (In my example above, I'm just trying to add the repeater to the page and populate it with data. It's the populating with data part that I can't get right). Link to comment Share on other sites More sharing options...
adrian Posted May 20, 2014 Share Posted May 20, 2014 Sorry I quickly ready your title and not really the description. It's actually really easy, but I think the instructions here: http://processwire.com/api/fieldtypes/repeaters/ are slightly wrong in that they say to save $page. This works: $socialphoto = $p->social_photo->getNew(); $socialphoto->save(); $socialphoto->date = '2013-05-16'; $socialphoto->image->add('http://localhost/pw/site/apitest/test.jpg'); $socialphoto->pinned = 1; $socialphoto->save(); So the things I changed are: Using $p instead of $page as it's not good practice to overwrite $page as it refers to the current page I am saving $socialphoto and not $page/$p You already figured this out, but you need to save straight after getNew() to be able to add items to the repeater I am using a valid date format - maybe your format will work if that is how you have the field configured - I have never really tested those configs Hope that helps. 2 Link to comment Share on other sites More sharing options...
kasperwf Posted May 21, 2014 Author Share Posted May 21, 2014 Thanks Adrian! I'll give it a shot tonight and return with lots of praise when I nail it Link to comment Share on other sites More sharing options...
kasperwf Posted May 21, 2014 Author Share Posted May 21, 2014 And back I am! WITH lots of praise! :-D You're excellent Adrian! And here's another question for you: Can I add a description to the image? I tried $socialphoto->image->add('http://localhost/pw/site/apitest/test.jpg'); $socialphoto->image->description = 'Oh haaay'; but that didn't work Link to comment Share on other sites More sharing options...
adrian Posted May 21, 2014 Share Posted May 21, 2014 Despite the name "image", I am guessing it's "Maximum Files Allowed" setting is set to: 0 If it is set to one, then what you are doing should work, unless you have a $p->of(false) in there somewhere which would make the field behave as an array again. Either way, try this and it should work: $socialphoto->image->last()->description = 'Oh haaay'; 2 Link to comment Share on other sites More sharing options...
kasperwf Posted May 21, 2014 Author Share Posted May 21, 2014 You're a genius. Thank you so much Adrian, it works perfectly. 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