NodeChooser in Options

AndyB

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

Options page

pic001.webp

Editing the Options

pic002.webp

The file structure

pic003.webp

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:
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 :).
 
Back
Top Bottom