0

I've been building custom entities for a while now, but have been using the entity construction kit (eck) to build them. This has limitations as it requires you to #1 depend on the package for new installs, and #2, wrap your entities into a feature and hope there aren't conflicts later.

I would like to get into the world of building out the entity types programatically, on module install, as well as removing them on module uninstall.

I was recently diving into an ECK created entity I made that I exported into a feature, and it looks like the feature for the entity type is in the correct format for if you were to programatically create the entity. I have been trying to learn by looking at documentation on how to build a custom fieldable entity (with bundle) for a while, but all the documentation I have found has either been inconsistent, or spread all over the place making it difficult to follow.

My question is, could I in theory copy the field_instance.inc, field_base.inc, and the features.inc (where the entity is defined at), and plug that into my modules hook_install() and create my entity inside of the install file, instead of having to rely on ECK and features? I should note that I am also using the conditional_fields module so that I can dyanmically show/hide certain fields inside of this entity and save time on building my own ajax functionality, so I would probably need to build the entity out so that the fields can be updated from the Fields UI if needed (but usually they'll be updated in hook_update_N.

  • IMO sticking to the core API is the way to go, instead of rely on a middleman to communicate to the API. Generally speaking, yes, defining entities based on the core API should net the same results. I did it plenty in 7. It starts with a hook_schema for the base of the entity, and hook_entity_info to tell Drupal about your new entity. From there, check the Entity API on menu generation part for the backend plumbing. After that, it acts just the way you would see a node type (fieldable, etc). – Kevin Mar 13 at 20:52
  • This is old, but still a great resource how-to so you can ditch ECK: trellon.com/content/blog/creating-own-entities-entity-api – Kevin Mar 13 at 20:55
  • @Kevin thanks for the response. Aside from using it to actually make the entities, I don't use ECK for anything else. I end up using the entity API to manage and create/edit/delete. What I'm stuck on is how to actually build the base entity and entity type so I can use like entity_create('entity_type') with it. All the documentation I've ever found hasn't been very good with explaining what everything does. They all sort of just say 'use this function with these parameters and replace this' – Ex0r Mar 13 at 23:04
  • Check the Entity API module. – Kevin Mar 13 at 23:30
  • The Entity API module didn't help me much, but I did take a look at user and node core modules to get a good idea. I think I have a good understanding now of what I need to do :) – Ex0r Mar 14 at 13:06
0

After some research and some helpful suggestions from another user, I ended up taking a look at the core modules user and node and got a pretty good understanding how to make base entities and attach fields to them. I suggest anybody who is stuck trying to figure out how to do it do the same as once you see it in live working code it's pretty easy to understand.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.