Jump to content

Repeatable Fields


ryan

Recommended Posts

Having said that, I have never had an issue with the image field, max file setting or displaying images EXCEPT when an image field is included in a repeater.

An image field shouldn't perform any differently in a repeater. Or if it is, it's not supposed to. :) Can you go into more detail about the difference you noticed--you've got me wondering if there's a bug somewhere.

Link to comment
Share on other sites

The image field is well documented http://processwire.c...ldtypes/images/, and it's not different in a repeater.

That's not what I've found. I've used the image field as part of a gallery with a max files setting, as part of a gallery without a max file setting and as a single image and have not had any problem as when I used it in a repeater. If it were no different I wouldn't have to treat it differently.

Maybe it's because it's a FieldtypeMulti inside a repeatable field that does it, what do I know but it's not the same in my experience.

Link to comment
Share on other sites

An image field shouldn't perform any differently in a repeater. Or if it is, it's not supposed to. :) Can you go into more detail about the difference you noticed--you've got me wondering if there's a bug somewhere.

I'm not an expert Ryan, I barely know what I'm talking about. All i can say is that I've used the image field on it's own many times now and I am fairly comfortable with it but when I use it as part of a repeatble field I have trouble making it work and it's not because i forget how from one day to the next. If I can find some time in the next day or two I'll try to post examples.

Link to comment
Share on other sites

I'm using repeaters and images and didn't notice anything different from images outside repeaters.

I just tested to see if multiple images works. Everything works like a charm.

// my single image field

foreach($page->teasers as $teaser) {
if(count($teaser->image)) echo "<img src='{$teaser->image->size(0,100)->url}'/>";
}

// changed to multiple
foreach($page->teasers as $teaser) {
if(count($teaser->images)) echo "<img src='{$teaser->images->first()->size(0,100)->url}'/>";
}

Images get rendered as it should.

Link to comment
Share on other sites

I'm using repeaters and images and didn't notice anything different from images outside repeaters.

I just tested to see if multiple images works. Everything works like a charm.

// my single image field

foreach($page->teasers as $teaser) {
if(count($teaser->image)) echo "<img src='{$teaser->image->size(0,100)->url}'/>";
}

// changed to multiple
foreach($page->teasers as $teaser) {
if(count($teaser->images)) echo "<img src='{$teaser->images->first()[->size(0,100)->url}'/>";
}

Images get rendered as it should.

See, it's you that doesn't understand the difference, but there it is. When working with the image field on other sites with galleries:

 	 $pics = $page->my_pics;
foreach($pics as $pic){
echo "<li><a href='{$pic->url}' class='pic' title='{$pic->description}'><img src='{$pic->size(85,85)->url}' alt='{$page->name} photo' /></a></li>\n";
}

Multiple images, max file set to 16, it works. But on the site I am currently working on that I posted about yesterday (August 14) where the image is in a repeater:

foreach($page->get("product") as $item) {
echo "<div><a class='nowandthen'><img src='{$item->product_shot->eq()->url}' alt='{$item->product_shot->eq()->description}'>";
}

It seems minor to you but to someone outside the scope of PHP, processwire or database driven cms development that is the difference between it works and it doesn't. You don't see it because you know it so well it doesn't even occur to you that it might be a big deal to leave that out.

It's not a bug, it inexperience. I'm learning this because static html websites are a thing of the past and I need a cms that works the way I want to work rather than forcing me to work the way it wants me to. Processwire is the ONLY system out there I've found that does that. Problem solved.

Link to comment
Share on other sites

But do you understand it now?

I think the real problem is in that we use same terms with different meanings. Some know more some less. And sometimes we forget about it so it's sometimes really hard to understand and find out what some people are asking and what the problem is. Some deal different with it than others and some keep out. I must confess I might took some things for granted and in my world and assumed you would understand. So don't take that personal or anything like that. And if something doesn't make sense what I'm saying we can try work it out. I'd like to help you really.

Your 'different' looks like you mean 'code' is different. The 'different' we were talking about was that image field can be either of single or multiple thus the code has to be different. You also was speaking about that it would be 'different' in repeaters... So actually it's still the same BUT the repeater add a level or complexness (is that even a word?) to it I think you might not aware of. Looking at your two examples of code you seem to miss that those, both foearch loops to iterate the entries of an array, the first on is a image field with multiple images, and the second example is the repeater field (which is also an array)

So let's assume you have in both examples an image field of multiple.

foreach($page->my_pics as $pic){
 //$pic is now one of $page->my_pics
 echo "<li><a href='{$pic->url}' class='pic' title='{$pic->description}'><img src='{$pic->size(85,85)->url}' alt='{$page->name} photo' /></a></li>\n";
}

Multiple images (if present) will get outputed.

And this if it's in a repeater. We need to add another foreach wrapping the previous example.

foreach($page->get("product") as $item) {
 // $item is now an entry from $page->product (repeater)
 // too access the images we now iterate $item->my_pics (images)
 foreach($item->my_pics as $img) {
   // $img is and image from $page->my_pics (inside repeater)
   echo "<div><a class='nowandthen'><img src='{$img->url}' alt='{$img->description}'>";
 }
}

This cycles all repeater elements (if any) and output each image from the my_pics field.

Now, if the image field is set to max "1" in the field setting, so you can only upload 1 image. The whole would look different as there's no foreach needed. But image field still work the same.

//$page->my_pic is now only singular so no foreach needed as it's not an array
echo "<li><a href='{$page->my_pic->url}' class='pic' title='{$page->my_pic->description}'><img src='{$page->my_pic->size(85,85)->url}' alt='{$page->my_pic->name} photo' /></a></li>\n";

Now the same in a repeater (product). BTW instead of $page->get("product") you can also just use $page->product, which is the same.

foreach($page->product as $item) {
 echo "<div><a class='nowandthen'><img src='{$item->my_pic->url}' alt='{$item->my_pic->description}'>";
}

One more. If the image field is again multiple >1 or 0 max files. You can also without foreach loop accessing one fromt he array directly. That's where ->first() comes into play. This also should be possible to use on repeaters to get the first repeater element from an array. Or it also can be used on Page arrays.

foreach($page->product as $item) {
 echo "<div><a class='nowandthen'><img src='{$item->my_pics->first()->url}' alt='{$item->my_pic->firs()->description}'>";
}

I'm not sure if that does make something clear for you. Let us know if still something unclear.

Link to comment
Share on other sites

level or complexness (is that even a word?)

I'm not of much help on the rest of this topic, but since you asked Soma I think the phrase you are looking for is "level of complexity".

Your English is still infinitely better than my Swiss ;)

  • Like 1
Link to comment
Share on other sites

  • 2 months later...

If I know the repeater page id, is there way to get the actual page where the repeater field is?

Some background: I am adding product variations to pw-shop using repeater field. Shopping cart then get's the repeater page id as a product. But since product variation title is something generic like "Size: XL" or "Color: black" I would like to show the actual product title there also.

Ok, it seems that parent() actually returns the page I am looking for (I was thinking that it would return the hidden page from the tree). Excellent!

Link to comment
Share on other sites

Hmm.. this is confusing.

$product	 = $this->pages->get($repeater_page_id);
echo $product->parent->id; // Outputs: 92641327 (there ain't so many pages..)
echo $product->parent->title; // Outputs: testproduct (this is why I assumed I have right item - "testproduct" is the original page name

So how I know the page where my single repeater item actually is, if I only know the id of repeater item?

Link to comment
Share on other sites

Thanks Soma. I was thinking about the same, tough I think it would be nicer if variation title would be only appended on actual product title. If product title changes, that needs to be accounted etc - more code that way...

But if getting the product page through repeater item id is difficult, then I will take that solution also.

Link to comment
Share on other sites

Using children would require some code tweaks also (to ShoppingCart.module and ShoppingCheckout.module) to append the parent page title before variation title. If variation title is all that is needed, then it would be just the same with repeater.

I think this is perfect use case for repeater and much simpler UI than using children pages.

Link to comment
Share on other sites

You're right, but one little problem I found with current shop is that changing the product variation in Order Management is nearly impossible. Much easier if it's a child page you can chose from.

  • Like 1
Link to comment
Share on other sites

Thanks guys.

Diogo: This is great, I will get the id from there. Still, if there is cleaner way to do it, let me know!

Soma: Only shop I have build don't really use the backend (email confirmation is all they need), so that's the main reason for the lack of focus there so far. Hopefully we get more demanding shop client soon :D

Link to comment
Share on other sites

Much more clever, both of you :) I didn't think of getting the parent name and started exploding everything...

Diogo: what about after page id 9999?

That's the problem of doing only small sites... pf

:-*

Who is who? :D

Link to comment
Share on other sites

  • 4 weeks later...
$forPage = wire('pages')->get( (int) substr($product->parent->name, 9))

As of the latest commit to the dev branch, you no longer have to do this. You can now replace the above bit of code with this:

$forPage = $product->getForPage();

That returns the page that the repeater item is for. I also added this, should anyone ever need it:

$forField = $product->getForField(); 
  • Like 2
Link to comment
Share on other sites

  • 5 weeks later...

I'm pretty sure this must have been discussed somewhere, but I'm not able to find it? Is there any way to get my repeaters to have a different name than the repeaterfield itself? More specifically, I'd like the plural form on the field and the singular form on the instances created. Eg.

* Images

* Image #1

* Image #2

* Image #3

etc.

Link to comment
Share on other sites

  • 3 weeks later...

Hi everyone - only a couple of days in PW so far, but loving it.

Just wondering if there might be a way to name the headers of the collapsible repeaters in the page edit view and also have them collapsed by default. I don't think this is what woop is asking for. What I am trying to achieve is a personnel directory and as it stands, it becomes difficult to edit - once you get a lot of entries you end up with a lot of scrolling. If it was possible to set them to be collapsed to start with and then use values from one or more of the fields (ideally I'd like to concat first_name and last_name) as the title for the header bar.

I know it is possible to adjust the visibility for the field to "collapsed ...", but this collapses the entire repeater field, rather than the individual items.

I am thinking that I might actually be bette off going back to parent>child approach I had originally set up as I think the repeater approach will become a little unwieldy when there are lots of entries.

Anyone have any suggestions - am I missing some obvious setting?

Thanks

  • Like 1
Link to comment
Share on other sites

Making the labels configurable based on field values within them, as well as making them initially collapsed, have been mentioned a couple times. Given that, I think we'll have to add this the next time updates are made to the repeater Fieldtype. 

Repeaters aren't meant to scale infinitely, so if you need that kind of scalability repeaters aren't the best way to go. Though, you'll be glad to know we also plan to introduce pagination to repeaters eventually... so they will be more scalable from the admin interface side.

  • Like 1
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...