Jump to content

Using methods/props on non existent fields in a template


owzim
 Share

Recommended Posts

The title might sound weird, but here's what I am talking about:
 
Let's say I have a field of the Page FieldType to be used as a checkbox selector with the name of "categories".
 
In the template file I want to do something depending on the categories the page is assigned to:
if($page->categories->has('name=catname1, name!=catname2')) {
    // do something
} 
 
I actually want to check for that in the header, so this code is executed on templates that might not have the filed "categories", so in that case it would result in (obviously):
Fatal error: Call to a member function has() on a non-object 
Of course, I can check that before, like that:
if($page->categories && $page->categories->has('name=catname1, name!=catname2')) {
    // do something
} 
But then there might be something I would like to just echo out, for example:
echo $page->some_radio_button_group->value; 
I would then have to cumbersomely turn the likeylikey one liner into this:
if($page->some_radio_button_group) {
    echo $page->some_radio_button_group->value;
} 

or, one line but still non-fancy

echo $page->some_radio_button_group ? $page->some_radio_button_group->value : ''; 
 
Perhaps there is a more convenient way of getting the values without checking beforehand?
Link to comment
Share on other sites

Hard to give an answer before knowing the question.

So the last line is not a question? =)

Again:

Is there a way to use the short and sexy api syntax

$page->some_radio_button_group->value; 

without having to check if the field actually exists on the template?

Link to comment
Share on other sites

Sorry, was a trick answer... No there's no way.

What do you expect to happen? This is possible if you can make sure the template code is only for pages that have the field.

The important thing here is you have code in a place that you don't know what page you're on so you have to ask.

Link to comment
Share on other sites

Don't think there's a way around checking if field exists, not at the moment at least. In your echo case you could always do this:

if($page->some_radio_button_group) echo $page->some_radio_button_group->value;

This doesn't look "cumbersome" or inconvenient to me (none of your examples do, really), but hey -- what do I know.. :)

Link to comment
Share on other sites

<just-kidding>You can create a module, the SexyApiCode.module. That has to hook somehow into ErrorHandler and if a call to a nonexistent PageProperty (optionally: that matches one in PropertyList from ModuleConfig) has raised the Error, don't throw exception but continue with next codeline.</just-kidding>

:P

Link to comment
Share on other sites

Sorry, was a trick answer... No there's no way.

What do you expect to happen? This is possible if you can make sure the template code is only for pages that have the field.

The important thing here is you have code in a place that you don't know what page you're on so you have to ask.

I should appreciate your subtle rhetoric more =)

Checking for the template might be the most convenient, because then I do not have to check for each field separately.

That the fields have to be checked before I can access their children lies of course in the nature of PHP or most other languages as well. What I thought was that there might be a PW way around this problem. But there isn't, and that's OK =)

teppo: still ... =)

horst: ... uhm ;D

Link to comment
Share on other sites

echo $page->some_radio_button_group->value; 

The some_radio_button_group field is a field of type Page I'm guessing. Since fields are bound to templates, and templates are bound to template files, you shouldn't have to check that the page has a some_radio_button_group field. That is, unless the code is independent of the template (like for shared functions across different templates). But if we can assume your template file knows what template it's dealing with, then you would want to set your Page field settings "details" tab to be dereferenced in the API as "Single page (Page) or empty page (NullPage) when none selected". That way your $page->some_radio_button_group->value; would result in a null (blank) even when the field has no value. 

Link to comment
Share on other sites

Btw, this might be a good place to mention that PW 2.3 dev adds a new Page::getUnknown hook (soon to be committed). That function gets called whenever a field is accessed that didn't resolve to anything. So you could [n the near future] do this (below) and you could call $page->some_radio_button_group on any page (specifically those that didn't already have the field) and have it resolve to what you want to. 

wire()->addHookAfter('Page::getUnknown', function($event) {
  $page = $event->object;
  $name = $event->arguments(0); 
  if($name == 'some_radio_button_group') $event->return = new NullPage(); 
}); 
  • Like 6
Link to comment
Share on other sites

  • 6 months later...

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