1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Adding a custom field to personal details in UCP

Discussion in 'XenForo Development Discussions' started by xpl0iter, Feb 1, 2013.

  1. xpl0iter

    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?
     
  2. Chris D

    Chris D XenForo Developer Staff Member

    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.
     
    Cyb3r, Mirovinger and xpl0iter like this.
  3. xpl0iter

    xpl0iter Active Member

    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();
    
            }
    
        }
    
     
  4. xpl0iter

    xpl0iter Active Member

    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
     
  5. Chris D

    Chris D XenForo Developer Staff Member

    That's what the code above does :)
     
  6. xpl0iter

    xpl0iter Active Member

    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!
     
  7. Chris D

    Chris D XenForo Developer Staff Member

    It just uses the existing DataWriter.

    Are you getting an error? The above should work.
     
  8. xpl0iter

    xpl0iter Active Member

    I don't see anything under the personal menu on my profile settings nor do I see anything on the database ("user_fields").
    And no error comes!
     
  9. Chris D

    Chris D XenForo Developer Staff Member

    You will need to install or upgrade the add-on first. Have you done that?
     
  10. xpl0iter

    xpl0iter Active Member

    Disabling and enabling the addon will do or do I have to delete the addon and reinstall it again?
    I have done the disabling/enabling already!
     
  11. Chris D

    Chris D XenForo Developer Staff Member

    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.
     
  12. xpl0iter

    xpl0iter Active Member

    Thanks a lot Chris. It worked!
    I thought disabling and enabling will work.

    Thanks a lot again! :D
     
  13. xpl0iter

    xpl0iter Active Member

    One more thing, how do I delete this user field once the addon is uninstalled?
     
  14. Chris D

    Chris D XenForo Developer Staff Member

    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'");
        }
     
  15. James

    James Well-Known Member

    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.
     
    dwdmadmac likes this.
  16. Chris D

    Chris D XenForo Developer Staff Member

    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 :)
     
    Steve F, dwdmadmac and James like this.
  17. Steve F

    Steve F Well-Known Member


    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: Jul 3, 2014
  18. Steve F

    Steve F Well-Known Member

    Last edited: Jul 3, 2014
  19. Steve F

    Steve F Well-Known Member

    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();
     
  20. Chris D

    Chris D XenForo Developer Staff Member

    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.
     

Share This Page