Jump to content

Link page with multiple pages by field value in backend


Recommended Posts

I have quite specific question. I am working on app in processwire which Is for contract and product management. I have already quite lots of pages with imported data. I need to establish link for page(contract) which should be connected with multiple pages (products). Imported data have same numeric field on contract template and also on product template. This field is not a name or title of page. Now is all achievable on fronted where is possible to prepare markup with selected data. But I need to have this connected in backend(admin) - is possible to connect fields between pages 1:n from already created data?

Reason for this is to have option to edit connected data in backend ...

I would appreciate any advice.

Anybody know how to do this?

Link to comment
Share on other sites

If I understand correctly, you want to make the connection between contract and product pages after data import. In the imported data you have a numeric ID that connects contracts to products.

Can you please clarify if that numeric ID has been saved to a field on both contract (multiple IDs) and product page (single ID)?

Once we know that, it would be quite easy using a small script to make the connection through a page reference field on the contract page that references one or more products.

  • Like 2
Link to comment
Share on other sites

Hi.
Thanks for help and you understand well. I can clarify that numeric field (ID) is saved on both contract and product page.
I would appreciate to have some script to handle links.
 

Link to comment
Share on other sites

First you need to add a page reference field to the contract template. I assume you name it 'products'

Set it to accept multiple pages

pagefield-details.png.c0fffc00b05b1f80f6f3a45985e4e9f5.png

Choose type ASM select and product template for allowed templates

pagefield-input.thumb.png.1518f076857ee41ea6f752e1a28d38e0.png

Now that you have your products field in place, you can write a script to assign the product pages to the contract pages.

foreach($pages->find("template=contract") as $cp) {
    // assuming you have a field $cp->uid with the ids of the products
    // if you have a comma separated list of ids or the like, convert it to array
    $uids = explode(',', $cp->uid); 
    // add products for each $id to products field
    foreach($uids as $id) {
        $cp->products->add($pages->find("template=product, uid=$id"));
    }
    // save the changes
    $cp->setAndSave('products');
}

I would recommend using TracyDebugger Console to run the script. Or you can run it from site/templates/home.php

This should assign the correct products to the contract pages and make them editable from the contract pages edit screen.

  • Like 2
Link to comment
Share on other sites

I have more explored your script. I do not have array of products IDs. I have products as pages already in PW and each of this product has numeric field with its ID.
Is it possible to link all those pages(they have main parent page("products") by this script with corresponding pages by same contract ID ?
 

Link to comment
Share on other sites

6 hours ago, PavelRadvan said:

Thanks for help and you understand well. I can clarify that numeric field (ID) is saved on both contract and product page.

Did you save the product IDs for the linked products to your contract pages when importing the data?

If you don't have the product IDs for each contract page, there is no way of linking products to contracts by a script.

Link to comment
Share on other sites

Yes. There is imported lots of pages of product items (products) with "contract head ID". Same "contract head ID" is also on pages which are contracts.
I am using pages->find and pages->get and other function on frontend to show them by this ID - so it now all works on frontend. But when I click to edit product in backend it shows only current product without link to contract there. So reason for this is to have editable contracts in backend and with already selected products(multiple) in page reference field of specific contract.
When I create now in PW new contract I could select by page reference field multiple products - but I do not want to do it manually for next several thousands of contracts...
I hope this is understandable for you...

 

Link to comment
Share on other sites

Thank you for the explanation. I still do not fully understand how you contract head ID is saved to the product and contract pages.

9 hours ago, PavelRadvan said:

But when I click to edit product in backend it shows only current product without link to contract there.

I assume that on the product template you do not have page reference field which links to contract(s).

9 hours ago, PavelRadvan said:

So reason for this is to have editable contracts in backend and with already selected products(multiple) in page reference field of specific contract.

But on the contract template you already have a page reference field for products which contains products for that contract. Please specify the field name

Now you want to "link back" the contracts to a product using the products available in the contracts' page reference field for products.

Is this correct?

Could you please share screenshots of the templates for contract and product? And also screenshot of the current edit page for contract and product with the relevent fields? That would help to clarify.

9 hours ago, PavelRadvan said:

When I create now in PW new contract I could select by page reference field multiple products - but I do not want to do it manually for next several thousands of contracts...

Sorry, but you lost me there ? When you create a new contract, how does the contract know, which products are connected to it? When creating a new contract, do you assign an existing contract head ID that already is linked to products?

Link to comment
Share on other sites

Thanks very much for reply and I have to explain current situation more.
I am sorry but mys system is in my language (Czech), but I think it is clearly visible in pictures how are fields and IDs.
I have imported data from Access in form of CSV trable files. This import was done always from one table to one page to create lots of children pages. I could as ne example of data take table with one type contracts named "ks-hlavicka" which is table of contracts (chidren pages)  and details (head of contract) and products table is named "ks-old-pol" (also children pages).
Each of those tables have numeric field "id_hlavicka_ks" with label ID hlavičky KS or ID hl KS.
Here are screenshots from admin (backend):
Contract:
image.thumb.png.4e8e4058a7ca150fe71eee39c3640d84.png

Product item:

image.thumb.png.57c87e168657e6dc8b34bb0c7226d578.png

As you could see there is same ID in both pages.
This is actual current situation. There is not page reference field yet in those pages - I need to know how to prepare it to template for scripts or guides or by your advice...
On Frontend I could use API of PW to show content by ID but I would like to have interconnected pages also visible in admin (backend).
What I want is to achieve this - it is example of created contract on another page with products items linked in page reference field:
image.thumb.png.97977b09cfa3c73ba00ca48a55ecea7b.png

Same functionality I would like to have same for product also with connected customers(also they have IDs) which are on contracts.
But after I will have some script or guide to link products I think other connected data could be handled by same way.
Here are screenshots of templates for contract (ks_hlavicka) and product item (ks_polozka_old):
image.thumb.png.eb6bf38b98ec0518743164f7b21e6e93.png

image.thumb.png.ad4ddf0eb0902c0eddff0da8ff5c3cc9.png
Does this helped to you to understand ?
Let me know how you think is possible to make that interlinked connections.
Thanks
Pavel

  • Like 1
Link to comment
Share on other sites

Thank you for providing the screens. That helps.

If you want to have linked products on the contract pages, you need to add a page reference field for products, like I described above.

After you have added that field, you can use this script to loop through all contract pages and assign linked products by id:

foreach($pages->find("template=contract") as $cp) {
    // assuming you have a field $cp->uid with the id
    // get products with same id
    $products = $pages->find("template=product, uid={$cp->uid}, status<" . Page::statusTrash);
    // add products with same id to products field
    $cp->products->add($products); // changed this line
    // save the changes
    $cp->setAndSave('products');
}

EDIT: changed a line in the code

You need to change the template and field names to fit your installation.

After running the script, every contract page should have linked products assigned.

The process for linking customers to products should be similar.

Let me know how it goes.

Link to comment
Share on other sites

8 hours ago, PavelRadvan said:

Hi.
Thanks very much. I will test it and let you know

Before you try, I changed a line in the code above. Use the new version.

Link to comment
Share on other sites

  • 1 month later...
On 5/5/2024 at 2:12 PM, gebeer said:

Thank you for providing the screens. That helps.

If you want to have linked products on the contract pages, you need to add a page reference field for products, like I described above.

After you have added that field, you can use this script to loop through all contract pages and assign linked products by id:

foreach($pages->find("template=contract") as $cp) {
    // assuming you have a field $cp->uid with the id
    // get products with same id
    $products = $pages->find("template=product, uid={$cp->uid}, status<" . Page::statusTrash);
    // add products with same id to products field
    $cp->products->add($products); // changed this line
    // save the changes
    $cp->setAndSave('products');
}

EDIT: changed a line in the code

You need to change the template and field names to fit your installation.

After running the script, every contract page should have linked products assigned.

The process for linking customers to products should be similar.

Let me know how it goes.

Hi.
I did not have time to test it sooner so I did it today.
I used your script which was run from tracy console and there is final variant of it :

foreach($pages->find("template=ks-import-hlavicka") as $cp) {
    $polozky = $pages->find("template=polozka_ks, id_hlavicka_ks={$cp->id_hlavicka_ks}, status<" . Page::statusTrash);
    $cp->id_polozka_ks->add($polozky);
    $cp->setAndSave('polozky');
}

where id_polozka_ks is that integer id field which is on both pages of contracts and products.

As you could see in one of contract pages, there is no linked data:

image.png.c79b52a4a3f45d7b034d14ee65e6f53c.png

But system is linked to see items from products template:

image.png.494c6ee861ef8d8b20747c7d661e13f0.png

 

Script just reported that executed with 4487.67 ms, 15.1 MB
What do you think of it - is there any problematic part or I need some permissions to interconnect data to pages ?
Thanks for reply

Pavel

 

Link to comment
Share on other sites

It seems I find myself where is problem with script from you.
This version is working for me:
 

foreach($pages->find("template=ks-import-hlavicka") as $cp) {
    $polozky = $pages->find("template=polozka_ks, id_hlavicka_ks={$cp->id_hlavicka_ks}, status<" . Page::statusTrash);
    $cp->Polozky_ks->add($polozky);
    $cp->setAndSave('Polozky_ks',$polozky);
}

Most important is that in setAndSave is needed to specify that page field to which I need to save changes and update data...
Without it there was no change in data...and no linked other pages...

Regarding page reference field with single-page input, there is not needed to use "add" method. It throws error if I used it for adding data, because I had same data already on same page...
I tried to add linked page with customer data to contract page and it is always only one customer for contract...

So thanks for your initial hint with script and now it is working...hope this could help to others also...

  • Like 1
Link to comment
Share on other sites

Thanks for reply. I am just figuring another think and it is validating data before creation of new page - it is for preparing right contract number so customer just click on new contract button on frontend and PW should prepare new page with constructed name from several parts and fields. But I need to validate if there is not already another contract number already, but before it is created.

I already discussed this with @ryan, but it seems that he is quite busy.
I will invite you to this and just need some small hint to figure it...

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