• 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

Chris D

XenForo developer
Staff member
#3
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.
 

Chris D

XenForo developer
Staff member
#4
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.
 

Jake Bunce

XenForo moderator
Staff member
#5
Try using $dw->setFieldChoices($choices)

$choices needs to be an array of choices.

array(
'key1' => 'text1',
'key2' => 'text2',
'key3' => 'text3'
)
 

Chris D

XenForo developer
Staff member
#6
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.
 

Chris D

XenForo developer
Staff member
#8
Try using $dw->setFieldChoices($choices)

$choices needs to be an array of choices.

array(
'key1' => 'text1',
'key2' => 'text2',
'key3' => 'text3'
)
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?
 

Chris D

XenForo developer
Staff member
#9
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?
 

Chris D

XenForo developer
Staff member
#10
Try using $dw->setFieldChoices($choices)

$choices needs to be an array of choices.

array(
'key1' => 'text1',
'key2' => 'text2',
'key3' => 'text3'
)
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?

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)