Skip to content

Instantly share code, notes, and snippets.

@biojazzard
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save biojazzard/654e9f00faacf419d952 to your computer and use it in GitHub Desktop.
Save biojazzard/654e9f00faacf419d952 to your computer and use it in GitHub Desktop.
Repeater Field to PageTable.
<?php
/* If You use it in a template file */
$useMain = false;
/* Repeater to PageTable */
/* Select the pages with the Repeater Field we want to migrate. Vía:Template Name */
$pagesTemplate = "checklist";
/* Repeater we wanto to migrate */
$repeater_field = "links_repeater";
/* pageTable:fieldName === {Pageset:Template:Parent} */
/* Template of the Parent Page that will hold old repeaters as Page Children */
$ParentPageTemplate = "links";
/* pageTable:Template === {Pageset:Template:Children} */
/* Template of the Page that will have the old repeater data */
$ChildrenPageTemplate = "link";
/* pagesWithRepeater:PageArray */
$pagesWithRepeater = $pages->find('template='.$pagesTemplate);
foreach ($pagesWithRepeater as $key => $repeaterItems){
/* CAUTION */
/* OK if all the Fields are the same.
foreach($repeaterItems->fields as $repeaterItemField){
$npt->$repeaterItemField = $repeaterItems->$repeaterItemField;
$repeaterItems->{$ParentPageTemplate}->add($npt);
}
*/
/* CUSTOM IMPORTED DATA */
/* Normally it´s the case: you don´t want old Repeater Names like dates-plush-hashes */
foreach ($repeaterItems->{$repeater_field} as $repeaterItem) {
$npt = new Page();
$npt->of(false);
$npt->template = $ChildrenPageTemplate;
$npt->parent = $pages->get('template='.$ParentPageTemplate);
$repeaterItems->of(false);
echo '<pre>';
echo '$npt->title ==>'.$repeaterItem->subject;
echo '<br>';
echo '$npt->name ==>'.$sanitizer->name($repeaterItem->subject);
echo '<br>';
echo '$npt->link ==>'.$repeaterItem->link;
echo '<br>';
echo '$npt->class_name ==>'.$repeaterItem->class_name;
echo '</pre>';
/* CUSTOM IMPORT VALUES */
$npt->title = $repeaterItem->subject;
$npt->name = $sanitizer->name($repeaterItem->subject);
$npt->link = $repeaterItem->link;
$npt->class_name = $repeaterItem->class_name;
/* Saves the new Page */
$npt->save();
/* Put the Repeater value as a Page instance of the new */
$repeaterItems->{$ParentPageTemplate}->add($npt);
/* Copy the references to the new created pages to the Page. */
$repeaterItems->save();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment