Jump to content

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






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


Edited by kongondo
  • Like 1

Share this post

Link to post
Share on other sites
23 minutes ago, kongondo said:


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


Thanks kongondo

It is indeed as simple. Both version work actually.

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


Share this post

Link to post
Share on other sites
Posted (edited)

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • 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:
      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.
      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.
      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!
    • 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?
    • By sjohnson
      [Posted this in another post, but realized it's doesn't really pertain to it, so I've moved it to this new topic -- again sorry]
      Hi Ryan (and everyone else!),
      Just starting using PW and I'm loving it... I've got a site build that requires a bunch of CSV loading and would like some help with API Importing, as I am running into some glitches with page fields.
      So far I have a bunch of CSVs that work fine with my php code, but I have one that crashes, unless I put in the pageID.
      I think it may be something larger as I have tried using both BatchChildEditor & ImportPagesCSV modules with a modified CSV, and both get the same error as my code.
      So, here's my code, it does the following:
      Reads a page field to get CSV filename Reads 1st row of file to figure out what template and parent to use Reads 2nd row for field titles Processes the remaining rows as data, it also echoes out what it is doing -- not elegant, but it's only for my use //    If no filename exit     if ($wire->page->getUnformatted('section_slogan') == '') throw new Wire404Exception(); //    File Pointer         $myfile = "http://" . $config->httpHost . $config->urls->templates . "loaders/" . $wire->page->getUnformatted('csvfilename');     $content = "<p>Using Loader File '{$myfile}'</p><hr/>"; //    Read The Data File     if (($handle = fopen("$myfile", "r")) !== FALSE) {     //    Get the Setup Info         if (($data = fgetcsv($handle, 0, ",")) !== FALSE) {             $myTemplate = $templates->get("$data[0]");             $myParent = $wire->pages->get("path='$data[1]'");             if (($myTemplate->id == 'NullPage') || ($myParent->id == 'NullPage')){                 throw new Wire404Exception();                             }         } else {             throw new Wire404Exception();         }     //    Setup the parent         $newItem = new Page();         $newItem->template = $myTemplate;     //    Tell User what's going on:             $content .= "<p>Adding New Pages based on the '{$newItem->template->name}' template to parent '{$myParent->title}' path: '{$myParent->path}'</p><hr/>";     //    List out the fields in the template         $num = count($newItem->fields);         $content .= "<p> $num fields in template</p><ol>";         foreach($newItem->fields as $itemfd) {             $content .= "<li>" . $itemfd->name . "</li>";         }         $content .= "</ol><hr/>";     //    Read header record and match-up fields to template         $fieldmatchup = '';         if (($data = fgetcsv($handle, 0, ",")) !== FALSE) {             $num = count($data);             for ($c=0; $c < $num; $c++) {                 foreach($newItem->fields as $itemfd) {                     if($data[$c] == $itemfd->name) {                         $fieldmatchup[$itemfd->name] = $c;                     }                 }             }                     }     //    Show the File's Field Array         $content .= "<p>Display the Field Array</p><ul>";         foreach($fieldmatchup as $item_key => $item_value) {             $content .= "<li>['" . $item_key . "'] = '" . $item_value . "'</li>";         }         $content .= "</ul><hr/><h4>Reading File</h4><hr/>";     //    Now add the data         $row = 0;         $newItem = '';         while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {             $num = count($data)-1;             $row++;             $mySel = "parent=" . $myParent->id . ", title='" . $data[$fieldmatchup['title']] . "'";             $dupPage = $wire->pages->get($mySel);             if($dupPage->id == 'NullPage') {                 $content .= "<p>newItem (" . $mySel . ")<br/><b>-- Creating from File</b>.</p>\n";             //    Setup blank WireArray based on a template                 $newItem = new Page();                 $newItem->template = $myTemplate;                 $newItem->title = $data[$fieldmatchup['title']];                 $newItem->parent = $myParent;                 $newItem->save();             }             else {                 $content .= "<p>Duplicate Found (" . $mySel . ")<br/><b>-- Updating from File</b>.</p>\n";                 $newItem = $dupPage;             }         $content .= "<p> {$num} fields in record {$row}: <br /></p>\n";             $newItem->of(false);             foreach($newItem->fields as $itemfd) {                 $key = $itemfd->name;                 if ($key != 'title') {                     $value = $data[$fieldmatchup[$key]];                     $content .= "[{$key}] = [{$value}]<br />\n";                     $newItem->set($key, $value);                 }             }             $newItem->save();             $content .= "<b>-- Saved: </b> <a target='_blank' href='" . $newItem->editUrl . "'>" . $newItem->title . " [" . $newItem->id . "]</a><hr/>";         }         fclose($handle);     }     $content .= "<b>{$row} records added/updated</b>";     And here's a sample of data that works: (associated_stat & special_category are page fields)
      skill,"/systems/sol/" title,associated_stat,multiplier,special,special_category,body Alien Archeology,Int,4,,,"Skill description" Alien Tech,Tech,4,,,"Skill description" Alien Weapons,Ref,4,1,Weapons,"Skill description" And the file that's giving me issues:(pc_role_category, associated_book, sa_skill are page fields, career_skills is a multiple page field)
      cp-role,"/systems/sol/" title,pc_role_category,associated_book,page_no,verified_via,sa_skill,career_skills,body "Merc","Combat Related","Book Name","8","Book Review","Combat Zen","Athletics|Alien Tech|Drive|Shoot","Role Description" It's the associated_book field that's giving me the issue, if I blank it or put in the PageID it works, but with anything else it errors out with this:
      Fatal Error Call to a member function __unset() on boolean search Source File: ...\core\wire\modules\Fieldtype\FieldtypePage.module:439 431: if($value instanceof Page) { 432: // ok 433: } else if($value instanceof PageArray) { 434: $value = $value->first(); 435: } else if(is_string($value) || is_int($value)) { 436: $value = $this->sanitizeValueString($page, $field, $value); 437: if($value instanceof PageArray) $value = $value->first(); 438: if($value->_FieldtypePage_remove === $value->id) { 439: $value->__unset('_FieldtypePage_remove'); 440: $value = null; // remove item 441: } 442: } So, looking at this field, and the associated page template, everything is setup the same as all the other page fields, except that the page template in question (books) has a field that references a page field (associated_system) that I'm not even referencing, so I'm not sure if that's the culprit or not, but that is the only thing that separated this template from the others is this custom label code and that the pages are outside of the parent:

      And yes, I've removed the custom label and it still has the same error.
      With my luck it's something simple, but I can't see it... Any help would be appreciated
  • Create New...