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

Adding custom thread field data into database

#1
Hi,

I am trying to add an additional field data into the database. For this I extend the class "Xenforo_DataWriter_Discussion_Thread". Now I am trying to get my field data and for that I am trying to access "_input" but I am not able to do that.

Please help me how can I get that ?

Extended Class: Xenforo_DataWriter_Discussion_Thread (Extended it using proxy system.)
Method (Overriding): _discussionPreSave()

Thanks.
 

Mr. Goodie2Shoes

Well-known member
#2
The '_input' property is only available to controller classes. What you can do is fetch the values in a more 'php' way. e.g.
PHP:
protected function _discussionPreSave()
{
    if (isset($_REQUEST['{field_name}']))
    {
        $this->set('{field_name}', XenForo_Input::rawFilter($_REQUEST['{field_name}'], XenForo_Input::{filter_type}));
    }
}
Note that you might not get expected values by this method if the field is a checkbox.
 
#3
Thanks a lot.Its working now. ;)

But I was following a plugin by "waindigo custom fields."

And they used the same method "discussion_preSave()" and extend the same class as I did.

PHP:
protected function _discussionPreSave()
    {
        $node = $this->_getForumData();

        if (isset($GLOBALS['XenForo_ControllerPublic_Forum'])) {
            /* @var $controller XenForo_ControllerPublic_Forum */
            $controller = $GLOBALS['XenForo_ControllerPublic_Forum'];

            $fieldValues = array();
            if (isset($node['custom_fields']) && $node['custom_fields']) {
                $fieldValues = unserialize($node['custom_fields']);
            }

            $customFields = $controller->getInput()->filterSingle('custom_fields', XenForo_Input::ARRAY_SIMPLE);
            $customFieldsShown = $controller->getInput()->filterSingle('custom_fields_shown', XenForo_Input::STRING,
                array(
                    'array' => true
                ));

            foreach ($fieldValues as $fieldName => $fieldValue) {
                if (!in_array($fieldName, $customFieldsShown)) {
                    $customFieldsShown[] = $fieldName;
                    $customFields[$fieldName] = $fieldValue;
                }
            }

            $this->setCustomFields($customFields, $customFieldsShown);
        }

        if (isset($GLOBALS['XenForo_ControllerPublic_Thread'])) {
            /* @var $controller XenForo_ControllerPublic_Thread */
            $controller = $GLOBALS['XenForo_ControllerPublic_Thread'];

            if (strtolower($controller->getRouteMatch()->getAction()) == 'save') {
                $customFields = $controller->getInput()->filterSingle('custom_fields', XenForo_Input::ARRAY_SIMPLE);
                $customFieldsShown = $controller->getInput()->filterSingle('custom_fields_shown', XenForo_Input::STRING,
                    array(
                        'array' => true
                    ));

                $this->setCustomFields($customFields, $customFieldsShown);
            }
        }

        parent::_discussionPreSave();
    }
But when I try check $globals['XenForo_ControllerPublic_Forum'] or $global['XenForo_ControllerPublic_Thread'] it gives me NULL.
So should I register these controller somewhere or what ? What I am missing here ?
 
#5
Thanks a lot. :)

I will surely do two things now,

1. Will check how to manually set the controller using controller_pre_dispatch listener.
2. And will study the product cmf_core that you mentioned.

Again thanks a lot.