Jump to content

Errors triggered by correct hooks for Page Reference field


Recommended Posts

Hi everybody! And thanks for Processwire!

There's a kind of bug or incorrect behavior associated with hooks for Page Reference fields.

The Page Reference field stores references for PW page(s), sorry for being so banal. This kind of field needs some rules to fetch PW pages which will be available as content of inputfield. There are some possibilities to fetch that pages:

  1. by choosing their parent
  2. by choosing their template
  3. by custom find procedure having a nice user interface
  4. by PW selector string
  5. by custom PHP code

Let's now focus on the last 5th option. In this case we should add some PHP code to /site/ready.php . Just an example from PW docs:

$wire->addHookAfter('InputfieldPage::getSelectablePages', function($event) {
  if($event->object->hasField == 'cloth') {
    $event->return = $event->pages->find('your selector here');

Up to this point, the things are simple and easy. But what if we will try something more complex?

$event->return = $event->arguments('page')->parents('template=product')->first()->findOne('name=clothes,include=all')->children('template=cloth');

It's an excerpt from my real hook. I tested it both outside of the hook and inside it, it works (fetches the pages i need). But when i try so edit and/or to save my Page Reference field which uses that hook, i get an error of this kind:

Fatal Error: Uncaught Error: Call to a member function find() on bool

One more time: the above-cited piece of php code is correct. It populates my inputfield with the desired page references, the pages having that pagefield can be saved, fetched and so on. The only problem is that fatal error during save/edit of pagefield itself.

I can suppose that this error takes place because the above-cited custom php code deals with some parent page(s). It works when the inputfield is used by non-admin PW page having correct parent(s) expected by cutom php code, but fails when we're trying to work with the admin PW page representing this inputfield itself.

If so, we can consider a kind of "sandbox" for custom php code of this kind. Ideally, it shouldn't give us a fatal error even if it's incorrect. It should better throw an exception and output a warning, still giving us possibility to edit/save the inputfield admin page.

Any ideas are welcome. Thanks in advance!

Link to comment
Share on other sites

It appears i've found a quick workaround for the problem. It's not a patch for PW core, just a methodology of writing hooks.

Okay, here's a part of real hook which works perfectly on every site/admin pages having a certain inputfield except the admin page for that inputfield:

$event->return = $event->arguments('page')->parents('template=products')->first()->find('template=cloth');

This hook fails on the inputfield admin page because first() is applied to nothing in this case. It's rather logical: the inputfield admin page has no parent with template=product ! So we need to split the above cited code into parts and to add some logic which will prevent the hook error on our inputfield admin page:

//$realProductsPage works everywhere except the inputfield admin page, it fails there because admin page has no parent with template=products 
$realProductsPage = $event->arguments('page')->parents('template=products')->first();

//$stubProductsPage is used only on the inputfield admin page to prevent hook error
$stubProductsPage = $event->pages->findOne('template=products');

$productsPage = $realProductsPage? : $stubProductsPage;

$event->return = $productsPage->find('template=cloth');

It's far from being a perfect solution but it works.

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Similar Content

    • By waheed
      Custom PHP code is available for use in PageReference field type. Is it possible to use custom php to generate the values for a Select Options FieldType?? 
      What I'm to trying to achieve is to generate values for a options field from values for repeater in a grand parent's page (parent of parent page).
    • By Oliver
      It's great that you can choose the fields of template that will be displayed in the Page List in admin pannel. But I really miss the possibility to use a field of a page reference field of a template to be displayed, too.
      A current website I'm working on uses a «project» template with a page reference field «client». As the title of the projects are often very similar or even the same, it would be a big help for editors to have the referenced client's page title added to the project's title in Page List. Like:
      List of fields to display in the admin Page List
      title client->title
      But I guess I've to create a little module to get this working, hm?
  • Create New...