1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

ForumChooser in Options

Discussion in 'XenForo Development Discussions' started by AndyB, Dec 2, 2015.

  1. AndyB

    AndyB Well-Known Member

    In one of my add-ons I have a need in the Options to be able to select forums. The following code works very well with the exception that nodes besides Forums can be selected. I would like to make it so that nodes which are a Category, Page or LinkForum cannot be selected, only Forums should be able to be selected.

    In this example "Community" is a Category:

    pic001.jpg

    Options page code:

    pic002.jpg

    Folder and Files

    pic003.jpg


    ForumChooser.php

    PHP:
    <?php

    abstract class Andy_SimilarThreads_Option_ForumChooser
    {
        public static function 
    renderOption(XenForo_View $view$fieldPrefix, array $preparedOption$canEdit)
        {
            
    $value $preparedOption['option_value'];

            
    $editLink $view->createTemplateObject('option_list_option_editlink', array(
                
    'preparedOption' => $preparedOption,
                
    'canEditOptionDefinition' => $canEdit
            
    ));

            
    $nodeModel XenForo_Model::create('XenForo_Model_Node');

            
    $forumOptions $nodeModel->getNodeOptionsArray($nodeModel->getAllNodes(), $preparedOption['option_value'], '(None)');
         
            return 
    $view->createTemplateObject('andy_option_list_option_select', array(
                
    'fieldPrefix' => $fieldPrefix,
                
    'listedFieldName' => $fieldPrefix '_listed[]',
                
    'preparedOption' => $preparedOption,
                
    'formatParams' => $forumOptions,
                
    'editLink' => $editLink
            
    ));
        }
    }

    Admin Template -> andy_option_list_option_select

    Code:
    <xen:selectunit label="{$preparedOption.title}" name="{$fieldPrefix}[{$preparedOption.option_id}]" value="{xen:raw $preparedOption.option_value}"
    hint="{$preparedOption.hint}" size="10" multiple="true">
        <xen:options source="$formatParams" />
        <xen:explain>{xen:raw $preparedOption.explain}</xen:explain>
        <xen:html>
            <input type="hidden" name="{$listedFieldName}" value="{$preparedOption.option_id}" />
            {xen:raw $editLink}
        </xen:html>
    </xen:selectunit>
    
     
  2. batpool52!

    batpool52! Well-Known Member

    You need to check for node_type_id and see if its not 'Forum' and then make option "disabled".
     
    AndyB likes this.
  3. AndyB

    AndyB Well-Known Member

    Hi batpool52!,

    Thank you for the tip.

    In the following ForumChooser.php code I simply remove the Category, Page or ForumLink nodes, but this approach is less than ideal because it's nice to be able to see the Categories. Can you tell me how I can make them "disabled" instead.

    PHP:
    <?php

    abstract class Andy_SimilarThreads_Option_ForumChooser
    {
        public static function 
    renderOption(XenForo_View $view$fieldPrefix, array $preparedOption$canEdit)
        {
            
    $value $preparedOption['option_value'];

            
    $editLink $view->createTemplateObject('option_list_option_editlink', array(
                
    'preparedOption' => $preparedOption,
                
    'canEditOptionDefinition' => $canEdit
            
    ));

            
    $nodeModel XenForo_Model::create('XenForo_Model_Node');

            
    $forumOptions $nodeModel->getNodeOptionsArray($nodeModel->getAllNodes(), $preparedOption['option_value'], '(None)');

            
    // remove nodes which are not forums
            
    foreach ($forumOptions AS $k => $v)
            {
                foreach (
    $v AS $k2 => $v2)
                {              
                    if (
    $k2 == 'node_type_id')
                    {
                        if (
    $v2 == 'Category' OR $v2 == 'Page' OR $v2 == 'LinkForum')
                        {                      
                            unset(
    $forumOptions[$k]);
                        }
                    }
                }
            }
          
            return 
    $view->createTemplateObject('andy_option_list_option_select', array(
                
    'fieldPrefix' => $fieldPrefix,
                
    'listedFieldName' => $fieldPrefix '_listed[]',
                
    'preparedOption' => $preparedOption,
                
    'formatParams' => $forumOptions,
                
    'editLink' => $editLink
            
    ));
        }
    }
     
  4. AndyB

    AndyB Well-Known Member

  5. AndyB

    AndyB Well-Known Member

    When I add a the following code to the ForumChooser.php file:

    print_r($forumOptions);

    It shows the following:

    Code:
    (
        [0] => Array
            (
                [value] => 0
                [label] => (None)
                [selected] =>
                [depth] => 0
            )
    
        [50] => Array
            (
                [value] => 50
                [label] => Community
                [selected] =>
                [depth] => 1
                [node_type_id] => Category
            )
    
        [5] => Array
            (
                [value] => 5
                [label] => General
                [selected] =>
                [depth] => 2
                [node_type_id] => Forum
            )
    
        [73] => Array
            (
                [value] => 73
                [label] => Off-Topic
                [selected] =>
                [depth] => 2
                [node_type_id] => Forum
            )
    
        [127] => Array
            (
                [value] => 127
                [label] => In The News
                [selected] =>
                [depth] => 3
                [node_type_id] => Forum
            )
    
        [129] => Array
            (
                [value] => 129
                [label] => Music
                [selected] =>
                [depth] => 3
                [node_type_id] => Forum
            )
    
        [19] => Array
            (
                [value] => 19
                [label] => Pictures
                [selected] =>
                [depth] => 2
                [node_type_id] => Forum
            )
    
        [4] => Array
            (
                [value] => 4
                [label] => Videos
                [selected] =>
                [depth] => 2
                [node_type_id] => Forum
            )
    
        [64] => Array
            (
                [value] => 64
                [label] => Dirt Riders
                [selected] =>
                [depth] => 2
                [node_type_id] => Forum
            )
    
        [178] => Array
            (
                [value] => 178
                [label] => Supercross/Motocross
                [selected] =>
                [depth] => 3
                [node_type_id] => Forum
            )
    
    Do I need to add the following to each array which is not a Forum?

    disabled => 1
     
    Last edited: Dec 2, 2015
  6. AndyB

    AndyB Well-Known Member

    Got it!

    ForumChooser.php

    PHP:
    <?php

    abstract class Andy_SimilarThreads_Option_ForumChooser
    {
        public static function 
    renderOption(XenForo_View $view$fieldPrefix, array $preparedOption$canEdit)
        {
            
    $value $preparedOption['option_value'];

            
    $editLink $view->createTemplateObject('option_list_option_editlink', array(
                
    'preparedOption' => $preparedOption,
                
    'canEditOptionDefinition' => $canEdit
            
    ));

            
    $nodeModel XenForo_Model::create('XenForo_Model_Node');

            
    $forumOptions $nodeModel->getNodeOptionsArray($nodeModel->getAllNodes(), $preparedOption['option_value'], '(None)');

            
    // disable nodes which are not forums
            
    foreach ($forumOptions AS $k => $v)
            {
                foreach (
    $v AS $k2 => $v2)
                {                
                    if (
    $k2 == 'node_type_id')
                    {
                        if (
    $v2 == 'Category' OR $v2 == 'Page' OR $v2 == 'LinkForum')
                        {
                            
    $forumOptions[$k] += array(
                                
    'disabled' => 1
                            
    );
                        }
                    }
                }
            }
        
            return 
    $view->createTemplateObject('andy_option_list_option_select', array(
                
    'fieldPrefix' => $fieldPrefix,
                
    'listedFieldName' => $fieldPrefix '_listed[]',
                
    'preparedOption' => $preparedOption,
                
    'formatParams' => $forumOptions,
                
    'editLink' => $editLink
            
    ));
        }
    }
     
  7. Jeremy P

    Jeremy P Well-Known Member

    Maybe you already know this and I'm missing something, but XenForo includes a built-in NodeChooser callback class for options that is pretty flexible: XenForo_Option_NodeChooser.

    The following (from deprecated XenForo_Option_ForumChooser) would only pull forums:
    PHP:
    $preparedOption['nodeFilter'] = 'Forum';
    XenForo_Option_NodeChooser::renderSelect($view$fieldPrefix$preparedOption$canEdit);
     
    HWS likes this.
  8. AndyB

    AndyB Well-Known Member

    Hi Jeremy P,

    I do recall seeing that class, but I don't know how to use it to display a node list in the Options page of an add-on.
     
  9. Jeremy P

    Jeremy P Well-Known Member

    Ah nevermind. Just realized you need a <select multiple> and the built-in class doesn't support the multiple attribute.

    You could extend it with a custom class to do what you need with relative ease, but since you've already got it figured out it's probably fine as-is.
     
  10. AndyB

    AndyB Well-Known Member

    The one thing which worries me is this class has been depreciated.

    PHP:
    <?php

    /**
    * Deprecated. Use XenForo_Option_NodeChooser::renderSelect
    */
    abstract class XenForo_Option_ForumChooser
    {
        
    /**
         * Deprecated
         *
         * @see XenForo_Option_NodeChooser::renderSelect
         */
        
    public static function renderOption(XenForo_View $view$fieldPrefix, array $preparedOption$canEdit)
        {
            
    $preparedOption['nodeFilter'] = 'Forum';
            return 
    XenForo_Option_NodeChooser::renderSelect($view$fieldPrefix$preparedOption$canEdit);
        }
    }
    I assume this means that in a future XenForo version this class will remove be removed and any add-on using the class no longer work.
     
  11. AndyB

    AndyB Well-Known Member

    Last edited: Dec 2, 2015
    Jeremy P likes this.

Share This Page