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

Add-On Installing Profile Fields?

Jake Bunce

XenForo moderator
Staff member
#2
Unlike some objects in the Admin CP, user fields can't be attributed to an addon. You will need to write some install code for your addon to create the field. Refer to this controller for code to save a new field:

XenForo_ControllerAdmin_UserField::actionSave
 

Jake Bunce

XenForo moderator
Staff member
#4
You can specify an install class when creating an addon:

Admin CP -> Development -> Create Add-on

But then you need to write the code to create the profile field. You can use that controller code for reference. Basically you need to call on the datawriter, set the field values, pass those values to the datawriter, and save the changes. The controller from my previous post is a good example to follow.
 

Jon W

Well-known member
#8
I see that I can import and export fields. Excellent, but I am not creating thread fields. Is that custom user field addon only for thread fields?
This add-on does thread fields and user fields.

With my add-on installed, you can attribute user fields to an add-on, and then they will be exported with the add-on. Also, you should be able to export user fields on their own as well. Both will require the end user to have the add-on installed in order to import them.
 

Jon W

Well-known member
#10
In your add-ons installer code you could just spawn a profile field model, check if they already exist then spawn a data writer and create them
I think that is what Jake suggested. But if you don't want to do that, you can use my add-on and avoid writing any code at all.
 

Jon W

Well-known member
#13
I think that is what Jake suggested. But if you don't want to do that, you can use my add-on and avoid writing any code at all.
Install custom fields, edit your user field and there should be a drop down menu to select an add-on. You may need to have debug mode enabled.
 

DRE

Well-known member
#16
Yeah I was able to make my first addon with it. I also don't like having to use TMS. Floris made a resource explaining how to create an addon that inserts a template edit. That's my next step but I would either have to track down Floris and ask if he saved it or beg someone to teach me.
 

Jon W

Well-known member
#17
There's this one for template hooks:
http://xenforo.com/community/threads/how-to-use-template-hooks.13167

But you probably want template post render. It's not that much different, it just searches on the template name instead of the hook name. If you follow the video but make the appropriate changes (i.e., selecting template_post_render instead of template_hook), you'll pretty much be there. You then just need to code your replacement, which could be not too difficult like:
PHP:
$content .= 'Some code I want to add at the end';
to more complicated things like:
PHP:
$pattern = 'Some code that already exists';
$replacement = 'Some code I want to put before it';
$content = str_replace($pattern, $replacement . $pattern, $content;
to the even more complicated things like:
PHP:
$pattern = '#(Some reg(?:ular )?exp(?:ression) for the code )you want( to replace)#';
$replacement = '${1}DRE wants${2}';
$content = preg_replace($pattern, $replacement . $pattern, $content;
Just make sure to use $content instead of $contents.

Can't believe there isn't a guide. If it wasn't for TMS, there would probably be plenty of guides for it.
 

DRE

Well-known member
#18
Yeah cause vbulletin had a ton of guides. I had only just started making addons for vbulletin using their guides before i switched over to xenforo.