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.

How to use ProfileExtender to show users state

I setup a field in profile extender called 'State/Location' and I'm trying to get the information to display next to users names when they post. I found a snippet of code from @peregrine but I'm uncertain how to modify it to correctly do what I'm trying to do.

       public function DiscussionController_AuthorInfo_Handler($Sender) {
           $Type= $Sender->EventArguments['Type'];
            if ($Type == 'Discussion') {
            $Author = $Sender->EventArguments['Discussion'];
            } else {
            $Author = $Sender->EventArguments['Comment'];
            $AuthorID = $Author->InsertUserID;
            $NameofProfileField = "Profile.Testing";
            $SQL = Gdn::SQL();
            $this->PfeData = $SQL
            ->Where(array('UserID' =>$AuthorID,'Name' => $NameofProfileField ))  
            echo WrapIf(htmlspecialchars(GetValue('Value', $this->PfeData)), 'span', array('class' => 'MItem AuthorTesting'));  

Any suggestions would be welcome!


  • Options
    edited August 2017

    my best guess was to change

    $NameofProfileField = "Profile.Testing";


    $NameofProfileField = "Profile.State/Location";

    but that's not working.

  • Options
    R_JR_J Ex-Fanboy Munich Admin

    I am a fan of making people understand what they do. Take a look at this two places

    The first line hands over the author of a comment to an array called EventArguments and the second lines fires an event where you can take action (in this case, by echoing something to the screen)

    Both lines are inside of the function writeComment but it should also work on the opening post, which in Vanilla-speak is called the discussion

    Those two lines do exactly the same for discussions.

    Have you taken a good look at the links? Do so! You will note that in the line above the fireEvent, there is the following line: echo ' '.wrapIf(htmlspecialchars(val('Location', $Author)), 'span', array('class' => 'MItem AuthorLocation'));

    wrapIf would enclose the first parameter (htmlspecialchars(val('Location', $Author))) with a span tag, and does nothing if the first parameter is empty
    htlspecialchars transforms "bad" characters in user names to prevent XSS attacks
    val('Location', $Author)looks for a field "Location" in $Author, 1. no matter if $Author is an array and it would be $Author['Location'] or if it is an object and it would be $Author->Location and 2. it will not result in an error if there is no key "Location".

    So obviously there is something already in Vanillas users which is called "Location", wouldn't it be great if you could use that? ;-)

    There are two special fields in the user table which are not handled by Vanilla, but can be "activated" with the ProfileExtender.

    If you create a field "Title" and/or a field "Location" with the ProfileExtender, those values will be displayed at several places. Try renaming your "State/Location" to "Location"

  • Options

    I can sort of follow what is happening there. I tried changing the name of the field to "Location" but it is still not showing up.

  • Options

    Turns out I just needed to create a new field called "Location" and then it shows up. Good to know.

  • Options

    Thank you for all your help!

Sign In or Register to comment.