JedWatson/keystone — pull request #218 by thedersen (2014-03-10 13:58:50+00:00)
Title: WIP: Allow custom field types to be added, ref #185

Hi,
I just noticed this project, and I think it looks great and have lots of potential. However, I need it to be more pluggable for me to be able to use it, so I thought I might be able to help with that.

This is a rather large pull request, and I understand if you need some time to review it, but here is the gist of it:

* All field types are moved into a new folder, plugins/fields/[fieldType]/index.js
* All templates are moved into a new folder, plugins/fields/[fieldType]/templates/form.jade
* The code and templates are in the same directory to make it easier to distribute plugins
* I have added an `extend` method to field.js to make it easier to create custom fields (see example below)
* All field types are loaded dynamically from plugins folder inside keystone
* If you want to add a custom field type, put it inside your plugins/fields/[fieldType] directory of your application, and it will be available to use
* Everything should be backwards compatible

This is still work in progress, but I thought I would get your input before spending too much time on it.

What's remaining?

* Loading plugins from npm. This should be trivial at this stage, but we need to agree on some conventions on how to package and naming the plugins etc.
* We need to look at how processFilters and getSearchFilters are done (this is currently inside list.js, but should be moved into the field types)
* Testing... (I have not tested all the field types after this change)
* Probably something more

Example:

``` js
// Field and keystone are injected into the
// factory method, so that you do not need to add a
// reference to keystone when publishing your custom
// field types on for instance npm.
module.exports = function(FieldBase, keystone) {
return FieldBase.extend({
constructor: function(list, path, options) {
// Do something
// Then call base
FieldBase.apply(this, arguments);
},

someMethod: function(item, data) {
}
});
};
```
Hope you appreciate this, and that we can discuss further development on this issue.

Thomas