What is the proper way to add custom fields to a user profile hash?
-
Ok, So I have figured out the template system, learned from some other plugins a few things. Able to modify broken ones to make work again etc.
Now I have a plugin that on their registration adds a field to their main user mongo objectid. Question is where can I find some useful information on how to pull that said information from the database to display it in html on the template.
I know base values can be done such as username etc with {username} obviously I need the plugin to "whitelist" or add this new value to be able to use it in templates too.
I'm self taught programmer, learn from things i see. Examples are super helpful to me.
I assume id have to do something like:
Require User src User.getUserField(data.uid, 'NAMEOFID', function (err, NAMEOFIDDATA) { //What to do here to push it to template field? }
I just find it hard to find accurate documentation and descriptions of nodebb plugin making maybe im looking in the wrong areas but seems like its hidden easter eggs.
But yeah, a bit more detail.
db.objects.find({_key: "user:4"}) { "_id" : ObjectId("5ce1e1fa549c71c7acd236cb"), "_key" : "user:4", "valueId" : "hello", "returnTo" : "/" }
(Obviously theres more in that object id i just removed it) - however I want to display valueid's value (hello) to a tpl. If this makes it easier to understand.
-
@Joykiller Starting with latest master this is much easier now, all you have to do is use the new hook
filter:posts.addUserFields
here is a sample.myPlugin.filterPostsAddUserFields = function (hookData, callback) { hookData.fields.push('valueId'); setImmediate(callback, null, hookData); };
That is enough to make
valueId
available totopic.tpl
improve adding new fields to user objects on topic view · Issue #7620 · NodeBB/NodeBB
Right now each post in a topic has a user object that contains these hard-coded fields. If you want to add a custom field to the topic.tpl you have to do something like below myPlugin.filterTopicBuild = function (hookData, callback) { co...
GitHub (github.com)
-
Depending on which template you want to add it to you can use it's
build
hook. If it's topic.tpl then you can usefilter:topic.build
, Here is a sample.myPlugin.filterTopicBuild = function (hookData, callback) { const uids = hookData.templateData.posts.map(p => p.uid); user.getUsersFields(uids, ['valueId'], function (err, valueData) { if (err) { return callback(err); } hookData.templateData.posts.forEach(function (p, index) { if (p) { p.valueId = valueData[index]; } }); callback(null, hookData); }); };
This code would make the
valueId
field available to the template topic.tpl so you can use it with{posts.user.valueId}
-
@baris said in Plugin Dev [newb Question]:
Depending on which template you want to add it to you can use it's
build
hook. If it's topic.tpl then you can usefilter:topic.build
, Here is a sample.myPlugin.filterTopicBuild = function (hookData, callback) { const uids = hookData.templateData.posts.map(p => p.uid); user.getUsersFields(uids, ['valueId'], function (err, valueData) { if (err) { return callback(err); } hookData.templateData.posts.forEach(function (p, index) { if (p) { p.valueId = valueData[index]; } }); callback(null, hookData); }); };
This code would make the
valueId
field available to the template topic.tpl so you can use it with{posts.user.valueId}
Thank you! - I'll get to playing around with this soon. GoT finale and all ha.
Oh question, what if the tpl is custom? Do I need to do anything diffrent than above other than changing the tpl file name?
Well anyrate ill ask stuff once i play with it for a bit. Thx again. -
@baris said in Plugin Dev [newb Question]:
It works for custom templates as well as long as it is rendered with
res.render('nameOfCustomTpl', data);
Then the hook name becomesfilter:nameOfCustomTpl.build
Just wanted to stop in and say thanks. Got it working!
-
@Joykiller Starting with latest master this is much easier now, all you have to do is use the new hook
filter:posts.addUserFields
here is a sample.myPlugin.filterPostsAddUserFields = function (hookData, callback) { hookData.fields.push('valueId'); setImmediate(callback, null, hookData); };
That is enough to make
valueId
available totopic.tpl
improve adding new fields to user objects on topic view · Issue #7620 · NodeBB/NodeBB
Right now each post in a topic has a user object that contains these hard-coded fields. If you want to add a custom field to the topic.tpl you have to do something like below myPlugin.filterTopicBuild = function (hookData, callback) { co...
GitHub (github.com)