Jump to content

Probleming saving page reference field via API


Beetrootman
 Share

Recommended Posts

Hello,

I have a page reference field, with the value type single page or boolean. On Page 1, when the user selects a page (Page 2) in the field, and then saves Page 1, I want to check which page has been selected, and then save Page 1 into the corresponding page reference field on Page 2:


Page 1 -> Selected Page 2 in page reference field
Page 1 -> Save page, hook into saveready, check which page was selected
Page 2 -> Save Page 1 to field via API

Now, I can get this working in practice. The field is being correctly saved on the referenced page, however, the main page being saved (Page 1) does not reload after the save process, it just stays blank. I get no errors or warnings being logged either. Here is my code:

$config->addHookAfter("Pages::saveReady", function($event) {
  $page = $event->arguments('page');
  if ($page->template->name === 'single-spectacle') {
    if ($page->soiree_double) {
      $double = wire('pages')->get($page->soiree_double->id);
      $double->of(false);
      $double->soiree_double = $page->id;
      $double->save('soiree_double');
    }
  }
});

Has anybody had similar problems saving a page reference field? If I change the field being saved to something else then the page reloads like usual, so it seems the problem is related specifically to the page reference field.

Thanks

Link to comment
Share on other sites

$config->addHookAfter("Pages::saveReady(template=single-spectacle)", function($event) {
	$pages = $event->object;
    $page = $event->arguments[0];

    if ($page->soiree_double) {
      $double = $pages->get($page->soiree_double->id);
      $double->of(false);
      $double->soiree_double = $page->id;
      $double->save('soiree_double');
    }
});

Does this help?

Link to comment
Share on other sites

Did you have a look at $page->references() method? This gives you all pages that reference this page. In the page edit screen you can see those in the "Settings" tab under "What pages link to this page". With this method available in your template php maybe you don't even need your hook.

And there is @Robin S's module ConnectPageFields that handles those cases.

You might also want to take a look at @Martijn Geerts' FieldPageSync Module that also does something similar.

  • Like 1
Link to comment
Share on other sites

Hi, thanks for the replies everyone.

So I've installed Tracey, and the error I am getting is:
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 262144 bytes) in /htdocs/cms/wire/core/Wire.php on line 1144.

This error occurs when using my hook, and also when attempting to do the same thing with ConnectPageFields.
The field is being updated correctly, very strange...

Note, if I remove the line that updates the field, but still save the field, I also get the same error:

$config->addHookAfter("Pages::saveReady", function($event) {
  $page = $event->arguments('page');
  if ($page->template->name === 'single-spectacle') {
    if ($page->soiree_double) {
      $double = wire('pages')->get($page->soiree_double->id);
      $double->of(false);
      $double->save('soiree_double');
    }
  }
});

Update:
I have tried the same thing on another processwire installation and I get the same error. So it doesn't seem to be related to the particular processwire configuration of the site I had the problem with.

Link to comment
Share on other sites

You cannot have two pages that reference each other via "single" Page Reference fields: https://github.com/processwire/processwire-issues/issues/572

Also watch out for: https://github.com/processwire/processwire-issues/issues/1092

Workarounds include the Page IDs module or use "multiple" Page Reference fields instead of "single".

I feel that the convenience of getting a Page object back as the value of a single Page Reference field is sadly outweighed by the frustration of these issues. I'd rather just get an ID that a Page object can then be loaded from if/when needed.

  • Like 2
Link to comment
Share on other sites

Hi, thanks for the clarification Robin S. I had come to the same solution that it was something to do with have two page that reference each other. The reason I wanted to do this was from and end-user point of view. If the admin makes a connection from one event to another, then the second event should also visually reflect this. If there are multiple editors working on a site it's important they can quickly know which two pages are linked.

In the end I created two more fields. A hidden text field that stores the name of the page with the reference (on the referenced page) in the page saveready hook, and a RuntimeMarkup field that outputs the page title. The page reference field is set to be visible if the text field is empty and vice versa. The RuntimeMarkup mark field is there only to output the title and be non-editable:

$config->addHookAfter("Pages::saveReady", function($event) {
  $page = $event->arguments('page');
  if ($page->soireedouble) {
    $double = wire('pages')->get($page->soireedouble->id);
    $double->of(false);
    $double->soireedouble_ref2 = $page->name;
    $double->save('soireedouble_ref2');
  }
});

P.s, it would be useful if this limitation of the page reference field would be in the documentation somewhere ?
Thanks for all the help!

Screenshot 2020-03-01 15.55.45.png

Screenshot 2020-03-01 15.55.50.png

Link to comment
Share on other sites

Instead of having extra fields and saving extra data, you could make use of the $page->references() method, I mentioned above. You could utilize this method inside a ProcessPageEdit::buildFormContent hook and add a Markup field that holds the titles (and even links) of the referenced pages. Just an idea.

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