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

How to get value of Comment body before user posts?

How can I get the value of Comment body before the user posts the comment and before it submits to the database?

Is there a variable or another way to get this information?

LindaBadurina

Comments

  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    Sure. You need to write a plugin and make use of the BeforeSaveComment event. I have written a plugin which makes use of that hook.

    You should be able to get the value of the comment in $args['FormPostValues']['Body']

  • @R_J, thanks!

    To confirm, $args[‘FormPostValues’][‘Body’] will give the value of this Comment body before it is put in the database or posted by the user? Can if work without a plugin?

  • @R_J, does this work with $this?

  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    Oh no! aarong again :angry:

    1. Stopp mentioning me immediately!
    2. Read some php tutorials
  • Hi R_J, not sure why you are calling me a different user. My username is User19.

    Just tried what you gave me by itself and it doesn’t seem to we working. Are there other methods of getting the Comment body value before it’s posted or in the Database? If this is the only method, would you be able to lead me through creating a plugin or code for this?

  • R_JR_J Cheerleader & Troubleshooter Munich Moderator
    edited December 2017

    Thanks for not using the mentioning feature! Nice to see that you make a progress there. See? It works without. I'm showing a reaction!

    But sadly I really don't see any progress in your understanding of PHP. Your questions of ways to do something without a plugin and that funny "$this" question shows that there is no progress in your PHP skills since your last appearance.

    Several users already have to tried to explain you things, but it always turned out that you don't understand a single thing. You need to work on that!
    I have given you a link to a plugin which you could use to get a blueprint. That plugin has all the information to get you started.
    Try it step by step. If you really want to learn, try the following:
    1. Alter the linked plugin in a way that it is your plugin by changing name and PluginInfo array
    2. Clear every unneeded method, only the linked method should remain
    3. With the info given in this discussion and the PHP resources all over the net, make a plugin which Makes All Words In Comments Having A
    Leading Upper Case Letter

    Show this code here and we will proceed. But no sooner.

  • user19user19 New
    edited December 2017

    Hi R_J, thank you!

    Will I be able to get the value of the body before it is posted to the DB in Vanilla/views/post/comment.php or is it only going to be available in the plugin file?

  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    The question makes no sense. Do as I told you above and you will learn and understand.

    Vanillas source code is very expressive. What would you expect from an event that is called "BeforeSaveComment"?

  • The code is giving an error.

    <?php
    
    $PluginInfo['firstPost'] = array(
        'Name' => 'Body Value',
        'Description' => 'Body value.',
        'Version' => '0.1',
        'RequiredApplications' => array('Vanilla' => '>= 2.2'),
        'RequiredPlugins' => false,
        'RequiredTheme' => false,
        'MobileFriendly' => true,
        'HasLocale' => false,
    
    
    );
    
    class FirstPostPlugin extends Gdn_Plugin {
    
    
    
        public function commentModel_beforeSaveComment_handler($sender, $args) {
            $this->setAttribute($args, 'Comment');
    
            str_replace('\' ', '\'', ucwords(str_replace('\'', '\' ', strtolower($Comment->Body))));
        }
    
    }
    
  • user19user19 New
    edited December 2017

    @R_J , Here is the plugin code with error:
    <?php

        $PluginInfo['BodyValue'] = array(
            'Name' => 'Body Value',
            'Description' => 'Body value.',
            'Version' => '0.1',
            'RequiredApplications' => array('Vanilla' => '>= 2.2'),
            'RequiredPlugins' => false,
            'RequiredTheme' => false,
            'MobileFriendly' => true,
            'HasLocale' => false,
    
    
        );
    
        class FirstPostPlugin extends Gdn_Plugin {
    
    
    
            public function commentModel_beforeSaveComment_handler($sender, $args) {
                $this->setAttribute($args, 'Comment');
    
                str_replace('\' ', '\'', ucwords(str_replace('\'', '\' ', strtolower($Comment->Body))));
            }
    
        }
    
  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    You do not even know what a variable is. Obviously you never cared to read and work through a basic tutorial. That's a problem. Without some efforts from your side that whole discussion is pointless

  • R_J, I did exactly what you said to do. It is giving me an error. Can you please assist me with debugging and tell me what to do next?

  • @R_J , would this work for what I’m trying to do?

    $PostInfo = $Sender->EventArguments['FormPostValues'];
    $PBodyText = val('Body',$PostInfo);
    
  • whu606whu606 I'm not a SuperHero; I just like wearing tights... Moderator

    If you mention R_J again, you will be banned, again.

  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    @whu606 thanks, but no need to ban him again, this time since he is only nagging me and if he really continues I'll ban him on my own, but thanks!

    @user19: this is really pointless. Instead of learning you are only doing copy and paste. Why not trying to find something that you can do well, earn money with that and pay a programmer? Obviously you do not want to become a programmer yourself.

  • @R_J ,
    Would this work?:

    $PostInfo = $Sender->EventArguments['FormPostValues'];
    $PBodyText = val('Body',$PostInfo);
    

    Should we proceed with the plugin or would that work instead?

  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    Your ignorance is frightening.

  • @R_J , can proceed with the plugin. What are the next steps?

  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    Give me a short explanation what $this is.

  • @R_J , $this->Comment->Body, etc.

  • @R_J , let’s continue the plugin to make the value of Comment body before the Comment is posted a variable.

  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    @user19 said:
    @R_J , $this->Comment->Body, etc.

    No, explain...

    I mean if you think that we can proceed, then you must have learned PHP basics way faster than I thought you would be able to. If you haven't learned the basics, we can not proceed.

    So go and understand (by reading tutorials) what $this is and tell me so that I can see that you are no lazy child.

  • @R_J , it is Object Oriented Programing (OOP).

  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    Okay Wiki, I'm out.

  • @R_J , not wiki. I know what it is. Please guide me through the plugin. I completed all the stats so far that you told me.

This discussion has been closed.