1. 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

Discussion in 'XenForo Development Discussions' started by wasif, Nov 3, 2014.

  1. wasif

    wasif Member

    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.
     
  2. Mr. Goodie2Shoes

    Mr. Goodie2Shoes Well-Known Member

    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. wasif

    wasif Member

    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 ?
     
  4. Mr. Goodie2Shoes

    Mr. Goodie2Shoes Well-Known Member

  5. wasif

    wasif Member

    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.
     

Share This Page