By Steel Rat on
I've been searching for this for days. Either I'm not entering the right search terms, or the answer doesn't exist.
Anyway, I've created four fields under Configuration>People>Account Settings>Manage Fields, and I just want to print the values stored there for the author of a node or comment. I don't need a new module or anything, just a simple PHP snippet that shows how to print fields create in the above section. I've found many snippets, but none work for me in D7.
The field names are:
field_user_location
field_user_occupation
field_user_witticism
field_user_skype
Surely this is a very simple thing! (And yes! I did call you surely)
Free attaboy for the first correct answer!
Comments
I could be talking out of my hat but...
Have you tried outputting the user object? I don't have anything setup to test this but I'd assume that user fields would load as part of $user in the same way that when you load the node object it includes any custom fields.
I saw you are trying to use Author Pane...
I saw your comment in the Author Pane docs (http://drupal.org/node/1488468), so I assume that using this module is acceptable. ;-)
Now, assuming you are editing the author-pane.tpl.php file (original or, better, a copy in your custom theme), you could add a block like (I got this to work in my D7 "test" site):
Note that you might want to var_dump() the $account variable to get all available variables and the full access parameters, since this may vary depending on your settings (i.e. if you have multilingual support enabled, etc). Devel is your friend, too...
If you look at the profile edit form for a user, you should see those custom fields and be able to also see the full field names in the HTML. In my form, I'd added the "field_user_occupation", but its value looks like this when viewed in with "inspect element" (Safari):
name="field_user_occupation[und][0][value]"You'll need quotes around the 'und' and/or 'value' parts in your tpl.php file, though (as above).
Hope that helps. :-)
See you at the Drupalcon!
Thanks for the replies. Yes
Thanks for the replies. Yes I'm using Author Pane ;).
I'm trying to get these variables into the Advanced Forum Author Pane tpl, since the modules which would normally take care of this haven't been written for D7.
I'll give your example a shot, LoMo, and see how it works.
Thanks again!
Oh, and what's the deal with
Oh, and what's the deal with the 'und' in the array indices?
Perfect!
LoMo, your example worked perfectly! I just pasted it in several times to get all the fields I needed displayed.
Thank you!
Glad it worked for you! :-)
BTW: The
['und']is the language code for content which has not been assigned to a language (i.e. language = "neutral"), i.e. "undefined". Otherwise it could be ['en'] (English), ['de'] (German), ['es'] (Spanish), etc. Identifying the fields for multi-value fields gets more complicated, so it's always best to just examine the fields in "Firebug" or whatever, if you need to manipulate them.See you at the Drupalcon!
Understood
Thanks again for all your help. Hopefully someday I can return the favor.
Hi Steel Rat,I have now
Hi Steel Rat,
I have now found out how to do what you seeking. You should use the field_view_field() function.
----------
I know you asked for how to do it in Author Pane then you should do something like this:
For more info how to display the label or not and so on, see this comment for example.
field_view_value() could also
field_view_value() could also be used.