Henner7 Posted January 20, 2019 Share Posted January 20, 2019 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? Link to comment Share on other sites More sharing options...
dragan Posted January 20, 2019 Share Posted January 20, 2019 Works here. ProcessWire: 3.0.121 PHP: 7.2.12 Webserver: Apache/2.4 MySQL: 10.1.35-MariaDB Link to comment Share on other sites More sharing options...
dragan Posted January 20, 2019 Share Posted January 20, 2019 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. Link to comment Share on other sites More sharing options...
tpr Posted January 20, 2019 Share Posted January 20, 2019 Perhaps you need to check $item->template->hasField('id') instead. 1 Link to comment Share on other sites More sharing options...
Robin S Posted January 20, 2019 Share Posted January 20, 2019 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'. Link to comment Share on other sites More sharing options...
szabesz Posted January 21, 2019 Share Posted January 21, 2019 I wonder what "valid" mean in this context: https://processwire.com/api/ref/page/has-field/ $page->hasField() => "Returns whether or not $field is valid for this Page" Anyone can shed light on this for me? Link to comment Share on other sites More sharing options...
dragan Posted January 21, 2019 Share Posted January 21, 2019 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() 1 Link to comment Share on other sites More sharing options...
szabesz Posted January 21, 2019 Share Posted January 21, 2019 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. Link to comment Share on other sites More sharing options...
Henner7 Posted January 21, 2019 Author Share Posted January 21, 2019 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. Link to comment Share on other sites More sharing options...
Henner7 Posted January 21, 2019 Author Share Posted January 21, 2019 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. Link to comment Share on other sites More sharing options...
Robin S Posted January 21, 2019 Share Posted January 21, 2019 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) { //... } 1 Link to comment Share on other sites More sharing options...
Henner7 Posted January 23, 2019 Author Share Posted January 23, 2019 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 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now