Adding a new forum setting

Gossamer

Active member
Hello! So, I'm trying to add a new option when creating a forum node. I've already created the option in the admin CP as a radio button list. The new field has been added to the xf_forum table, called 'forum_type'.

However, when I try to save the forum node, nothing gets saved to the forum_type field.

Here is my ControllerAdmin/Forum.php:
PHP:
class Goss_RoleplaySystem_ControllerAdmin_Forum extends XFCP_Goss_RoleplaySystem_ControllerAdmin_Forum
{
    public function actionSave()
    {
        $response = parent::actionSave();
        if($response->redirectType == XenForo_ControllerResponse_Redirect::SUCCESS)
        {
            $data = $this->_input->filter(array(
                'forum_type' => XenForo_Input::STRING
            ));
           
            $dw = XenForo_DataWriter::create('Goss_RoleplaySystem_DataWriter_Forum');
           
            $dw->bulkSet($data);
            $dw->save();
        }
       
        return $response;
    }
}

And here is my Datawriter:
PHP:
class Goss_RoleplaySystem_DataWriter_Forum extends XFCP_Goss_RoleplaySystem_DataWriter_Forum
{
    protected function _getFields()
    {
        $fields = parent::_getFields();
       
        $fields + array('xf_forum' => array(
            'type' => self::TYPE_STRING,
            'default' => NULL
        ));
       
        return $fields;
    }
}

What am I doing wrong?
 
Here is an example I use to save added options (I will point out I use a separate table to store the forums new options). Hopefully it helps, :)

PHP:
<?php

class Icewind_IcewindDale_ControllerAdmin_Forum extends XFCP_Icewind_IcewindDale_ControllerAdmin_Forum
{
    public function actionSave()
    {
        $response = parent::actionSave();
        if ($response->redirectType == XenForo_ControllerResponse_Redirect::SUCCESS)
        {
            $xpmData = $this->_input->filter(array(
                'node_id' => XenForo_Input::UINT,
                'xp_mod' => XenForo_Input::UINT,
                'gold_mod' => XenForo_Input::UINT,
                'encounter' => XenForo_Input::UINT,
                'trap' => XenForo_Input::UINT,
                'treasure' => XenForo_Input::UINT
            ));
         
            if (empty($xpmData['node_id']))
            {
                $xpmData['node_id'] = $this->_getLastSavedForumNodeId();
            }
 
            $dw = XenForo_DataWriter::create('Icewind_IcewindDale_DataWriter_Forum');
         
            $xpmOptionsModel = $this->_getForumXpModOptionsModel();
            if ($xpmOptionsModel->verifyForumXpModId($xpmData['node_id']))
            {
                $dw->setExistingData($xpmData['node_id']);
            }

            $dw->bulkSet($xpmData);
            $dw->save();
         
            $xpmOptionsModel->rebuildForumXpModCache();
        }
        return $response;
    }
 
    public function actionDelete()
    {
        $parent = parent::actionDelete();
        $this->_getForumXpModOptionsModel()->removeDeletedForums();

        return $parent;
    }
 
    public function actionValidateField()
    {
        $parentResponse = parent::actionValidateField();

        $fieldName = $this->_input->filterSingle('name', XenForo_Input::STRING);
        if ($fieldName == 'xp_mod' OR $fieldName == 'gold_mod')
        {
            $xpmResponse = $this->_validateField('Icewind_IcewindDale_DataWriter_Forum', array(
                'existingDataKey' => $this->_input->filterSingle('node_id', XenForo_Input::UINT))
            );
            return $xpmResponse;
        }
        else
        {
            return $parentResponse;
        }
    }
 
    protected function _getForumXpModOptionsModel()
    {
        return $this->getModelFromCache('Icewind_IcewindDale_Model_Options');
    }
 
    protected function _getLastSavedForumNodeId()
    {
        return XenForo_Application::get('db')->fetchOne('
            SELECT node_id
            FROM xf_forum
            ORDER BY node_id
            DESC'
        );
    }
}
 
Last edited:
Thanks for that! I updated my code to pull in the node_id and make sure that exists. Also made some updates to the DataWriter since I was missing the _getExistingData function which seems kind of important. Unfortunately, it's still not saving. :(

ControllerAdmin/Forum.php
PHP:
<?php

class Goss_RoleplaySystem_ControllerAdmin_Forum extends XFCP_Goss_RoleplaySystem_ControllerAdmin_Forum
{
    public function actionSave()
    {
        $response = parent::actionSave();
       
        if($response->redirectType == XenForo_ControllerResponse_Redirect::SUCCESS)
        {
            $data = $this->_input->filter(array(
                'node_id' => XenForo_Input::UINT,
                'forum_type' => XenForo_Input::STRING
            ));
           
            if (empty($data['node_id']))
            {
                $data['node_id'] = $this->_getLastSavedForumNodeId();
            }
            $dw = XenForo_DataWriter::create('Goss_RoleplaySystem_DataWriter_Forum');
           
            $dw->bulkSet($data);
            $dw->save();
        }
       
        return $response;
    }
   
    protected function _getLastSavedForumNodeId()
    {
        return XenForo_Application::get('db')->fetchOne('
            SELECT node_id
            FROM xf_forum
            ORDER BY node_id
            DESC
        ');
    }
}

?>

DataWriter/Forum.php
PHP:
<?php

class Goss_RoleplaySystem_DataWriter_Forum extends XFCP_Goss_RoleplaySystem_DataWriter_Forum
{
    protected function _getFields()
    {
        $fields = parent::_getFields();
       
        $fields + array('xf_forum' => array(
            'type' => self::TYPE_STRING,
            'default' => NULL
        ));
       
        return $fields;
    }
   
    protected function _getExistingData($data)
    {
        if (!$id = $this->_getExistingPrimaryKey($data, 'node_id'))
        {
            return false;
        }
       
        $forum = $this->getModelFromCache('XenForo_Model_Forum')->getForumById($nodeId);
       
        if (!$forum)
        {
            return false;
        }
       
        return $this->getTablesDataFromArray($forum);
    }
}

?>
 
Try this for the ControllerAdmin/Forum.php

PHP:
<?php

class Goss_RoleplaySystem_ControllerAdmin_Forum extends XFCP_Goss_RoleplaySystem_ControllerAdmin_Forum
{
    public function actionSave()
    {
        $response = parent::actionSave();
      
        if($response->redirectType == XenForo_ControllerResponse_Redirect::SUCCESS)
        {
            $writerData = $this->_input->filter(array(
                'forum_type' => XenForo_Input::STRING
            ));

            if(empty($writerData['node_id']))
            {
                $writerData['node_id'] = $this->_getLastSavedForumNodeId();
            }

            $writer = $this->_getNodeDataWriter();

            if($writerData['node_id'])
            {
                $writer->setExistingData($writerData['node_id']);
            }

            $writer->bulkSet($writerData);
            $writer->save();
        }
      
        return $response;
    }

    protected function _getLastSavedForumNodeId()
    {
        return XenForo_Application::get('db')->fetchOne('
            SELECT node_id
            FROM xf_forum
            ORDER BY node_id
            DESC
        ');
    }
}

Whereas for DataWriter/Forum.php try this:

PHP:
<?php

phpclass Goss_RoleplaySystem_DataWriter_Forum extends XFCP_Goss_RoleplaySystem_DataWriter_Forum
{
    protected function _getFields()
    {
        $fields = parent::_getFields();
       
        $fields['xf_forum']['forum_type'] = array('type' => self::TYPE_STRING);
       
        return $fields;
    }
}


Hope it helps.
 
Thank you! I've tried that, and also fiddled around with the code a bit, but it's still not saving.

I did manage to find a typo in the html for the radio buttons I created, but fixing that also gave me no results. This is what I have there now:

Code:
<xen:radiounit label="{xen:phrase rps_forum_type}:" name="forum_type" value="{$forum.forum_type}">
    <xen:option value="roleplay">{xen:phrase forum_type_roleplay}</xen:option>
    <xen:option value="none">{xen:phrase none}</xen:option>
</xen:radiounit>

I can upload a copy of my add-on if that might help.

Edit: Ok, so I know it's definitely not an issue with my database field. If I manually add in the value to the xf_forum.forum_type field, the radio button shows as properly selected. I just can't save/update it to a new value.
 
Last edited:
Top Bottom