Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Try Vanilla Forums Cloud product

Get the post/topic creator name

Hallo Forum,
I guess this might be the simple questions for you guys.
How do you get the post creator name.
for example: with
echo userAnchor($Author, 'Username'); you get the user name.
And I want to check if this user is also a post creator. something like this:

 if ($Author) == $postCreator) {
    echo'this user is also a post creator';
 } else {
  echo'this user is not a post creator';
 }

The above code is just an example just to make my question clear.

I would appreciate your help.
Thank you!!!

Comments

  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    So you are looking at a comment? In most cases you have
    $sender->EventArguments['Discussion'] available and you can check $sender->EventArguments['Comment']->InsertUserID == $sender->EventArguments['Discussion']->InsertUserID

    But if you tell us which event you are using, it would be much easier.

    vanillawhiskyhgtonight
  • @R_J said:
    So you are looking at a comment? In most cases you have
    $sender->EventArguments['Discussion'] available and you can check $sender->EventArguments['Comment']->InsertUserID == $sender->EventArguments['Discussion']->InsertUserID

    But if you tell us which event you are using, it would be much easier.

    Yes I am looking at comment. On discussion.php (where you get post heading) you have $sender->EventArguments['Discussion'] and I also tried writing $sender->EventArguments['Discussion'] on comment page but it gets null value.
    I tried this:

    if (!function_exists('WriteComment')):
     function writeComment($Comment, $Sender, $Session, $CurrentOffset) {
            .......................
           $Sender->EventArguments['Discussion'] = &$Discussion;
           var_dump($Discussion);
    }
    

    and I get null value. So how can I proceed.

  • R_JR_J Cheerleader & Troubleshooter Munich Moderator
        public function discussionController_BeforeCommentDisplay_handler($sender, $args) {
            decho($args['Comment']->InsertUserID, 'comment user id');
            decho($args['Discussion']->InsertUserID, 'discussion user id');
        }
    

    When I put this in a plugin, I get the correct information.

    Maybe the problem is the way you write your code. Do you use plugins and themehooks or are you trying to override files?

  • @R_J said:
    Maybe the problem is the way you write your code. Do you use plugins and themehooks or are you trying to override files?

    I am not using from plugins and themehooks but trying to overwrite helper_functions.php file in a function
    if (!function_exists('WriteComment')):

  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    Örks. Are you really convinced that this is needed? Most of the times using css and a themehooks file is all you need.

    But anyway: $Sender->EventArguments['Discussion']->InsertUserID will give you the discussion authors user id in this case.

    In your code above you've used an ampersand before the variable name. I do not know why you have done so, but I bet it was the reason why your first try wasn't successful.

  • edited August 2016

    @R_J said:
    But anyway: $Sender->EventArguments['Discussion']->InsertUserID will give you the discussion authors user id in this case.

    In your code above you've used an ampersand before the variable name. I do not know why you have done so, but I bet it was the reason why your first try wasn't successful.

    Well this 'ampersand ' is by default from vanilla. even without 'ampersand ' the result is same, NULL.
    In views/discussion/discussion.php this code $Sender->EventArguments['Discussion']->InsertUserID works fine but not in views/discussion/helper_functions.php because here are only comments not the discission titles (i guess).
    Can you please test,if possible, in yours and let me know.
    my main task is to see if the creator comments on his own discussion .
    For example if I comment on my own discussion then beside my name there will be small text with 'creator'.
    I hope you understand my question.
    as an exaple , you can see schreenshot in spoiler.

    I want this text just in comment section .

  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    Yes, it is working for me. But instead of overriding a view, that would be the way to achieve what you've asked for:

        public function discussionController_authorInfo_handler($sender, $args) {
            if ($args['Comment']->InsertUserID == $args['Discussion']->InsertUserID) {
                echo '<span class="MItem IsDiscussionAuthor">'.t('(Creator)').'</span>';
            }
        }
    

    Do you know about themehooks? Create a custom theme and put that code in your themehooks file.

  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    And if you would like to only style such comments differently, you would use something like that:

        public function discussionController_beforeCommentDisplay_handler($sender, $args) {
            if ($args['Comment']->InsertUserID == $args['Discussion']->InsertUserID) {
                $args['CssClass'] .= ' DiscussionAuthorComment';
            }
        }
    

    ... just to show you what you can achieve with Vanilla events ;)

  • @R_J said:
    Yes, it is working for me. But instead of overriding a view, that would be the way to achieve what you've asked for:

        public function discussionController_authorInfo_handler($sender, $args) {
            if ($args['Comment']->InsertUserID == $args['Discussion']->InsertUserID) {
                echo '<span class="MItem IsDiscussionAuthor">'.t('(Creator)').'</span>';
            }
        }
    

    I also tried with themehooks but ,dont know why, its not working.
    I created a file class.themehooks.php in themes/mythemename/class.themehooks.php.
    and inside i have this code:

    class MythemenameThemeHooks implements Gdn_IPlugin {
        public function setup () {
            return true;
        }
        public function onDisable () {
            return true;
        }
        public function discussionController_authorInfo_handler($sender, $args) {
           if ($args['Comment']->InsertUserID == $args['Discussion']->InsertUserID) {
               echo '<span class="MItem IsDiscussionAuthor">'.t('(Creator)').'</span>';
           }
        }
    }
    

    and now when I trigger an event in \themes\mythemename\views\discussion\helper_functions.php with:
    <?php $Sender->fireEvent('AuthorInfo'); ?>
    I get nothing.
    Is there anything missing?
    I also tried with test value:

    public function discussionController_authorInfo_handler() { echo ('testing'); }
    as a matter of fact , this function is never called.
    Can you please suggest me what is wrong ?

  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    The filename of your themehooks file must be class.yourpluginsnamethemehooks.php

    vanillawhiskyhgtonight
  • edited August 2016

    @R_J said:
    The filename of your themehooks file must be class.yourpluginsnamethemehooks.php

    Well Thanks. This atleast works. So what about this document http://docs.vanillaforums.com/developer/theming/hooks/
    is this wrong ?

    But my problem ist still ON.
    In \themes\mythemename\views\discussion\helper_functions.php,
    I have:

    $Sender->EventArguments['Discussion'] = &$Discussion;
    $Sender->EventArguments['Comment'] = &$Comment;
    ......
    $Sender->fireEvent('AuthorInfo');
    

    (note: if i dont use 'ampersand' it says undefined variables. )

    and in themehook, when I var_dump the commented userID, it gives the value:

    public function discussionController_authorInfo_handler($sender, $args) {
           var_dump(($args['Comment']->InsertUserID));
    }
    

    Output is : /.../themes/mythemename/class.whiskythemehooks.php:13:int 19068

    But when I try to discussion`s userID ,it gives null:

    public function discussionController_authorInfo_handler($sender, $args) {
           var_dump(($args['Discussion']->InsertUserID));
    }
    

    Output is : /.../themes/mythemename/class.whiskythemehooks.php:13:int null

  • @R_J
    Well I found the problem.
    I forget to get the discussion data fron Gdn Controller.
    so the solution is:
    Since I have rewrite only the 'WriteComment' function,
    In \themes\mythemename\views\discussion\helper_functions.php, add
    $Discussion = Gdn::controller()->data('Discussion');
    and everything works fine.
    Thank you for your time @R_J

  • @R_J just wondering, How can I mark my discussion as solved?

  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    @vanillawhisky said:
    So what about this document http://docs.vanillaforums.com/developer/theming/hooks/
    is this wrong ?

    Yes, obviously. You can correct it here and help others!

    @vanillawhisky said:
    @R_J just wondering, How can I mark my discussion as solved?

    I don't know. A mod would have to do it, but I don't think it is that important...

    @vanillawhisky said:
    Since I have rewrite only the 'WriteComment' function,

    If it is only one or the other function that you like to replace, I would advice you not to replace the complete file. Just add those functions e.g. below the themehooks class

    vanillawhisky
Sign In or Register to comment.