Robin S Posted April 28, 2016 Posted April 28, 2016 I have added an image field "avatar" to the user template. The field settings are "Maximum files: 1", "Formatted value: Single items (null if empty)". So I believe I should be able to get the URL of this image in my template with $user->avatar->url But this returns /site/assets/files/1217/ If I add the avatar field to any other template besides the User template I can get the URL as expected. For the user template if I use $user->avatar->first->url I get the URL okay. So it's like image fields added to the User template don't honour the "Formatted value" setting. Has anyone else experienced this? If so I guess it's bug and I'll file an issue on Github. Edit: If I get a specific User page it works normally $pages(1217)->avatar->url So it's just an issue when trying to get the image from the User object. Is this difference from the Page object likely to be intentional? 3
adrian Posted April 28, 2016 Posted April 28, 2016 This seems like it might go back a very long time: https://processwire.com/talk/topic/1125-images-field-returning-array-even-when-max-files-set-to-1/ I feel like I have come across this before too and been in a hurry and just used ->first() rather than trying to debug it. A couple of things that work other than first() $user->of(true); echo $user->avatar->url; or this: $u = $users->get($user->id); echo $u->avatar->url; Both of these work. In a rush so not sure why - I don't see why the $user object seems to have outputformatting off by default - maybe this is intentional, but I don't recall. 5
Robin S Posted April 28, 2016 Author Posted April 28, 2016 (edited) Thanks. Probably the easiest thing for me is to put: $user->of(true); in my templates' auto-prepended init.php and then I can use the field consistently across $user and $page. (edited for clarity) Some of Ryan's comments in the old thread you linked to make it sound like having output formatting off for the user template is deliberate, but then the docs say Note that a User is just another type of Page, and likewise inherits all the other methods of $page. and Depending on the context, you may have to call $user->of(false); to turn off output formatting before assigning values to a user. which make it sound like $user should be essentially the same as $page and will have outputting formatting on by default. Edited April 28, 2016 by Robin S 1
adrian Posted April 28, 2016 Posted April 28, 2016 Beware of putting $user->of(true); in init.php because it will also be loaded for the admin (as well as the frontend) which I expect will cause all sorts of problems. 2
Robin S Posted April 28, 2016 Author Posted April 28, 2016 Beware of putting $user->of(true); in init.php because it will also be loaded for the admin (as well as the frontend) which I expect will cause all sorts of problems. Are you sure that's true? Good to know if it is, but tried testing it by putting die() in my init.php and admin is unaffected.
adrian Posted April 28, 2016 Posted April 28, 2016 Are you sure that's true? Good to know if it is, but tried testing it by putting die() in my init.php and admin is unaffected. I am talking about site/init.php - that is definitely loaded in the admin. Maybe you are referring to your own manually included init.php that is in the templates folder? 2
Robin S Posted April 28, 2016 Author Posted April 28, 2016 Ahh, right. I was meaning the file I have auto-prepending to my templates, which I happen to have called init.php. I should have been clearer. 2
szabesz Posted April 28, 2016 Posted April 28, 2016 I think Robin S is right. It would be nice if this behavior was pointed out in the docs. 1
adrian Posted May 1, 2016 Posted May 1, 2016 https://github.com/ryancramerdesign/ProcessWire/issues/1803 Will see what Ryan has to say. 2
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