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.

Limit Plugin to Certain Categories and Discussion Types

I love this plugin: https://open.vanillaforums.com/addon/extradiscussiondata-plugin

It is a plug that allows you to add extra discussion data.

However, I cannot limit it to a certain category in my forum. If I enable it, all the extra discussion data pops up on every new post made by every user. I only want this to occur in one specific category.

Additionally, I want it it to only operate when a user selects "Ask a Question" discussion option in a specific forum category. How do I set the permissions for this or modify the code to check for this logic: If category = A && user activates QnA plugin, then activate extra discussion data plugin.

Also , I would like for this data to auto populate based on previous entries. If my extra field is an entry for 'Fruit' and there are five entries for Apple, I want it to have an autocomplete option when the user types in the letter 'A'.

Lastly, when I use this plugin, it doesn't have a line break between the extra data. How do I add a line break?

Sorry if this is asking too much. I am just trying to incorporate some information I found on this board in order to essentially combined this plugin with the Q&A plugin.

Thanks!

Comments

  • How good are your programming skills? It would require mostly Javascript knowledge.

    You would have to add a JavaScript which hooks to the category-dropdown change event. I once did this for a plugin and this is the script I've used.

    You would have to additionally control the change event for the ask a question/new discussion button to allow this only for questions.

    For the autocomplete part you might use jquery.tokeninput.js which comes with Vanilla.

  • edited January 2018

    I have programming skills but not web dev skills.

    I feel there must be a simpler way to achieve this. I am looking at the code in 'extradiscussiondata-plugin' and there is an element that checks for

    ($RequestMethod == 'discussion')

    Surely there must be a way to simply get if the discussion type is QnA or Discussion and if it is Discussion 'extradiscussiondata-plugin' is allowed to run.

    I've tried inserting this around the entire contents of the plugin:

    if ($Sender->Data('Discussion.Type') == 'QnA')
    // plugin contents pertaining to post controller
    }

    But that does not work.

  • I just posted a comment and now it's gone? Hmm

  • Anyways, I've tried a lot of thins and nothing seems to work.

    I tried writing an if statement saying :

    if ($RequestMethod != 'discussion') {

    But that does not work. I feel this should be really simple. Don't understand why it requires a js hook. This is a php plugin that is dependent on a button press (either QnA or Discussion) which is interpreted as a RequestMethod of some sort.

    What am I missing?

  • Your comment got caught in the spam filter. It's back now.

  • @hgtonight , not sure if you've revisited this plugin since January 2017, but I'd like to work together to increase the functionality of this plugin.

  • @R_J said:
    You would have to additionally control the change event for the ask a question/new discussion button to allow this only for questions.

    @VanillaNoob said:
    Don't understand why it requires a js hook. This is a php plugin that is dependent on a button press (either QnA or Discussion) which is interpreted as a RequestMethod of some sort.

    What am I missing?

    You are correct, sorry for leading you in the wrong way. This is taken from the QnA plugin:

        public function postController_beforeDiscussionRender_handler($sender) {
            // Override if we are looking at the question url.
            if ($sender->RequestMethod == 'question') {
                $sender->Form->addHidden('Type', 'Question');
                $sender->title(t('Ask a Question'));
                $sender->setData('Breadcrumbs', [['Name' => $sender->data('Title'), 'Url' => '/post/question']]);
            }
        }
    

    As you can see, you would have to check the PostControllers property RequestMethod.


    @R_J said:
    You would have to add a JavaScript which hooks to the category-dropdown change event. I once did this for a plugin and this is the script I've used.

    But this remains true. If you click the New Discussion/Ask a Question button from the homepage, you will see a Category dropdown. If you want to add a functionality only for some categories, it is good UX to disable those additional form controls for the category where they are useless/not allowed.


    @VanillaNoob said:
    What am I missing?

    If you show us some of your code, we might be able to asist.

  • @R_J , I deleted my code. I got really frustrated.

    I'll try again tomorrow and post what I end up with.

  • edited January 2018

    This is what I have in

    class.discussionextender.plugin.php

    public function PostController_BeforeFormInputs_Handler($Sender) {
    $RequestMethod = strtolower($Sender->RequestMethod);
    if ($RequestMethod == 'editdiscussion') {
    $this->AddExistingDiscussionFieldData($Sender->Form, $Sender->Data('Discussion'));
    $this->RenderDiscussionFieldInputs($Sender->Form, 'cat');
    }
    else if ($RequestMethod == 'question') {
    $this->RenderDiscussionFieldInputs($Sender->Form, 'cat');
    }
    }

    All I did was change this line:

    else if ($RequestMethod == 'discussion') {

    to this:

    else if ($RequestMethod == question') {

    I also tried:

    else if ($RequestMethod != 'discussion') {

    None of these work.

    Cannot get the js hook method to work, like I said, I deleted that code and will try again tonight.

  • edited January 2018

    What is frustrating is that if I insert this bit of code in class.discussionextender.plugin.php:

    echo $Sender->RequestMethod;

    I get either 'question' or 'discussion' as the response depending if I click on 'Ask a Question' or 'New Discussion'.

    However, if I do an if statement to allow the extra discussion data by checking the 'RequestMethod', it doesn't work. Yet I can write this if satement and have it perform as expected. What is going on?

    `$TheType =$Sender->RequestMethod;`
    `$Condition = 1;`
    `if ($TheType == 'discussion') {`
    `   $Condition = 3;`
    `}`
    
    `   echo $Condition;`
    

    }

  • I might have taken a look at the plugin before answering you. I just downloaded it in order to be able to support. This plugin takes a config file and I guess you are trying the code above in the part below, correct?

        $Config['ShowFormWhen'] = function($Sender) {
            // Some logic based on $Sender->Discussion here
            return true;
        };
    

    Your problem doesn't lie here, though...

    The Q&A plugin works by replacing the post view and this is a good example why this should be done...
    If you look at the original view (\applications\vanilla\views\post\discussion.php) from line 50 to 65 you see that there are "DiscussionOptions".

    But looking at the Q&A plugins view (\plugins\QnA\views\post.php) somewhere between $this->FireEvent('BeforeBodyInput'); and $this->FireEvent('AfterDiscussionFormOptions'); you will see that those discussion options aren't printed out for questions.

    The plugin ExtraDiscussionData extends the discussion options which are not available on a question.

    I would consider it being a bug that the Q&A plugin doesn't implement that part of the post view. So if you want to get your program up and running you would have to change one of both plugins. I think changing the Q&A plugin makes more sense and you should make a PR on GitHub to implement that change.

    Search the line $this->FireEvent('AfterDiscussionFormOptions'); \plugins\QnA\views\post.php

    and make it look like that:

        $Options = '';
        // If the user has any of the following permissions (regardless of junction), show the options.
        if (Gdn::session()->checkPermission('Vanilla.Discussions.Announce')) {
            $Options .= '<li>'.checkOrRadio('Announce', 'Announce', $this->data('_AnnounceOptions')).'</li>';
        }
    
        $this->EventArguments['Options'] = &$Options;
        $this->fireEvent('DiscussionFormOptions');
    
        if ($Options != '') {
            echo '<div class="P">';
            echo '<ul class="List Inline PostOptions">'.$Options.'</ul>';
            echo '</div>';
        }
        $this->fireEvent('AfterDiscussionFormOptions');
    

    Afterwards you should be able to use

            if ($Sender->RequestMethod == 'discussion') {
                return false;
            }
    

    in the config of the ExtraDiscussionData plugin.


    By the way: I guess you have confused the plugin author. It is @JasonBarnabe and not hgtonight you wanted to ask for cooperation...

  • I am not using the "ExtraDiscussionData" plugin.

    I am using the "DiscussionExtender" plugin.

    @R_J , thanks for all the help so far. Sort of seeing the relationship between Gdn and functions. Don't quite understand $Sender and $This, yet.

  • Just realized I made that mistake on my original post.

Sign In or Register to comment.