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 $Author's role in plugin using BeforeCommentMeta?

edited April 2011 in Vanilla 2.0 - 2.8
I'm trying to write a simple plugin that highlights the comments of different authors in different colors, by role. It looks like I could hook into the BeforeCommentMeta event (in applications/vanilla/views/discussion/helper_functions.php) to add a wrapper div with class="[Role]" and then use a stylesheet to customize appearance for each role.

However, I'm not sure how to access the role data for the author of each comment. Any advice on how to do this? I'm aware that $Sender, $ClassName, $EventName, and $EventArguments are passed into the callback function, but to my knowledge none of them have the $Author information. Am I wrong in this? Thanks in advance!

I realized that $Sender->EventArguments['Author']->UserID has the user's ID. How do I get the role from this?


  • Options
    bump :(
  • Options
    lucluc ✭✭
    I guess it it is as you say (no Author info).
    The easiest way is to add the role as an eventarguments to have it available in your plugin. So you'll need to change the garden/vanilla source.

  • Options
    I was afraid of that...what happens when I upgrade to a newer version of Vanilla? The purpose of plugins is to be able to modularize these changes. Is there no other way to do this? I'm trying to just run the Database queries myself:

    $Database = Gdn::Database();
    $Database->SQL()->Select('RoleID')->From('GDN_UserRole')->Where('UserID =',$UserID);
    $DataSet = $Database->Get();

    Get() doesn't seem to be defined. I feel like I'm not using the database class properly. The documentation doesn't help much either. Any suggestions here?
  • Options
    You probably want to be chaining the Get() call

    $result = $Database->SQL->Select("RoleID")
    ->From('UserRole') // I believe the automation here will auto do the prefix defined in the database singleton
    ->Where('UserID', $UserID)

    There was an error rendering this rich post.

  • Options
    Many thanks! This works wonderfully, $result['RoleID'] gets me what I need.
  • Options
    lucluc ✭✭
    @joshma: in case a new eventarguments was really required, you just have to open a bug request in github to ask for it, and it should be added to the next release.
    So no issue of reapplying your patch on every single release.

    I didn't had access to a garden db, hence I couldn't check if it could be availabled.
    But, in my opinion, if the RoleID is available before the call, it might be better to add the info, instead of doing another database request afterward. (I haven't checked the code, so no idea about that).
Sign In or Register to comment.