Jump to content

Copy Page references from one field to another


nikola
 Share

Recommended Posts

I currently have a page reference field (multiple pages select through AsmSelect) for selecting categories that are created under page "Settings".

I want to transfer these references (selected categories) to another page reference field that will have exact page reference names.

For example:

Article has selected categories: Category1, Category5, Category7; these are stored as children under page "Settings"

New reference field would have exact categories but they are stored under main categories as children. I'm using custom selector to find these categories (template=subcategory|advices, sort=name).

Is there a way to transfer these references through the API? All have the same name.

Link to comment
Share on other sites

I'm not sure that I totally understand the entirety of what you are doing, but it is relatively simple to copy page references from one page to another:

$page1 = $pages->get('/some/page/'); // page we want to copy from
$page2 = $pages->get('/some/other/page/');  // page we want to copy to

// optional: remove any existing categories if there are any
$page2->categories->removeAll();

foreach($page1->categories as $category) {
  $page2->add($category);
}

$page2->save();
Link to comment
Share on other sites

Thanks Ryan for your answer. I'll try to clarify what I would like to do:

I have around 400 articles located under Articles page (articles holder page).

All these articles have multiple page reference field called "categories" that pull references (pages) from structure below:

Tools

- Category1

- Category 2

- Category 3

- and so on

I've made another multiple page reference field called "subcategory" and added it to same "article" template so now I have two page reference fields in this template.

Simply, I want to transfer all page references from "categories" to "subcategory".

The catch is:

Although they all have same names (page references), "subcategory" page reference field has different tree structure from where I'm pulling them:

They are located like this:

Category 1

- Subcategory 1

- Subcategory 2

- Subcategory 3

- and so on

Category 2

- Subcategory 1

- Subcategory 2

- Subcategory 3

- Advices

- and so on

Category 3

- Subcategory 1

- Subcategory 2

- Subcategory 3

- Advices

- and so on

I'm using custom selector for "subcategory" page reference field to find those subcategories (they can have 2 different templates):

template=subcategory|advices, sort=name

After that I can simply discard "categories" page reference field because of no need anymore...

I think it's a bit tricky because of different tree setups for these two reference fields.

Link to comment
Share on other sites

Hmm.

$arts = $pages->find("template=article");
$arts->setOutputFormatting(false);

foreach($arts as $art){
    foreach($art->categories as $cat){ // loop current categories
        // find subcategory with same name
        $subcat = $pages->find("has_parent=/path/to/subcategories/, template=subcategory|advices, name=$cat->name")->first();
        if(!$subcat->id) {
           echo "<p>subcategory not found $cat->name</p>";
        } else {
           // add it to subcategories page field
           $art->subcategories->add($subcat);
           // save field
           $art->save("subcategories");
        }
    }
}

Something like this would do.

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