Adding a custom field to personal details in UCP

xpl0iter

Active member
I am working on an addon in which I need to add a text box to personal details in UCP.
I know adding is not the hard part but saving it is. I don't have any idea on how to save it to the database, can some one please help me on this?
 
If you're releasing an add-on that requires a custom user profile field, you will need to run a couple of database queries during the install of the add-on.

Here's an example function from one of my add-ons:

PHP:
	public static function installer($existingAddOn)
	{

		if (!$existingAddOn)
		{

			$dw = XenForo_DataWriter::create('XenForo_DataWriter_UserField');
			$dw->set('field_id', 'displayEmailAddress');
			$dw->set('display_group', 'preferences');
			$dw->set('display_order', 1);
			$dw->set('field_type', 'select');
			$dw->set('required', 1);
			$dw->set('show_registration', 1);
			$dw->set('user_editable', 'yes');
			$dw->set('viewable_profile', '0');

			$choices = array(
				'hide' => 'Hide',
				'show' => 'Show'
			);

			$dw->setFieldChoices($choices);

			$dw->save();

		}

	}

Hopefully most of the options above should be self explanatory in relation to the options you get in the Admin CP.

Also, you may not need field choices if you're just doing a text box.
 
So the following would be my code to be run while installation of the addon?
Also so when the user want to edit it next time, will the custom profile field get saved automatically?
Code:
public static function installer($existingAddOn)
    {

        if (!$existingAddOn)
        {

            $dw = XenForo_DataWriter::create('XenForo_DataWriter_UserField');
            $dw->set('field_id', 'URL_myaddon');
            $dw->set('display_group', 'preferences');
            $dw->set('display_order', 1);
            $dw->set('field_type', '[B]textbox[/B]');
            $dw->set('required', 1);
            $dw->set('show_registration', 1);
            $dw->set('user_editable', 'yes');
            $dw->set('viewable_profile', '0');

            
            $dw->save();

        }

    }
 
I want to add the custom user field by using my plugin, jist like the way we add it manually by going to ACP->Users->Custom User Fields
 
That's what the code above does :)
This might seems stupid as my experience with Xenforo adgon creation is really limited!
PHP:
<?php
class WGBB_tpoh_installer
{
    public static function tpoh_installr()
    {
        $dw = XenForo_DataWriter::create('XenForo_DataWriter_UserField');
        $dw->set('field_id', 'URL"');
        $dw->set('display_group', 'personal');
        $dw->set('display_order', 10);
        $dw->set('field_type', 'textbox');
        $dw->set('required', 1);
        $dw->set('show_registration', 1);
        $dw->set('user_editable', 'yes');
        $dw->set('viewable_profile', '0');
        $dw->save();
    }
}
?>
This my installer.php file and I have given this in the addon settings page under installation code and method!
Is there something I am missing?
Also do I need to write datawriter for this code to work?

Btw thanks for helping me Chris!
 
It just uses the existing DataWriter.

Are you getting an error? The above should work.
 
You've added the code to an Installation call back. This only runs when you either Install or Upgrade the add-on.

Export the XML and upgrade your add-on in the Admin CP with that XML. It will run the installer.
 
Set an uninstall method :)

This is an example from one of my add-ons again:
PHP:
    public static function uninstaller()
    {
        $db = XenForo_Application::get('db');
        $db->query("DELETE FROM `xf_user_field` WHERE `xf_user_field`.`field_id` = 'displayEmailAddress'");
        $db->query("DELETE FROM `xf_user_field_value` WHERE `xf_user_field_value`.`field_id` = 'displayEmailAddress'");
    }
 
Maybe someone needs to create an add-on where, once you've set the userfield, you can click 'PHP' link and get the PHP code required to insert the custom user field.

Makes writing it easier!

But, I believe there was a suggestion to associate userfields with add-ons.
 
Yeah I thought about creating something similar once but ultimately it should most definitely be something included in the core. We should be able to create our user fields, associate them with an add-on and export them in the XML file.

Hopefully it will get entertained when things are better :)
 
If you're releasing an add-on that requires a custom user profile field, you will need to run a couple of database queries during the install of the add-on.

Here's an example function from one of my add-ons:

PHP:
    public static function installer($existingAddOn)
    {

        if (!$existingAddOn)
        {

            $dw = XenForo_DataWriter::create('XenForo_DataWriter_UserField');
            $dw->set('field_id', 'displayEmailAddress');
            $dw->set('display_group', 'preferences');
            $dw->set('display_order', 1);
            $dw->set('field_type', 'select');
            $dw->set('required', 1);
            $dw->set('show_registration', 1);
            $dw->set('user_editable', 'yes');
            $dw->set('viewable_profile', '0');

            $choices = array(
                'hide' => 'Hide',
                'show' => 'Show'
            );

            $dw->setFieldChoices($choices);

            $dw->save();

        }

    }

Hopefully most of the options above should be self explanatory in relation to the options you get in the Admin CP.

Also, you may not need field choices if you're just doing a text box.


Looking through some code examples ( XenForo/DataWriter/UserField.php) I'm not seeing where it inputs a 'title' or 'description'. Am I just missing the obvious?

Adding
$dw->set('title', 'MyTitle');

Just errors with "The field 'title' was not recognised." when trying to run the install. Same with 'description'..Any idea?

Also, would it be bad to duplicate the above code to install 4 - 5 fields at once?
 
Last edited:
Set an uninstall method :)

This is an example from one of my add-ons again:
PHP:
    public static function uninstaller()
    {
        $db = XenForo_Application::get('db');
        $db->query("DELETE FROM `xf_user_field` WHERE `xf_user_field`.`field_id` = 'displayEmailAddress'");
        $db->query("DELETE FROM `xf_user_field_value` WHERE `xf_user_field_value`.`field_id` = 'displayEmailAddress'");
    }

Using this in my add-on but for whatever reason not all data is removed for the custom user field. Example, I uninstall the add-on it removes the custom fields from ACP and 'Contact Details' but if I install the add-on again the info for the old field is still somewhere that I can't find because it shows the previous data I had entered before uninstalling the add-on, and the actual field in Contact Details is empty. Am I missing something that should be removed as well?

Also tried this in my uninstall function with the same results.

PHP:
$dw = XenForo_DataWriter::create('XenForo_DataWriter_UserField');
$dw->setExistingData('steamUserProfile');
$dw->delete();
 
Set an uninstall method :)

This is an example from one of my add-ons again:
PHP:
    public static function uninstaller()
    {
        $db = XenForo_Application::get('db');
        $db->query("DELETE FROM `xf_user_field` WHERE `xf_user_field`.`field_id` = 'displayEmailAddress'");
        $db->query("DELETE FROM `xf_user_field_value` WHERE `xf_user_field_value`.`field_id` = 'displayEmailAddress'");
    }
Yeah that's a bad way to do it.

The data writer method is better. But I think this is expected.

I think the same would happen with fields that are created manually and then deleted manually in the admin CP too. Custom field values are cached so you would need some sort of rebuild to truly remove the cached values.

I suspect the User rebuild does it but I don't think that's something an add-on developer would need to worry about.

I'm only guessing at the moment though. I'll look at the code later.
 
Top Bottom