Jump to content

Migrate from Repeater to Pages


Pete Jones
 Share

Recommended Posts

Afternoon,

I have a page setup with a repeater which has 40 or so items in. Each repeater item has around 6 fields.

This is becoming a little unusable so I'd like to convert them to child pages of the current parent. Is there a way to do this (import/export maybe?)

Many thanks

Pete

Link to comment
Share on other sites

Hi,
I think to achive this, it might be the easiest if you create a new template with the 6 fields of your repeaters.
As repeaters are also just saved as pages, you can select them and iterate over the PageArray.
For each of the "repeater-pages" you can now create a new page of your new template (with your initial page as parent) and copy over the values to this new page.

Afterwards you can check the pages and delete your repeaters.

Greetings

Edit: Or see below for the easy way :)

Link to comment
Share on other sites


$rf = "repeater_field"; //repeater field name

$pt = "basic-page"; //page template name

foreach($p->$rf as $r){

    $np = new Page();

    $np->of(false);

    $np->template = $pt;

    $np->parent = $page;

    foreach($r->fields as $f){

        $np->$f = $r->$f;

    }

    $np->save();

}

Edited by adrian
Edited to set parent to current page and fix missing closing quote - thanks to @Sebii and @Christophe
  • Like 2
Link to comment
Share on other sites

I think just setting a new parent might be problematic, as repeaters don't really use a template (or a generic one?).
But it might be sufficient to change the parent and set the template (to a template using the same fields).

What Adrian suggested above however sounds very reliable.

Link to comment
Share on other sites

$rf = "repeater_field"; //repeater field name
$pt = "basic-page"; //page template name

foreach($p->$rf as $r){
    $np = new Page();
    $np->of(false);
    $np->template = $pt;
    $np->parent = $page;
    foreach($r->fields as $f){
        $np->$f = $r->$f;
    }
    $np->save();
}

Hi Adrian,

Do you just create a template file with this in it and run it?

Link to comment
Share on other sites

Hi Adrian,

Do you just create a template file with this in it and run it?

If you already have HannaCode installed, you can use that to run code in the backend easily using the Test option. If not, then you can place it into an existing template file. If it's a live site then maybe wrap it in:

if($user->isSuperuser()) {

so that it only happens once when you visit the page with the template.

I would also recommend testing that code of mine on a dev/testing site first. I adapted it quickly from https://processwire.com/talk/topic/6654-convert-repeater-to-pagetable/ - I think it should be fine - looks good, but always worth a double check before running something like this on a live site.

  • Like 1
Link to comment
Share on other sites

OK, so I've got the script to run but had 2 problems:

1) Dates - my dates are appearing as -0001-11-30

2) Titles - I probably need to concatenate 2 fields together for the titles - how can I do this?

Thanks Pete!

If you already have HannaCode installed, you can use that to run code in the backend easily using the Test option. If not, then you can place it into an existing template file. If it's a live site then maybe wrap it in:

if($user->isSuperuser()) {

so that it only happens once when you visit the page with the template.

I would also recommend testing that code of mine on a dev/testing site first. I adapted it quickly from https://processwire.com/talk/topic/6654-convert-repeater-to-pagetable/ - I think it should be fine - looks good, but always worth a double check before running something like this on a live site.

Link to comment
Share on other sites

Hi,
1) Dates might be broken due to output formatting

2) You can acces the fields of the repeater, like you would do with a page.

$p = $pages->get("/path/to/currentParent");
$rf = "repeater_field"; //repeater field name
$pt = "basic-page"; //page template name

foreach($p->$rf as $r){
    $r->of(false);    //Should make sure raw data are copied. (dates)
    $np = new Page();
    $np->of(false);
    $np->template = $pt;
    $np->parent = $p;
    foreach($r->fields as $f){
        $np->$f = $r->$f;
    }
    $np->title = $r->field1 + $r->field2 //that's how you should be able to access repeater fields and set the title
    $np->save();
}
  • Like 1
Link to comment
Share on other sites

I think just setting a new parent might be problematic, as repeaters don't really use a template (or a generic one?).

Repeaters are actually using real templates, they are just marked as system templates and therefore hidden by default like the user template. Only the new matrix fieldtype is using a somehow combined generic templates for each of the different data types in a single matrix field.

Link to comment
Share on other sites

  • 5 years later...

@adrian's script works like a charm when executed from a template page - I followed these steps:

Step 1 - Create new template with same fields set as on the repeater field.
* Field names must match (if they don't, you might map them manually).

Step 2 - Add the script to a Template.
* I set repeater field name and page template name.
* The script will attempt to copy all "repeater pages" found by '$page->$rf'. So, I used a template file that could access the repeater fields with '$page->$rf'.

Step 3 - Run the migration by visiting the page.
* At this point, I removed the script and inspected the results.

Results:

* All fields were migrated (saved me a ton of detailed copy-paste work!).
* Title field was set to a code ("1234-9234") because my Repeater didn't have a Title field - but it was easy to update manually.
* Url name field was set to a code ("1234-9234") - and updated after I manually pressed Save on each page.
* Even language fields were correctly migrated.

Nice!

<?php
$rf = "my_source_repeater_fieldname"; //repeater field name
$pt = "my-destination-page-template"; //page template name

foreach($page->$rf as $r){
    $np = new Page();
    $np->of(false);
    $np->template = $pt;
    $np->parent = $page;
    foreach($r->fields as $f){
        $np->$f = $r->$f;
    }
    $np->save();
}

 

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

×
×
  • Create New...