Vulcan/packages/nova-categories/lib/custom_fields.js

46 lines
1.2 KiB
JavaScript

import PublicationUtils from 'meteor/utilities:smart-publications';
import Posts from "meteor/nova:posts";
import Users from 'meteor/nova:users';
import Categories from "./collection.js";
// check if user can create a new post
const canInsert = user => Users.canDo(user, "posts.new");
// check if user can edit a post
const canEdit = Users.canEdit;
const alwaysPublic = user => true;
Posts.addField(
{
fieldName: 'categories',
fieldSchema: {
type: [String],
control: "checkboxgroup",
optional: true,
insertableIf: canInsert,
editableIf: canEdit,
viewableIf: alwaysPublic,
form: {
noselect: true,
type: "bootstrap-category",
order: 50,
options: function () {
var categories = Categories.find().map(function (category) {
return {
value: category._id,
label: category.name
};
});
return categories;
}
},
publish: true,
join: {
joinAs: "categoriesArray",
collection: () => Categories
}
}
}
);
PublicationUtils.addToFields(Posts.publishedFields.list, ["categories"]);