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

Add-On Install PHP Callback - Automatically create custom user field

Discussion in 'XenForo Development Discussions' started by Chris D, Jun 17, 2012.

  1. Chris D

    Chris D XenForo Developer Staff Member

    Got an idea for an add-on which needs a custom user field (preference).

    Has anyone got advice on achieving this? I assume it needs a callback for the installer process.

    I've seen an add-on try to achieve this before: http://xenforo.com/community/threads/steam-identity-service.9819/

    But I believe for some reason the creation of this user field failed so I just want to make sure a) it's possible and b) it works.
     
  2. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

  3. Chris D

    Chris D XenForo Developer Staff Member

    Thanks Jake.


    Got everything working, but stumbling at the field choices.

    Two simple choices:

    id Value
    show Show
    hide Hide

    If I create the field manually, PHP My Admin stores it as a BLOB which I can download as a .bin file. The contents of which is:

    Code:
    a:2:{s:4:"show";s:4:"Show";s:4:"hide";s:4:"Hide";}
    In my Install class:

    PHP:
    $dw->set('field_choices''a:2:{s:4:"show";s:4:"Show";s:4:"hide";s:4:"Hide";}');
    But installing I get the error:

    Error
    Please enter at least one choice.


    Not sure what the best way is to achieve that.
     
  4. Chris D

    Chris D XenForo Developer Staff Member

    Ok, I've done this differently.

    Based on this tutorial by Furhman: http://xenforo.com/community/resources/how-to-read-and-write-into-the-database-with-a-page.328/

    I just do it with a query:

    Code:
    'createTable' => "INSERT INTO `xf_user_field` (`field_id`, `display_group`, `display_order`, `field_type`, `field_choices`, `match_type`, `match_regex`, `match_callback_class`, `match_callback_method`, `max_length`, `required`, `show_registration`, `user_editable`, `viewable_profile`, `viewable_message`, `display_template`) VALUES ('myTable', 'preferences', '1', 'radio', 0x613a323a7b733a343a2268696465223b733a343a2248696465223b733a343a2273686f77223b733a343a2253686f77223b7d, 'none', '', '', '', '0', '1', '1', 'yes', '0', '0', '')",
    Really would like to know how to get the datawriter to work with that data, though.
     
    yavuz likes this.
  5. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    Try using $dw->setFieldChoices($choices)

    $choices needs to be an array of choices.

    array(
    'key1' => 'text1',
    'key2' => 'text2',
    'key3' => 'text3'
    )
     
  6. Chris D

    Chris D XenForo Developer Staff Member

    I will try that, thank you.

    I looked at that code yesterday. I think I'm guilty of over complicating things. I looked at it and thought - how on earth do I make that work?

    Turns out it looks very simple :D

    I'll let you know how I get on.
     
  7. Chris D

    Chris D XenForo Developer Staff Member

    Thank you Jake,

    Works great. So easy, I should have been able to work it out. Thank you. (y)
     
  8. Chris D

    Chris D XenForo Developer Staff Member

    Next challenge.

    My select options are basically "Show" and "Hide". Default should be "hide".

    However, if you go into the user's Preferences neither option is selected.

    Yet, if you do {xen:raw $user.customFields.myField} in the template, the output is "show".

    Doesn't matter what order the array is in, the field always outputs "show" (unless someone specifically changes it to "hide").

    If anything, as by default it isn't selected or specified in xf_user_field_value then I would expect it to output ' '

    Any ideas?
     
    Steve F likes this.
  9. Chris D

    Chris D XenForo Developer Staff Member

    Should I achieve this by running a query durung install that sets it to hide for all users? How would big boards handle a query that affects tens of thousands of rows?
     
  10. Chris D

    Chris D XenForo Developer Staff Member


    Turns out this may be a caching issue :oops:

    I was testing with users who had previously set the option. I'd then uninstalled the add-on which removes the rows in xf_user_field_value where the option is set but the previous choices are still cached. So reinstalling the add-on gave the impression that the incorrect option was set by default.

    I rebuilt the user caches in Admin CP and now, as no users have the preference to "show" the information is hidden appropriately.

    This means I was completely wrong, and actually any custom user fields are empty by default, like they should be :)

    Thank you for your help, Jake, as always.

    Trying to avoid contacting you directly by PM so I'm not bothering you... and it's still you that helps me. Genius, thank you. (y)
     
  11. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    That makes sense. There is no option to set a default selection for profile fields so I was like :confused:.
     
    Yorick likes this.
  12. Chris D

    Chris D XenForo Developer Staff Member

    Steve F and yavuz like this.

Share This Page