Jump to content

Adding pages reference entry on the fly


Spiria
 Share

Recommended Posts

Hi, 

I must create a synchronisation process to import data from a CRM. I have a page reference field where I don't know in advance all the available options. It has to be added in the reference tree. 

In my import script, based on Ryan's Import Page by CSV module, i coded this:

$p->crm_member_type = $data['TypeDeMembre'];

where $p is a valid page to be saved.  $data['TypeDeMembre'] is a string.

Actually, if the entry is already present in the page reference tree, the data is saved correctly. But if the entry does not exist, nothing is saved.

This page reference field ("crm_member_type") is set to accept new entry on the UI, but I think this feature is not available from API.

Do I have to check first if the entry exists then create a the right page to be referenced afterwards?

Link to comment
Share on other sites

40 minutes ago, Spiria said:

Do I have to check first if the entry exists then create a the right page to be referenced afterwards?

As far as I know you have to go this way.

You could take just that $data ['TypeDeMembre '] column on first run and create all non-existing pages and afterwards you import as usual.

 

Link to comment
Share on other sites

That's why I asked... We did just a great project (https://centura.ca) and we have a lot of page references (it was not coded by me, and the guy is absent for the moment, and his code is not different from mine...). There is indeed such a field that is supposed to be automatically completed. Maybe I am just too tired of trying.

Link to comment
Share on other sites

4 hours ago, Spiria said:

Do I have to check first if the entry exists then create a the right page to be referenced afterwards?

Yes, you have to check if the page you are trying to reference exists, if not, create it through the API.

For educational purposes, you can check how InputfieldPage does it. See how it iterates through the titles it receives as input, and makes decisions based on the page already existing or has to make a new one. 

https://github.com/processwire/processwire/blob/master/wire/modules/Inputfield/InputfieldPage/InputfieldPage.module#L840

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