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

NodeChooser in Options

AndyB

Well-known member
#1
This is how I set up a Node Chooser in the Options page of my Similar Threads add-on.

Options page

pic001.jpg

Editing the Options

pic002.jpg

The file structure

pic003.jpg

NodeChooser.php

PHP:
<?php

abstract class Andy_SimilarThreads_Option_NodeChooser
{
    public static function renderSelect(XenForo_View $view, $fieldPrefix, array $preparedOption, $canEdit)
    {
        return self::_render('andy_option_list_option_select', $view, $fieldPrefix, $preparedOption, $canEdit);
    }

    public static function getNodeOptions($selectedForum, $includeRoot = false, $filter = false)
    {
        /* @var $nodeModel XenForo_Model_Node */
        $nodeModel = XenForo_Model::create('XenForo_Model_Node');

        $options = $nodeModel->getNodeOptionsArray(
            $nodeModel->getAllNodes(),
            $selectedForum,
            $includeRoot
        );
    
        // define filter
        $filter = 'Forum';

        if ($filter)
        {
            foreach ($options AS &$option)
            {
                if (!empty($option['node_type_id']) && $option['node_type_id'] != $filter)
                {
                    $option['disabled'] = 'disabled';
                }

                unset($option['node_type_id']);
            }
        }

        return $options;
    }

    protected static function _render($templateName, XenForo_View $view, $fieldPrefix, array $preparedOption, $canEdit)
    {
        $filter = isset($preparedOption['nodeFilter']) ? $preparedOption['nodeFilter'] : false;

        $preparedOption['formatParams'] = self::getNodeOptions(
            $preparedOption['option_value'],
            sprintf('(%s)', new XenForo_Phrase('unspecified')),
            $filter
        );

        return XenForo_ViewAdmin_Helper_Option::renderOptionTemplateInternal(
            $templateName, $view, $fieldPrefix, $preparedOption, $canEdit
        );
    }
}
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>
 
Last edited:

nrep

Well-known member
#4
Thanks for posting this, I'm working on replacing some of my old manual selection code and using a forum chooser - so this is really handy :).