Jump to content

API manipulation of Reference Page Field


Schwab
 Share

Recommended Posts

Is there a way to manipulate Reference Page Fields by the API?

I want users to add page reference items to the field, but found no information about it.

The other option I see is using Repeaters instead and save the page id of the referenced page in the repeater field. But creating or deleting the Reference Page field would be nicer. There is no automatic update of the id in the repeater, in case of a change to the referenced page.

Any solutions?

Cheers Simon

 

Link to comment
Share on other sites

Welcome to the forums @Schwab

12 minutes ago, Schwab said:

Is there a way to manipulate Reference Page Fields by the API?

I want users to add page reference items to the field, but found no information about it.

Have a read here about WireArrays. A multi page field returns a PageArray (which is derived from a WireArray).

http://processwire.com/api/ref/page-array/

http://processwire.com/api/ref/page-array/add/

http://processwire.com/api/ref/wire-array/

http://processwire.com/api/ref/wire-array/add/

Examples

// examples
$someOtherPage = $pages->get(1234);
$page->your_page_field = $someOtherPage;// @note: not sure it works in all contexts
$page->your_page_field->add($someOtherPage);
$page->your_page_field->add(1234);
// Add multiple pages
$page->your_page_field->add($pages->find("template=basic-page"));

 

Edited by kongondo
  • Like 1
Link to comment
Share on other sites

23 minutes ago, kongondo said:

Examples


// examples
$someOtherPage = $pages->get(1234);
$page->your_page_field = $someOtherPage;// @note: not sure it works in all contexts
$page->your_page_field->add($someOtherPage);
$page->your_page_field->add(1234);

 

Thanks kongondo

It is indeed as simple. Both version work actually.

$p = $pages->get(1234);
$p->of(false);
$p->page_reference_field->add(1111); // add another page by id
$p->save();

 

  • Like 1
Link to comment
Share on other sites

  • 1 year later...
  • 1 year later...

@kongondo

Hey I have a pagereference array added to user template.

I'm trying to add multiple pages to it via api. I have all the page ids to add as an array.

I can't add multiple pages to the page reference array. I've tried with: 

$userToCreate->projectReferences->add(wire()->pages->find('id=' . implode ('|', $data->projects), ['findAll' => true]));
$userToCreate->projectReferences = wire()->pages->find('id=' . implode ('|', $data->projects), ['findAll' => true]));
//however this works
$userToCreate->organisationReference = wire()->pages->get('id=' . $data->organisationId, ['findOne' => true]);

I'm adding 'findAll' because of my custom authentication for templates. And using wire() because this doesn't happen in a template file

How I can add multiple pages to this pagereference array?

Thanks for your help!

Link to comment
Share on other sites

On 10/28/2020 at 1:05 PM, VeiJari said:

I can't add multiple pages to the page reference array.

@VeiJari,

Works fine for me here with the code I posted above. 

<?php namespace ProcessWire;

// get current user
$user->of(false);
// $user->project_references = $someOtherPage;// @note: not sure it works in all contexts
// $user->project_references->add(1654);
$user->project_references->add($pages->find("template=basic-page,parent=1651"));
// $user->save();
$user->save('project_references');// saving just the multipage reference field

What is $userToCreate?

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