Processwire - Repeater Fields
Aus Wikizone
Version vom 1. Februar 2021, 20:03 Uhr von 109.192.169.230 (Diskussion)
Repeater Field (Core Funktion)
Maintains a collection of fields that are repeated for any number of times...
Hinweis: Alternativen sind: ProFields Multiplier and Table. Repeater sollte man nicht einsetzen wenn es auf Performance ankommt. Also bei vielen Items lieber Pages, PageTable, Multiplier...
https://processwire.com/api/fieldtypes/repeaters/ https://processwire.com/talk/topic/1855-repeater-fields-how-to/
Ist ein Field Type, um mehrere Felder in einem Zusammenzufassen und diese zu wiederholen. Anzahl der Daten ist beschränkt, da bei einer Änderung immer die kompletten Daten der Seite betroffen sind. ProFields sind u.U. effizienter im Sinne der Datenbankabfragen. Multiplier muß ich noch testen.
Repeater über API
https://processwire.com/talk/topic/1855-repeater-fields-how-to/
Im Wesentlichen kann man Repeater wie ein PageArray behandeln.
Hier ein Beispiel von Soma https://gist.github.com/somatonic/5391391
repeater_example.php
<?php
$mypage = $pages->get("/about/");
if($input->post->submit){
$n = 1;
$title = "element_title_$n";
$url = "external_url_$n";
$mypage->setOutputFormatting(false);
while($input->post->$title){
// on first round remove all repeater items, to keep things easy.
// teasers is the repeater field
if($n == 1) $mypage->teasers->removeAll();
// create new repeater item
$item = $mypage->teasers->getNewItem();
// populate any fields in the repeater you want
$item->element_title = $input->post->$title;
$item->external_url = $input->post->$url;
// save the repeater item (a page itself)
$item->save();
// add the repeater item to the page
$mypage->teasers->add($item);
// update counter and field names
$n++;
$title = "element_title_$n";
$url = "external_url_$n";
}
if($n > 1) {
// save the page
$mypage->save('teasers');
}
}
?>
<form name="test" method="post">
<?php $count = 1; foreach($mypage->teasers as $key => $t): ?>
<input type="text" name="element_title_<?php echo $key+1?>" value="<?php echo $t->element_title?>"/>
<input type="text" name="external_url_<?php echo $key+1?>" value="<?php echo $t->external_url?>"/><br/>
<?php $count++; endforeach; ?>
<input type="text" name="element_title_<?php echo $count; ?>" value=""/>
<input type="text" name="external_url_<?php echo $count; ?>" value=""/><br/>
<input type="text" name="element_title_<?php $count++; echo $count; ?>" value=""/>
<input type="text" name="external_url_<?php echo $count; ?>" value=""/><br/>
<input type="submit" name="submit" value="submit"/>
</form>