1

I was studying how to work with entities following http://www.bluespark.com/blog/drupal-entities-part-3-programming-hello-drupal-entity.

I downloaded the module, created by the author of that article. Then I wanted to delete entities, using the following code.

entity_delete_multiple('basic', array(1,4) );

But it didn't delete anything. I though that the delete method should be implemented, and added it into the class.

class BasicController extends DrupalDefaultEntityController{

  public function delete($basic) {
    var_dump(1);exit;
    db_delete('basic')
      ->condition('basic_id', $basic->basic_id, '=')
      ->execute();
    field_attach_update('basic', $basic);
    module_invoke_all('entity_delete', 'basic', $basic);
  }
};

It doesn't work: 1 is not printed.

How do I delete an entity with fields?

1

entity_delete_multiple() is implemented by the Entity API module, which is not part of Drupal.

In order to be able to delete entities with entity_delete_multiple() you need to do one of two things.

  • Define a deletion callback for your entity
  • Use a controller class that implements EntityAPIControllerInterface

In fact, this is the code used by entity_delete_multiple().

function entity_delete_multiple($entity_type, $ids) {
  $info = entity_get_info($entity_type);
  if (isset($info['deletion callback'])) {
    foreach ($ids as $id) {
      $info['deletion callback']($id);
    }
  }
  elseif (in_array('EntityAPIControllerInterface', class_implements($info['controller class']))) {
    entity_get_controller($entity_type)->delete($ids);
  }
  else {
    return FALSE;
  }
}

In the first case, you simply define a function similar to the following.

function basic_delete_entity($id) {
  // Delete the entity whose ID is $id.
}

You can use any name for the delete callback, as long as you refer it in your hook_entity_info().

In the second case, you normally sub class EntityAPIController. EntityAPIController::delete() should normally be sufficient.

| improve this answer | |
0

The latter part of your post isn't relevant, but your syntax for entity_delete_multiple() is correct. The first argument is the entity type and the second is an array of numeric IDs.

Be sure the first argument is the entity type and not the bundle name. Also ensure the IDs are valid. That's all there is to it.

| improve this answer | |
  • Add that entity_delete_multiple() is not a Drupal function. – kiamlaluno Feb 8 '15 at 6:15
  • I think what you mean to say is entity_delete_multiple() is not a Drupal CORE function. That's true, but Drupal 7 projects worth their salt use Entity API. – bacteriaman Mar 11 '15 at 0:19
  • The OP doesn't seem aware of that; in fact, he is not even using the controller the Entity API module defines. – kiamlaluno Mar 11 '15 at 13:17

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.