diff --git a/flag_lists.module b/flag_lists.module index d829113..b3c04f0 100644 --- a/flag_lists.module +++ b/flag_lists.module @@ -1587,6 +1587,32 @@ function flag_lists_fix_link(&$link, $action) { return $fcid; } + + /** + * Remove all entries of entity_id and type + * + * @param $entity_id + * Entity id which has been flagged. + * @param $type + * The entity type. + */ + function _flag_lists_remove_entity($entity_id, $type) { + $query = db_select('flag_lists_content') + ->condition('entity_id', $entity_id) + ->condition('entity_type', $type); + $query->fields('flag_lists_content', array('fcid', 'fid', 'uid', 'sid')); + $items = $query->execute()->fetchAll(); + + if ($items) { + foreach ($items as $key => $value) { + db_delete('flag_lists_content') + ->condition('fcid', $value->fcid) + ->execute(); + watchdog('flag_lists', t('Deleted entry @fcid from flat_lists_content', array('@fcid' => $value->fcid))); + } + } + } + /** * Updates the flag count for this content */ @@ -1766,3 +1792,17 @@ function flag_lists_views_form_substitutions() { $select_all_placeholder => drupal_render($select_all), ); } + + +/** + * Implements hook_entity_delete + */ +function flag_lists_entity_delete($entity, $type) { + foreach (flag_get_flags($type) as $flag) { + if (isset($entity->vid)) { + $items = _flag_lists_remove_entity($entity->vid, $type); + } + } +} + +