Search code examples
phpcheckboxadminxenforo

How to get checkboxes to repopulate using xenforo options


Im using the following function in XenForo and the checkboxes are created for each node and the chosen options are saved in the db, but when the option is reloaded, the saved values are not being repopulated in the form.

Option:

<option option_id="hc_watched_forums_list" edit_format="callback" data_type="array" can_backup="1">
<default_value>a:6:{i:0;s:1:"4";i:1;s:1:"5";i:2;s:1:"6";i:3;s:1:"7";i:4;s:2:"36";i:5;s:2:"38";}</default_value>
            <edit_format_params>HotCopper_Option_NodeChooser::renderCheckbox</edit_format_params>
<sub_options>*</sub_options>
<relation group_id="hc_misc_options" display_order="3306"/>
</option>

Function: (simplified)

class HotCopper_Option_NodeChooser extends XenForo_Option_NodeChooser
{
    public static function renderCheckBox(XenForo_View $view, $fieldPrefix, array $preparedOption, $canEdit)
    {
        return self::_render('option_list_option_checkbox', $view, $fieldPrefix, $preparedOption, $canEdit);
    }
}

Can you suggest why its not repopulating the checkboxes and what I can do to fix it. Thanks


Solution

  • I overrode original xenforo node model and option. The option just needs to be changed to call the new model, and the model needed the selected to check if the nodeid was in the value array.

    public static function getNodeOptions($selectedForum, $includeRoot = false, $filter = false)
        {
            /* @var $nodeModel HotCopper_Model_Node */
            $nodeModel = XenForo_Model::create('HotCopper_Model_Node');
    
            $options = $nodeModel->getNodeOptionsArraySelectedArray(
                $nodeModel->getAllNodes(),
                $selectedForum,
                "Choose Required Forums"
            );
    
            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;
        }
    
    public function getNodeOptionsArraySelectedArray(array $nodes, array $selectedNodes, $includeRoot = false)
        {
            $options = array();
    
            foreach ($nodes AS $nodeId => $node)
            {
                $node['depth'] += (($includeRoot && $nodeId) ? 1 : 0);
    
                $options[$nodeId] = array(
                    'value' => $nodeId,
                    'label' => $node['title'],
                    'selected' => (in_array($nodeId,$selectedNodes)),
                    'depth' => $node['depth'],
                    'node_type_id' => $node['node_type_id']
                );
            }
    
            return $options;
        }