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.
I needed to make sure to delete the existing posts with this specific content type before I could add the new item to the group (Drupal 7 Add Node to Organic Group (OG) programmatically).
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); }



Discussions
Entry not deleting from user other table
HI,
Your code is working fine as it is removing entry from og_membership table. But it is not removing entries from field_data_group_audience and og_users_roles table. Do I need to add something else?
Thanks in advance.
Interesting
I don't think you need to add anything else, but I could be wrong. From looking at the documentation here, you can see all the functions from og.module:
http://drupalcontrib.org/api/drupal/contributions%21og%21og.module/7
If you notice the og_og_membership_delete function that should get called when any og_membership entity is deleted, it appears to be deleting the og_users_roles table data for that specific membership.
http://drupalcontrib.org/api/drupal/contributions%21og%21og.module/funct...
Let me know what you find out or if you figure out what might be missing.
Thanks,
Post new comment