Jump to content

Move content of one field to another using API


a-ok
 Share

Recommended Posts

I have a standard ckeditor textarea 'article_text' on all pages that use the 'article-detail' template. I am now needing to create a RepeaterMatrix field with one of the types being a ckeditor textarea 'article_content_text'. I now need to find a way to batch move the content of 'article_text' to a RepeaterMatrix row 'article_content_text' on each page.

Any ideas with using the API if this is possible? I have over 200 articles so don't fancy manually doing it.

Link to comment
Share on other sites

I know, via the docs, that we can update a repeater field; but what about a RepeaterMatrix?

$building = $page->buildings->getNew(); // I guess here would be a good place to tell it what 'type' to use?
$building->title = 'One Atlantic Center';
$building->feet_high = 880;
$building->num_floors = 50;
$building->year_built = 1997;
$building->save();
$page->buildings->add($building);
$page->save();    

 

Link to comment
Share on other sites

9 hours ago, Robin S said:

The Repeater Matrix documentation seems to be lacking in this regard - I have requested some in the Repeater Matrix subforum.

Great, thanks Robin.

If anyone could shed any light on the matter (adding specific rows via the api for a RepeaterMatrix) that would be appreciated.

Link to comment
Share on other sites

The bit that I can't work out at the moment is how to set the matrix item type using a friendly name. I'm sure @ryan will have built in a way to do this, but until he responds you can set the matrix type as an integer using "repeater_matrix_type". First you need to find out the integer for the type of row you want to add. You can do this by dumping a repeater item of the right type and checking the repeater_matrix_type value in Tracy. Or you can open the field settings and inspect the source for the type you want to add. For example, in the screenshot below you can see the id of the inputfield for the "body" type is "matrix3_item", therefore the repeater_matrix_type integer is 3.

matrix1.png

So find out the repeater_matrix_type value (integer) for "article_content_text" and then you would run some code like this...

$articles = $pages->find("template=article");
foreach($articles as $article) {
    $text = $article->article_text;
    $article->of(false);
    $new_item = $article->my_matrix_field->getNew(); // replace with the name of your matrix field
    $new_item->repeater_matrix_type = 3; // replace with the integer of your matrix type
    $new_item->article_text = $text; // assuming you added the article_text field to this matrix type
    $new_item->save();
    $article->my_matrix_field->add($new_item);
    $article->save();
}

But first test this works for a single article page before you process all the articles. :)

 

  • Like 3
Link to comment
Share on other sites

This totally works, Robin. This is great. It looks like the type is just ordered 1, 2, 3 etc based on the position (much like the Options field) so I guess that kind of makes sense but would be good to be able to call the name ('text', for example) rather than the integer.

Thanks for the help here. Really appreciated as always.

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...