Jump to content

Convert Repeater to PageTable


Torsten Baldes
 Share

Recommended Posts

I'm using Repeater to give my editors the possibility to insert multiple instances of different types of content (dependencies ftw!) in one page.

This works really well for the most pages. But on some pages, the backend takes ages to load, because of to many repeaters.

The new PageTable module would probably speed things up for me. 

How could i convert my Repeater field to a PageTable field with all the content preserved?

Thanks!

  • Like 2
Link to comment
Share on other sites

This is really rough, but I have tested it and it does work :)

$p = $pages->get("/test/");

$rf = "repeater_test"; //repeater field name
$ptf = "pagetabletest"; //pagetable field name
$ptt = "basic-page"; //pagetable template name

foreach($p->$rf as $r){
    $npt = new Page();
    $npt->of(false);
    $npt->template = $ptt;
    $npt->parent = $p;
    foreach($r->fields as $f){
        $npt->$f = $r->$f;
    }
    $npt->save();
    $p->of(false);
    $p->$ptf->add($npt);
    $p->save();
}

The key thing is that for this to work as is, the repeater field must include a title field, because this is used to generate the names of the child pages for the pagetable entries. You could of course manually add a title field in the loop when creating the $npt page.

EDIT: You can ignore the need for a title by making the title hidden and not required in the context of the pagetable template and setting a value for the automatic page name format when setting up the pagetable field.

For this to work, the pagetable field must be already set up and ready to go.

If I had more time today, I would have enhanced this a little more regarding the title/name issue, but hopefully it should get you going.

Edited by adrian
mention option to avoid need for title field
  • Like 12
Link to comment
Share on other sites

  • 6 months later...

Well, I wanted to move some Repeater fields to PageTable.

I couldn´t use your script because I wanted it to traverse a PageArray.

So I totally rewrote your script, and added some comments.

 

You can use as a template file for any page.

 

Steps:

  • Create a new Categories/Category Structure in PW: BothTemplates & Parent Page (Categories) and the new PageTable Field: categories.
  • Assign the "new" PageTable Field to de templates where the Repeater is. The one we will migrate.
Here:

https://gist.github.com/biojazzard/654e9f00faacf419d952/ 

 

With love.

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