How to make an add-on to add a custom user field?

Dadparvar

Well-known member
Hi,

I created an add-on. But it need a custom user field to be added in each site that add-on is going to be installed.

How can I do it? I don't see any option to associated a custom field to any add-on.

Regards
 
Custom User Fields can't be associated with an add-on, sadly. Though you can create, modify and delete them through your add-on using the DataWriter.

Here is a short example from my Gamer Profiles add-on.
PHP:
    public static function install($existingAddOn)
    {
        if (XenForo_Application::$versionId < 1040800)
        {
            // note: this can't be phrased
            throw new XenForo_Exception('This add-on requires XenForo 1.4.3 or higher.', true);
        }
       
        // Checks if add-on is already present if it is skip creating the Custom User Field
        if (!$existingAddOn)
        {


            $dw = XenForo_DataWriter::create('XenForo_DataWriter_UserField');
            //Twitch
            $dw->set('field_id', 'twitchUserProfile');
            $dw->set('display_group', 'contact');
            $dw->set('display_order', 5);
            $dw->set('field_type', 'textbox');
            $dw->set('required', 0);
            $dw->set('show_registration', 0);
            $dw->set('user_editable', 'yes');
            $dw->set('viewable_profile', '1');
            $dw->set('viewable_message', '0');
            $dw->set('max_length', 25);
            $dw->setExtraData(XenForo_DataWriter_UserField::DATA_TITLE, 'Twitch');
            $dw->setExtraData(XenForo_DataWriter_UserField::DATA_DESCRIPTION, 'Enter your Twitch user name here to link to your channel page');
            $dw->save();
        }
       
        // Deletes Custom User Field when add-on is uninstalled
        public static function uninstall()
        {
            $dw = XenForo_DataWriter::create('XenForo_DataWriter_UserField');
            $dw->setExistingData('twitchUserProfile');
            $dw->delete();
        }
    }
 
Custom User Fields can't be associated with an add-on, sadly. Though you can create, modify and delete them through your add-on using the DataWriter.

Here is a short example from my Gamer Profiles add-on.
PHP:
    public static function install($existingAddOn)
    {
        if (XenForo_Application::$versionId < 1040800)
        {
            // note: this can't be phrased
            throw new XenForo_Exception('This add-on requires XenForo 1.4.3 or higher.', true);
        }
      
        // Checks if add-on is already present if it is skip creating the Custom User Field
        if (!$existingAddOn)
        {


            $dw = XenForo_DataWriter::create('XenForo_DataWriter_UserField');
            //Twitch
            $dw->set('field_id', 'twitchUserProfile');
            $dw->set('display_group', 'contact');
            $dw->set('display_order', 5);
            $dw->set('field_type', 'textbox');
            $dw->set('required', 0);
            $dw->set('show_registration', 0);
            $dw->set('user_editable', 'yes');
            $dw->set('viewable_profile', '1');
            $dw->set('viewable_message', '0');
            $dw->set('max_length', 25);
            $dw->setExtraData(XenForo_DataWriter_UserField::DATA_TITLE, 'Twitch');
            $dw->setExtraData(XenForo_DataWriter_UserField::DATA_DESCRIPTION, 'Enter your Twitch user name here to link to your channel page');
            $dw->save();
        }
      
        // Deletes Custom User Field when add-on is uninstalled
        public static function uninstall()
        {
            $dw = XenForo_DataWriter::create('XenForo_DataWriter_UserField');
            $dw->setExistingData('twitchUserProfile');
            $dw->delete();
        }
    }
Thanks.

Helped a lot.

It seems this codes doesn't add user field while upgrading add-on? (if in previous installed version still those fields are not created yet). Right? Any idea?
 
Thanks.

Helped a lot.

It seems this codes doesn't add user field while upgrading add-on? (if in previous installed version still those fields are not created yet). Right? Any idea?

Code:
    public static function install($existingAddOn)
    {
        $version = isset($existingAddOn['version_id']) ? $existingAddOn['version_id'] : 0;
        if ($version < 10000)
....

Where $version is being compared to the add-on's version_id attribute.
 
What would be the correct way to achieve this in XF2?
I have tried too many ways to list...
Thank You!
 
Back
Top Bottom