XF 2.1 Custom UserOption in xf_user_option not saving

Wutime

Well-known member
I'm hoping someone with some more experience creating a custom option in the existing xf_user_option table can help me.

My new database field was added through the schema for xf_user_option and shows in the database with the correct structure and appears in the [account/preferences] properly through a template modification.

However, when saving the page the option is not saved and resets to default. I've checked the database and the option value never changes.

I get no errors. I'm wondering if it's because it's a custom UserOption field, and I need to explicitly use a setter for save (neither of which I see in the existing XF\Entity\UserOption.php

Setup.php (this properly adds the new field to the existing xf_user_option table

Code:
<?php

namespace Name\Addon;

use XF\AddOn\AbstractSetup;
use XF\AddOn\StepRunnerInstallTrait;
use XF\AddOn\StepRunnerUninstallTrait;
use XF\AddOn\StepRunnerUpgradeTrait;
use XF\Db\Schema\Alter;

class Setup extends AbstractSetup
{
    use StepRunnerInstallTrait;
    use StepRunnerUpgradeTrait;
    use StepRunnerUninstallTrait;

    public function installStep1()
    {
        $this->schemaManager()->alterTable('xf_user_option', function(Alter $table)
        {
            $table->addColumn('name_addon_enable', 'tinyint')->setDefault(0)->after('user_id');
        });
    }
}

\Addon\Name\XF\Entity\UserOption.php (added through class extension)
Code:
<?php

namespace Addon\Name\XF\Entity;
use XF\Mvc\Entity\Structure;

class UserOption extends XFCP_UserOption
{
    public static function getStructure(Structure $structure)
    {
        $parent = parent::getStructure($structure);
        $parent->columns['addon_name_enable'] = ['type' => self::BOOL, 'default' => false];
        return $parent;
    }
}

Note, if I change the value in the DB directly, the option value is correctly updated on the account preferences page on load. It's only when I change it and click save, it never actually updates the database and when the page refreshes it goes back to whatever the setting in the database was originally.

account_preferences template customization (inserted to account_preferences)
Code:
<xf:checkboxrow label="{{ phrase('addon_name_accessibility_title') }}">
    <xf:option name="option[addon_name_enable]" checked="{$xf.visitor.Option.addon_name_enable}"
               label="{{ phrase('addon_name_option_title') }}" />
</xf:checkboxrow>

Finally, this is the HTML code when inspected on the account preferences page for the custom xf_user_option:
Code:
<input type="checkbox" name="option[addon_name_enable]" value="1">

I think I must be missing something that actually puts my new xf_user_option field into the "save" routine. I would have assumed adding it to the structure would have been enough.

Where do I add it to the schema?
 
Last edited by a moderator:
I think you've been through the correct steps, but the only obvious issue I can see is your database field seems to be called name_addon_enable but your code seems to reference addon_name_enable.
 
Actually, that's probably just a typo.

More significantly:
I think I must be missing something that actually puts my new xf_user_option field into the "save" routine. I would have assumed adding it to the structure would have been enough.
Yes, you're probably right. You would need to extend the Account controller and the preferencesSaveProcess method.
 
I think you've been through the correct steps, but the only obvious issue I can see is your database field seems to be called name_addon_enable but your code seems to reference addon_name_enable.

I actually renamed everything in hopes of making it more clear for readers - so yes, a typo, but the non-consequential kind :)
 
Back
Top Bottom