Jump to content

Can't figure out how to create a repeater through the API


kasperwf
 Share

Recommended Posts

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.  :mellow:

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!)  :lol:  :lol:  :lol:

Link to comment
Share on other sites

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

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:

  1. Using $p instead of $page as it's not good practice to overwrite $page as it refers to the current page
  2. I am saving $socialphoto and not $page/$p
  3. You already figured this out, but you need to save straight after getNew() to be able to add items to the repeater
  4. 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.

  • Like 2
Link to comment
Share on other sites

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

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';
  • Like 2
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

  • Recently Browsing   0 members

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