XF 2.1 Add new custom thread field

shqawe

Member
Licensed customer
I'm trying to add user group as thread field which user who has permission can choose the group. My problem is how can i insert user group ids to database

This is my code


HTML:
<xf:selectrow name="usergroup[]" value="{$thread.usergroup}" label="{{ phrase('choose_ug') }}" multiple="multiple">
    <xf:option value="0">{{ phrase('All') }}</xf:option>
    <xf:foreach loop="$thread.getUserGrups()" value="$userg">
            <xf:option value="{$userg.user_group_id}">{$userg.title}</xf:option>
    </xf:foreach>
</xf:selectrow>

Thread controller

PHP:
protected function setupThreadEdit(\XF\Entity\Thread $thread)
    {
      
        $editor = parent::setupThreadEdit($thread);
        
        $canChooseUserGroups = $thread->canChooseUserGroups();
        if ($canChooseUserGroups)
        {
            $editor->setCanChoseGroups($this->filter('usergroup', 'int'));
        }

        return $editor;
    }

Forum Controller

PHP:
protected function setupThreadCreate(\XF\Entity\Forum $forum)
    {
        
        $creator = parent::setupThreadCreate($forum);

        $creator->setCanChoseGroups($this->filter('usergroup', 'int'));
      
        return $creator;
    }

Service Editor

PHP:
protected $userGroup;

    public function setCanChoseGroups($userGroup)
    {
        $this->userGroup = $userGroup;
    }

    protected function _save()
    {
        $thread = parent::_save();

        if ($this->userGroup !== null && $thread->discussion_state == 'visible')
        {

            
            if (!$this->userGroup)
            {
                    $thread->fastUpdate('usergroup', true);
            }
            else
            {
                    $thread->fastUpdate('usergroup', false);
            }
        }

        return $thread;
    }

Service Creator

PHP:
protected $userGroup;

    public function setCanChoseGroups($userGroup)
    {
        $this->userGroup = $userGroup;
    }

    protected function _save()
    {
        $thread = parent::_save();

        if ($this->userGroup !== null)
        {
            $thread->fastUpdate('usergroup', true);         
        }

        return $thread;
    }

Event Listener

PHP:
$structure->columns['usergroup'] = ['type' => Entity::UINT, 'default' => 0];


I hope someone can help.
 
Back
Top Bottom