Jump to content

Image field in User template not behaving normally


Robin S
 Share

Recommended Posts

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?

  • Like 3
Link to comment
Share on other sites

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.

  • Like 5
Link to comment
Share on other sites

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 by Robin S
  • Like 1
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

  • Like 2
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...