Jump to content
Schwab

API manipulation of Reference Page Field

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

 

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Found out the solution myself, thank you!

Edited by VeiJari
Nevermind I was thinking this problem from a wrong standpoint.

Share this post


Link to post
Share on other sites

@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!

Share this post


Link to post
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?

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Flashmaster82
      Is it possible to just show a list (page reference) but without any radio buttons? Or a drop down but the references is not selectable? The items needs to be references but the main thing is that its just a list. Please help!

    • By sambadave
      Hi everyone, here's the problem I'm trying to solve.
      I have a config area in my PW admin that is locked down for admin use. I use pages to store a bunch of settings that I'll use for my clients website. It's mostly used for visual things like colours and theming. This list could be tiny or large, depending on the sites requirements, but its great because I can store any information I want to. So the page tree could look something like this:
      ADMIN SETUP
      Home Config Aesthetics Colours Red Field: Custom Label - "Red" Field: Class name - "theme--red' Green Field: Custom Label - "Green" Field: Class name - "theme--green' Blue Field: Custom Label - "Blue" Field: Class name - "theme--blue' etc... Sizes Small Field: Custom Label - "Small" Field: Class name - "sm' Medium Field: Custom Label - "Medium" Field: Class name - "md Large Field: Custom Label - "Medium" Field: Class name - "lg" etc... Icons Target Field: Custom Label - "Hands shaking Icon" Field: Icon SVG - "[svg code]' Target Field: Custom Label - "Target icon" Field: Icon SVG - "[svg code]' Success Field: Custom Label - "Happy face icon" Field: Icon SVG - "[svg code]' etc... HOW I USE THIS
      I'm then able to set up page reference fields for colour, size and icons. I'll use these fields on particular pages so that my clients can select a particular colour, size or icon, or anything really. Currently, with the page reference field I can create a custom label for the options. So for something like colour I can label the field "Theme" and present a list of colours like "Red, "Green" and "Blue" using the custom page label label format of the colours page.
      This of course means that I can use this to do some lovely presentation on the front end of the site. With a colour selected I'll then be able to use the page reference to get the class name for that colour theme so that I can update the page's look and feel. So in my markup I'd end up with "theme--red" or "theme--green" etc.
      THE PROBLEM
      I have this all working which is great and it's really flexible for the client which they love. However, I'd love to be able to make things more visual for the client.
      Is there a way to be able to output more than just text in the page reference field? I might have a bunch of different blue colours, so instead of a list like so:
      Navy Blue Deep Blue Bright Blue Sea Blue ... it would be great to be able to output actual colour swatches, which is a lot mor visual for the client.
      Taking the icon selector, I would ideally like to show the actual svg that I've stored against that icon as a selectable image instead of seeing text options like:
      Hands shaking icon Target icon Happy face icon Just a couple of scenarios here, but as you can see there could be any number of reasons to display a more visual method of selection.
      ANY SOLUTIONS?
      I've looked at modules like FieldtypeColorPicker which could help in solving the colour issue, but it doesn't allow me to select a colour and then use a particular class name assigned to it the way I describe above.
      Considering the other use cases I mention above, does anyone know if anything exists already that would help me to create custom presentaion for page reference lists, or if there's anything planned?
      Thank you in advance for anyone who's read this far and has any words of wisdom!
      Dave
       
    • By theoretic
      Hi there! And thanks for Processwire!
      Another bug found for fields of type FieldtypePage. There's a "custom find" section in the "input" tab allowing to select several conditions for finding pages which will be included into thepages listing. This feature doesn't find any page. Tried to use "template" and "id" conditions, always no result. Fortunately, the "template + additional templates" section works fine.
      My PW is 3.0.77. Hope that helps, and thanks in advance!
       
    • By cosmicsafari
      Hi all,
      I am creating a page field (field of type FieldtypePage) via the API, however im still trying to find some documentation as to how I would go about setting the Selectable Pages for said field using the API.
      From what I have found it looks like it involves the use of, albeit this looks like a getter rather than a setter:
      $field->getInputfield($page) Which looks like it would make sense if I wanted to specify the selectable pages by a parent page, but what if I wanted to specify it by say a template?
    • By lenno
      Dear all,
      I have a presumably simple question, which I simply can't find the answer for.
      I need to programatically add a page to a FieldtypePage field. I tried the following:
      $page->a_field_of_fieldtype_page->add($anotherPage); but without success.
      Can anybody help?
×
×
  • Create New...