Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Access Custom Profile Fields (Profile Extender Fields) from Plugin

Hello All,

I have successfully deployed my Vanilla Forum (www.tadconnect.com/forums) and am now trying to get into customising it a little!

Thanks to all the great discussions and helpful members, I have not really had to post any of my problems, and a simple search got me solutions.

I am a hobbyist programmer and know some PHP so I am trying to write a simple plugin to pull a custom field (using profile extender plugin) from my users profile and display it against the username in each discussion/comment. However I am still struggling to properly understand the Garden framework so I am not sure exactly where to look.

Using Eventi, I have found the appropriate hook and have the following code. I am able to show the standard profile fields using the Author object, but I do not know how to access the custom fields. I have tried reading the sourcecode of Profile Extender but I'm not getting it.

This is what I have so far. Can anybody guide me further? If you can point me in the right direction I am happy to study that material. Thank you!

class ShowUserMetaPlugin extends Gdn_Plugin {

public function DiscussionController_AuthorInfo_Handler($Sender) {

    // Get the Author object
    $Author = GetValue('Author', $Sender->EventArguments);
    echo "($Author->Email)";
    //print_r($Author);
}

}

Tagged:

Comments

  • Use The Source, young Jedi ;)

    By the way: I've once read an article about why you should prefer var_dump over print_r. It was something about the info about the variable type, I think.
    The function decho is Vanillas way of using var_dump. Try this two use cases:
    decho('Only visible by admins - very handy!');
    decho('But sometimes you have to debug as a normal user', 'DEBUG: ', true);

  • Thanks for the tip @R_J !

    I had actually seen this but have no clue how to use it. How do I get hold of a ProfileExtender class in my plugin? Also this particular function is private one.

    Am I missing something obvious?!

  • Ah, I have led you in a totally wrong direction! Look here: http://vanillaforums.org/discussion/comment/217856/#Comment_217856

    You need Gdn::UserMetaModel()->GetUserMeta($Session->UserID, 'Profile.YourField');

    https://github.com/vanilla/vanilla/blob/master/applications/dashboard/models/class.usermetamodel.php#L51

    Sorry... :(

  • @Bleistivt‌ & @R_J‌, thank you so much guys! I dont know how I missed the post by piotrni. Thats exactly what I wanted to do. And its working fine.

    I do have a couple of questions about the code though.

    I had actually come across the GetUserMeta function while digging through code, but I didnt understand what the $Key parameter meant.

    @param $UserID integer UserID or array of UserIDs
    @param $Key string relative user meta key
    @param $Default optional default return value if key is not found
    @return array results or $Default
    public function GetUserMeta($UserID, $Key = NULL, $Default = NULL) {
    

    From the example in the other thread $Key is entered as 'Profile%'. What does this mean?

    Thanks again!

  • The key is whatever you want it to be!

    Generally, a plugin will set user meta and get user meta, so you just pick what key you want to use. You generally namespace your keys with dots (.) so that collisions are unlikely. So you pick keys with PluginName.MetaName for example. As a matter of a fact, if you use the plugin's convenience methods (SetUserMeta and GetUserMeta), the name spacing is done automatically for you.

    In this case, you are bypassing the plugin's convenience functions because you want to access meta used by another plugin. So you use the UserMetaModel::GetUserMeta(). Then you also pass a wildcard to it (the % is the MySQL wildcard character) so you get all the meta entries for that user for that plugin.

    Does this make sense?

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • Thanks @hgtonight‌ . It makes some sense. I am in the process of studying everything I can on the vanillawiki.homebrewforums.net to understand the framework and how it works. Hopfully will make more sense after I go through all of this!

Sign In or Register to comment.