Jump to content


Photo

Repeatable Fields


  • Please log in to reply
240 replies to this topic

#161 Soma

Soma

    Hero Member

  • Moderators
  • 3,217 posts
  • 1762

  • LocationSH, Switzerland

Posted 25 May 2012 - 03:40 AM

Sure you can

// get all pages that have at least 1 quote
$res = $pages->find("employee_quotes.count>0");

// add all quotes to an PageArray
$allquotes = new PageArray();
foreach($res as $r) $allquotes->add($r->employee_quotes);

// shuffle or whatever you please
$allquotes->shuffle();

// output all
foreach($allquotes as $q){
	echo "<p>$q->employee_quote</p>";
}


@somartist | modules created | support me, flattr my work flattr.com


#162 arjen

arjen

    Sr. Member

  • Members
  • PipPipPipPip
  • 340 posts
  • 125

  • LocationHoogeveen, The Netherlands

Posted 25 May 2012 - 03:57 AM

@soma, @diogo,

This is what I've been looking for. Thanks you very much! *double like this* So in retrospect it was my way of thinking. I thought that with the $pages->find you can also find the fields. Now I see it doesn't make sense at all the way I was thinking. I dig deeper into PW with PageArray().

Thanks all for your thoughts!
work will always be the curse of the drinking classes...

#163 diogo

diogo

    Hero Member

  • Moderators
  • 2,010 posts
  • 1089

  • LocationPorto, Portugal

Posted 25 May 2012 - 04:12 AM

$allquotes = new PageArray();


Ah that's how! I updated my code with this.

#164 tinacious

tinacious

    Full Member

  • Members
  • PipPipPip
  • 66 posts
  • 33

  • LocationToronto, Canada

Posted 30 May 2012 - 09:52 AM

This looks amazing. I was just thinking how I need to use this feature for a site I'm building and this is perfect. Thanks.

#165 digitex

digitex

    Full Member

  • Members
  • PipPipPip
  • 96 posts
  • 21

  • LocationOntario

Posted 15 June 2012 - 12:58 PM

This is interesting. I have been using the repeatable field for a little while and it's a huge benefit. I'm stuck though.

I have several pages with a repeater field and I want to display the fields, organized by page:

<h2>Page Title</h2>
    <ul>
	    <li>repeatable field info</li>
	    <li>repeatable field info</li>
	    <li>repeatable field info</li>
	    <li>repeatable field info</li>
	    <li>repeatable field info</li>
   </ul>
<h2>Page Title</h2>
    <ul>
	    <li>repeatable field info</li>
	    <li>repeatable field info</li>
	    <li>repeatable field info</li>
	    <li>repeatable field info</li>
	    <li>repeatable field info</li>
   </ul>
<h2>Page Title</h2>
    <ul>
	    <li>repeatable field info</li>
	    <li>repeatable field info</li>
	    <li>repeatable field info</li>
	    <li>repeatable field info</li>
	    <li>repeatable field info</li>
   </ul>


Like so. The array that Soma and diogo wrote seems like the ticket but does that just return the contents of the array as one big list or can it be organized by page? I have no idea how you'd do that.

#166 Soma

Soma

    Hero Member

  • Moderators
  • 3,217 posts
  • 1762

  • LocationSH, Switzerland

Posted 15 June 2012 - 01:27 PM

digitex, look at the markup and you see a somewhat 2 dimensional list. That's always done using nested foreachs.

foreach( $pages->get("/someparent/")->children() as $pa ) {
  // $pa is the page
  echo "<h2>$pa->title</h2>";
  echo "<ul>";
  // cycle the repeater field as if they are pages (they are)
  foreach( $pa->repeaterfield as $rp ) {
	echo "<li>$rp->info</li>";
  }
  echo "</ul>";
}

@somartist | modules created | support me, flattr my work flattr.com


#167 digitex

digitex

    Full Member

  • Members
  • PipPipPip
  • 96 posts
  • 21

  • LocationOntario

Posted 15 June 2012 - 02:36 PM

THANK YOU SOMA!

I thought it must be easier than I was making it and that something was just a little off on what I was doing. I had pretty much what you have (you showed me a few ways to simplify things) but it wasn't working. With your example to go by as correct, I figured there was something else wrong so I looked at my selector and found my problem.

Now all I need to do is glue the hair I pulled out trying to get this, back on my head. Thanks again.

#168 Soma

Soma

    Hero Member

  • Moderators
  • 3,217 posts
  • 1762

  • LocationSH, Switzerland

Posted 15 June 2012 - 03:18 PM

You're welcome. Haha, don't hurt yourself please! ;)

Just for throwing in when using php "Wire"-tag ( ;) ) short notation the above could also be something like this.


<? foreach( $pages->get("/about/")->children() as $pa ) : ?>

	<h2><?= $pa->title ?></h2>
	<? if( count( $pa->teasers ) ): ?>
		<ul>
		<? foreach( $pa->teasers as $rp ) : ?>
			<li><?= $rp->element_title ?></li>
		<? endforeach ?>
		</ul>
	<? else: ?>
		<p>No entries found</p>
	<? endif ?>

<? endforeach ?>

(short tags needs to be enabled on server, but only few hostings have it disabled. In near future it will always be enabled in php5.4)

@somartist | modules created | support me, flattr my work flattr.com


#169 diogo

diogo

    Hero Member

  • Moderators
  • 2,010 posts
  • 1089

  • LocationPorto, Portugal

Posted 15 June 2012 - 03:21 PM

Nice example in the "Wire" templating language ;)

#170 tomaszsobczak

tomaszsobczak

    Newbie

  • Members
  • Pip
  • 2 posts
  • 1

Posted 05 July 2012 - 02:54 AM

Iam using in one project Repeatable Fields inside another Repeatable Fields. Everything works find except image uploads in inner Repeatable Field.
Any solution for that?

#171 apeisa

apeisa

    Hero Member

  • Moderators
  • 2,528 posts
  • 856

  • LocationVihti, Finland

Posted 05 July 2012 - 04:32 AM

How does it fails? Does firebug or chrome console give you any error messages?

Also: are you sure you really need repeater inside a repeater? Of course depends on your use case - it very well might be the best possible solution here.

#172 tomaszsobczak

tomaszsobczak

    Newbie

  • Members
  • Pip
  • 2 posts
  • 1

Posted 05 July 2012 - 04:52 AM

How does it fails? Does firebug or chrome console give you any error messages?

Also: are you sure you really need repeater inside a repeater? Of course depends on your use case - it very well might be the best possible solution here.


Processwire returns this on upload
{"error":false,"message":"Page not saved (no changes)"}

I could end with subpages with repeaters on each of them but page structure is more logical if there would be possibility to use nested repeaters on it

#173 ryan

ryan

    Hero Member

  • Administrators
  • 5,780 posts
  • 3125

  • LocationAtlanta, GA

Posted 05 July 2012 - 12:16 PM

Iam using in one project Repeatable Fields inside another Repeatable Fields. Everything works find except image uploads in inner Repeatable Field.


Unfortunately I don't think that file fields can be nested beyond one level of repeaters. There is some necessary security going on behind the scenes that limits the scope of how deep an ajax request can go, and I think that's what you are running into here. The truth is that you may actually be able to get it to work if it weren't using ajax uploads (like if you were using Safari, which doesn't support ajax uploads). But I think you may be better off avoiding nested repeaters if possible. Nested repeaters are a rather complex thing to develop around and may be difficult to maintain vs. a simpler solution that doesn't rely on nesting. Still, you've got me curious and I do plan on experimenting here and trying to duplicate the next time I'm working with repeaters, just to be certain I'm right about the reason it's not working. If there is an easy way around it, I'll find it. Nested repeaters do sound fun. :) But either way, for practical purposes, I don't think it's good to build around a structure that requires nesting repeaters.

#174 jbroussia

jbroussia

    Full Member

  • Members
  • PipPipPip
  • 70 posts
  • 11

  • LocationFrance

Posted 11 July 2012 - 01:03 PM

Hi,

It seems I found another bug with repeaters (unless it was already reported, though I didn't find about it in his thread)...

Here is what I did:
- I created one repeater field named "sliders" containing 3 fields title, body and images
- I added the field sliders to my home template
- I added 3 elements of type sliders on my homepage (which uses the home template), filling each with a title, a body and one image
- then I went back to the Fields manager and created a new repeater field named features by cloning the sliders field so features will have the same 3 fields title, body and images

(so far so good)

- went back to editing my homepage, and here the problem starts...
- first it already contained 3 elements of type features being a copy of my 3 previously created elements of type sliders (fine because I needed 3 elements anyway, "I'll just have to edit them" or so I thought)
- but then if I edit any field of, say, "Features #1" element, the change is also made to element "Sliders #1" !
- and I can't edit elements from sliders type anymore ! if I save a change in sliders, it is not applied; if I save a change in features, it is applied to features and sliders

Any idea ? Maybe I should not clone a repeater field ?

#175 Pete

Pete

    Administrator

  • Administrators
  • 1,756 posts
  • 658

  • LocationChester, England

Posted 11 July 2012 - 01:31 PM

There was an issue a while back with cloning repeaters that has since been fixed. Are you using the latest version?

#176 jbroussia

jbroussia

    Full Member

  • Members
  • PipPipPip
  • 70 posts
  • 11

  • LocationFrance

Posted 11 July 2012 - 01:42 PM

Using 2.2.0.1. Going to check if this is the latest...

#177 ryan

ryan

    Hero Member

  • Administrators
  • 5,780 posts
  • 3125

  • LocationAtlanta, GA

Posted 11 July 2012 - 01:56 PM

@jbroussia: Thanks, I have looked into this and it is a bug. I think it may be possible to make repeater fields cloneable, but they are kind of a unique situation and the factors involved in cloning them are more broad and complex than with others fields. So my solution for this now is going to be to have it detect when it's being cloned and throw an exception to prevent it from happening. Longer term, I should be able to figure out when I've gone 1-2 days to wrap my head around it. :)

@Pete: He's talking about cloning fields rather than cloning pages. We did fix the issue with cloning pages that have repeaters. But cloning actual fields (Setup > Fields > some_repeater > Advanced > Copy/Clone) is a different thing.

Hope to find a way to make these cloneable. But since this is the first time it's come up, I don't think there's a lot of demand for cloning repeater fields. Unlike cloning pages with repeaters, which is probably a more common occurrence.

#178 jbroussia

jbroussia

    Full Member

  • Members
  • PipPipPip
  • 70 posts
  • 11

  • LocationFrance

Posted 11 July 2012 - 01:56 PM

Updated to 2.2.2, the problem remains.
I tried to delete all those repeaters and to recreate them; on my page I have a first repeater sliders and a second features which was created by cloning sliders. If I add a couple sliders items and save, the page will now have the same number of features items :'-(

EDIT: oh Ryan, you replied while I was typing this. That's fine, I will manually clone my repeater. Thanks :)

#179 Pete

Pete

    Administrator

  • Administrators
  • 1,756 posts
  • 658

  • LocationChester, England

Posted 11 July 2012 - 02:01 PM

Ah right, that will teach me for rushing to read a post and not doing it thoroughly :-[

#180 digitex

digitex

    Full Member

  • Members
  • PipPipPip
  • 96 posts
  • 21

  • LocationOntario

Posted 13 August 2012 - 05:10 PM

The repeater is still twitchy for me. I have a site where they sell buttons, and I need to be able to associate a name, description and image to each button. The repeatable field is perfect for that since there's only about 5 different buttons but I can't get the images to display.

foreach($page->get("product") as $item) {
if (count ($item->product_shot) > 1) {
echo "<div class='productcontainer'><div class='prodpic'><a class='nowandthen'><img src='{$item->product_shot->url}' alt='{$item->product_shot->description}'>
<img src='{$item->product_shot->url}' alt='{$item->product_shot->description}'></a></div>\n<div class='prodtext'><h2>{$item->product_name}</h2>\n{$item->product_description}</div></div>";
} else {
echo "<div class='productcontainer'><div class='prodpic'><img src='{$item->product_shot->url}' alt='{$item->product_shot->description}'></div>
\n<div class='prodtext'><h2>{$item->product_name}</h2>\n{$item->product_description}</div></div>\n";
}
}

I get
<img src='/pw/site/assets/files/1010/' alt=''>
in the markup while the name and description displays correctly.

I have almost the same set up in my own photography site for the galley where I have a name field and description associated with each image and it works (although I don't remember how I got it to work because it didn't at first either). is my selector wrong? "Product" is the repeater.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users