Henner7

Problems with hasField('id')

Recommended Posts

I have a question about the hasField function. I run the following code in site/templates/_main.php:

      $myPages = $pages->find('parent=/abteilungen')->sort('title');
      foreach($myPages as $item) {
        print $item->title."<br />";
        if($item->hasField('id')) {
            print "hasId";
        }
        else {
            print "hasNoId";
        }

I get for all items "hasNoId".  With processwire 3.0.123 and Php 7.2.13.

For older Php 5.6 and older processwire versions the field 'id' was found. Also, I know that each item should have a field 'id' anyway but I think this can not always be taken as granted.

Any suggestions?

Share this post


Link to post
Share on other sites

Works here.

ProcessWire: 3.0.121
PHP: 7.2.12
Webserver: Apache/2.4
MySQL: 10.1.35-MariaDB

Share this post


Link to post
Share on other sites
19 minutes ago, Henner7 said:

Also, I know that each item should have a field 'id' anyway but I think this can not always be taken as granted.

I don't know your setup / scenario, but if you need to make sure each page-type needs to have a certain field, you can configure your templates, i.e. don't allow pages with template x to be moved (change parent), or only allow children of 'abteilungen' to use template x.

Share this post


Link to post
Share on other sites

Perhaps you need to check $item->template->hasField('id') instead.

  • Like 1

Share this post


Link to post
Share on other sites
9 hours ago, Henner7 said:

Also, I know that each item should have a field 'id' anyway but I think this can not always be taken as granted.

A page's ID is a property of the page but it's not a field that belongs to the page's template/fieldgroup. So $page->hasField() would not be expected to return true for 'id'.

Share this post


Link to post
Share on other sites
Quote

 

Note that this only indicates validity, not whether the field is populated.


 

It basically means: Does that page (and hence, its template) has this field or not? I guess it's like a shortcut for page->template->hasField()

  • Like 1

Share this post


Link to post
Share on other sites
1 hour ago, dragan said:

Does that page (and hence, its template) has this field or not?

Thanks, sure, the wording "hasField" does indeed imply that, it is just that using "valid" in this context is not something I remember seeing before.

Share this post


Link to post
Share on other sites
On 1/20/2019 at 1:54 PM, tpr said:

Perhaps you need to check $item->template->hasField('id') instead.

Thank you, however, this did not change anythin.

Share this post


Link to post
Share on other sites
20 hours ago, Robin S said:

A page's ID is a property of the page but it's not a field that belongs to the page's template/fieldgroup. So $page->hasField() would not be expected to return true for 'id'.

Thank you, I assume this is the final answer. Something seems to have changed in the last versions of processwire.

I did a further test: If I try to create a field "id", I get the error message: Field name "id" is a reserved word. Therefore there can never be a field called 'id' in a template.

Share this post


Link to post
Share on other sites
11 hours ago, szabesz said:

I wonder what "valid" mean in this context

I agree the wording could be clearer here.

If you look at the method code for Page::hasField() it is:

return $this->template ? $this->template->fieldgroup->hasField($field) : false;

So it's just an alias for Fieldgroup::hasField() where the fieldgroup is that used by the page's template. And the explanation for Fieldgroup::hasField() is:

Quote

Does this fieldgroup having the given field?

Not great English but you get the meaning. So if the given field name / field id / field object is not part of the page's fieldgroup then Page::hasField() will return false.

 

@Henner7, if your intention is to check whether $item has an ID property and the property is not 0 (i.e. it is not a NullPage) then typically you would just check the "truthiness" of the ID property...

if($item->id) {
    //...
}

 

  • Like 1

Share this post


Link to post
Share on other sites
On 1/21/2019 at 10:17 PM, Robin S said:

if your intention is to check whether $item has an ID property and the property is not 0 (i.e. it is not a NullPage) then typically you would just check the "truthiness" of the ID property...

Thanks, I have overseen this easy approach.

Thanks to all for the help. I think we can close this thread now. If you like to have a look on my pages: https://tsvwesthausen.de

  • Like 1

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.