/**
 * Implements hook_entity_delete().
 *
 * Delete group membership of deleted entity.
 */
function og_entity_delete($entity, $type) {
  $wrapper = entity_metadata_wrapper($type, $entity);
  if (!isset($wrapper->og_membership)) {
    return;
  }
  foreach ($wrapper->og_membership->value() as $og_membership) {
    $og_membership->delete();
  }
}

In the above code OgMemerbship::delete() is called.

If I change the line to be foreach ($wrapper->og_membership as $og_membership) {
where $og_membership is a wrapper, Entity::delete() is called instead of OgMemerbship::delete().

Comments

fago’s picture

Status: Active » Closed (works as designed)

you mean entity_delete_multiple() I guess.
Deletion always goes through the controller, but not through Entity:delete(), because of multiple deletion. Thus, for modifying delete behaviour you have to override the controller.

amitaibu’s picture

I do override the delete, that's why $og_membership->delete() works ok; but $wrapper->delete() for some reason doesn't -- or am I missing your point ;)


/**
 * Main class for Group membership entities provided by Entity API.
 */
class OgMembership extends Entity {
  public function __construct(array $values = array(), $entityType = NULL) {
    parent::__construct($values, 'og_membership');
  }

  public function delete() {
    parent::delete();
    og_group_membership_invalidate_cache();
  }
}
amitaibu’s picture

> but $wrapper->delete() for some reason doesn't -- or am I missing your point ;)

Ok, I've seen what it's doing -- it calls the controller class not the entity class. Took me some time to get back to this issue ;)