Pages

Monday 20 August 2012

Drupal 7 Deleting Organic Group content


Ran into a situation today where I had to delete content from a group programmatically. Basically I have a setup where a group can only have one of a specific type of content posted to their group at any one time. I have it set up so a user can control which one is posted into the group but it needs to restrict them to only allow one.
Here is how I was able to delete all posts of a specific content type in a specific group:
  $content_type = 'my_content_type';
$group_id = 25;
 
$query = db_select('og_membership', 'ogm');
 
$query
->condition('ogm.group_type', 'node', '=')
->condition('ogm.entity_type', 'node', '=')
->condition('ogm.gid', $group_id, '=')
->fields('ogm', array('id'))
->join('node', 'n', "ogm.etid = n.nid AND n.type = :ctype", array(':ctype' => $content_type));
 
$results = $query->execute();
 
$ids = array();
foreach ($results AS $result) {
$ids[] = $result->id;
}
 
if (!empty($ids)) {
og_membership_delete_multiple($ids);
}

No comments:

Post a Comment