flag_lists-remove_deleted_entity-2114731-2.patch 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. diff --git a/flag_lists.module b/flag_lists.module
  2. index d829113..b3c04f0 100644
  3. --- a/flag_lists.module
  4. +++ b/flag_lists.module
  5. @@ -1587,6 +1587,32 @@ function flag_lists_fix_link(&$link, $action) {
  6. return $fcid;
  7. }
  8. +
  9. + /**
  10. + * Remove all entries of entity_id and type
  11. + *
  12. + * @param $entity_id
  13. + * Entity id which has been flagged.
  14. + * @param $type
  15. + * The entity type.
  16. + */
  17. + function _flag_lists_remove_entity($entity_id, $type) {
  18. + $query = db_select('flag_lists_content')
  19. + ->condition('entity_id', $entity_id)
  20. + ->condition('entity_type', $type);
  21. + $query->fields('flag_lists_content', array('fcid', 'fid', 'uid', 'sid'));
  22. + $items = $query->execute()->fetchAll();
  23. +
  24. + if ($items) {
  25. + foreach ($items as $key => $value) {
  26. + db_delete('flag_lists_content')
  27. + ->condition('fcid', $value->fcid)
  28. + ->execute();
  29. + watchdog('flag_lists', t('Deleted entry @fcid from flat_lists_content', array('@fcid' => $value->fcid)));
  30. + }
  31. + }
  32. + }
  33. +
  34. /**
  35. * Updates the flag count for this content
  36. */
  37. @@ -1766,3 +1792,17 @@ function flag_lists_views_form_substitutions() {
  38. $select_all_placeholder => drupal_render($select_all),
  39. );
  40. }
  41. +
  42. +
  43. +/**
  44. + * Implements hook_entity_delete
  45. + */
  46. +function flag_lists_entity_delete($entity, $type) {
  47. + foreach (flag_get_flags($type) as $flag) {
  48. + if (isset($entity->vid)) {
  49. + $items = _flag_lists_remove_entity($entity->vid, $type);
  50. + }
  51. + }
  52. +}
  53. +
  54. +