Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add support for a custom entity? #47

Open
luxio opened this issue Sep 2, 2016 · 7 comments
Open

How to add support for a custom entity? #47

luxio opened this issue Sep 2, 2016 · 7 comments

Comments

@luxio
Copy link

luxio commented Sep 2, 2016

Is there a way (hooks) to add support for a custom entity type in jDurpal?

@luxio luxio changed the title How t add support for new entity? How to add support for a custom entity? Sep 2, 2016
@signalpoint
Copy link
Owner

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.

@luxio
Copy link
Author

luxio commented Sep 3, 2016

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()?

@signalpoint
Copy link
Owner

@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?

@kentr
Copy link

kentr commented Sep 3, 2016

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);
}

@signalpoint
Copy link
Owner

@kentr excellent, very clever. That'll work.

@kentr
Copy link

kentr commented Sep 3, 2016

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);
}

@signalpoint
Copy link
Owner

FYI, jDrupal can now utilize Services Entity to support all entity types (core and custom): #60

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants