RSS

Newsletter

The CodeKarate Newsletter is the best way for technical ninjas to keep their swords sharpened. Don't worry, we won't flood your inbox and your email address will always remain private.
Back to Top

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

1
subhash (not verified)

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.

1-1
shane

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,

shane's picture

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <h2> <h3> <blockquote> <img>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <css>, <html>, <php>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <mysql>, <python>, <ruby>. PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.