How to add support for a custom entity?
Is there a way (hooks) to add support for a custom entity type in jDurpal?
You can use entity_load(), entity_save() and entity_delete() with jDrupal and it should do most of it for you. For any custom entity, you do need to tell jDrupal about the primary key though. For example, to support Commerce Orders, I needed to add this function:
function commerce_order_primary_key() {
return 'order_id';
}
That should be it. Then I typically make a wrapper function, for example:
function commerce_order_load(order_id, options) {
entity_load('commerce_order', order_id, options);
}
Then callers can more easily use it:
function foo() {
commerce_order_load(123, {
success: function(order) {
console.log(order);
}
});
}
I'll also make wrappers for save/delete following the same guidelines.
Thanks. I have tried this, but I get an error when loading an entity:
WARNING: entity_load - unsupported type: my_entity_type
Do I have to add my custom entity to entity_types()?
@luxio It looks like we'll need some type of hook to allow people to declare an entity type:
https://github.com/easystreet3/jDrupal/blob/7.x-1.x/src/entity.js#L547
Right now it's hard coded in for core entity types. I'd love it if it was dynamic and automatically knew what was on the Drupal site. Thoughts?
What about just redefining entity_types() in the custom code? Something like this:
// redefine entity_types()
var core_enity_types = entity_types;
entity_types = function() {
return core_entity_types().concat('my_entity_type');
}
// load wrapper.
function my_entity_type_load(order_id, options) {
entity_load('my_entity_type', my_entity_id, options);
}
@kentr excellent, very clever. That'll work.
Cleaner version, based on this example
// Decorate entity_types().
entity_types = (function() {
var core_types = entity_types;
return function() {
return core_types.apply(this, arguments).concat('my_entity_type');
}
})();
// load() wrapper.
function my_entity_type_load(order_id, options) {
entity_load('my_entity_type', my_entity_id, options);
}
FYI, jDrupal can now utilize Services Entity to support all entity types (core and custom): https://github.com/signalpoint/jDrupal/issues/60