Hello there, I am trying to create a custom module of utilities. One of the functionalities of this module is the ability to hit the endpoint with a post request containing data, and use that data to create an entity of a custom type. I currently have a custom entity of type "testentity", and I tried the following code:

function save_cust_entity() {

    entity_create('testentity', $_POST);

}

However this results in backdrop throwing the following error:
 

My question is, how do I point entity_create to my custom entity type? Thanks.

Accepted answer

Hi colbycat. 

Creating custom entity involves a few steps:

  1. Implementing hook_entity_info() to define the entity, its keys (id, label, bundle, etc)  its classes and its UI path, etc
  2. Defining the schema of the entity with hook_schema() so that Backdrop can save the entity values to the DB
  3. Creating the CRUD functions that will create, read, update and delete the entity.
  4. Creating menu items to build and serve the forms used to fill in the values for the entity. If the entity is fieldable, then you have to also use field_attach_form() to attach the field widgets to the form

     

Now, if you just want to use entity_create(), be sure to fully complete step 1 above in a custom module, and to provide a bundle in entity_create() as in:

entity_create('testentity', array('bundle' => [MY_BUNDLE]));

Instead of the key 'bundle' you need to use whatever key your entity uses for bundles (for example, for nodes, this is 'type', for taxonomy terms, 'vocabulary', etc).

So... not a super easy task, but not too bad. I would suggest that you install and study the example contrib module Basic Entity Plus Example, which shows how to do all of the above.

Comments

Hi colbycat. 

Creating custom entity involves a few steps:

  1. Implementing hook_entity_info() to define the entity, its keys (id, label, bundle, etc)  its classes and its UI path, etc
  2. Defining the schema of the entity with hook_schema() so that Backdrop can save the entity values to the DB
  3. Creating the CRUD functions that will create, read, update and delete the entity.
  4. Creating menu items to build and serve the forms used to fill in the values for the entity. If the entity is fieldable, then you have to also use field_attach_form() to attach the field widgets to the form

     

Now, if you just want to use entity_create(), be sure to fully complete step 1 above in a custom module, and to provide a bundle in entity_create() as in:

entity_create('testentity', array('bundle' => [MY_BUNDLE]));

Instead of the key 'bundle' you need to use whatever key your entity uses for bundles (for example, for nodes, this is 'type', for taxonomy terms, 'vocabulary', etc).

So... not a super easy task, but not too bad. I would suggest that you install and study the example contrib module Basic Entity Plus Example, which shows how to do all of the above.