Prevent deletion of an object

As far as I know when an owner of an object is deleted the object is deleted as well. Same for the container of the object. However, we have a payment object that due to accounting requirements must not be deleted even if the payer or the container are deleted .

What is the recommended way to do this?

Thank you.

  • You can use permissions_check:delete hook.

    Something like this:

    elgg_register_plugin_hook_handler('permissions_check:delete', 'object', function (\Elgg\Hook $hook) {
    
        $entity = $hook->getEntityParam();
    
        if (!$entity->getSubtype() == 'payment') {
           return;
        }
    
        //if you use class
        if (!$entity instanceof \MyPlugin\Payment) {
            return;
        }
    
        // Payment should not be deleted
        return false;
    
    });
    As always, all the information is in the documentation.
  • Interesting approach. I'll try it.

    BTW I forgot to mention that I use Elgg 2.3.16. But it has similar hook.

    Thank you very much Nikolai.

  • Interesting approach. I'll try it.

    BTW I forgot to mention that I use Elgg 2.3.16. But it has similar hook.

    Thank you very much Nikolai.

  • Very good.

    Thank you Nikolai

  • depending on the design of the plugin and privacy issue maybe you could look into making the objects owned/contained by the Site. The site is never removed.

    Sometimes this can be a solution.

    The prevent deletion hook is an option. Keep in mind that maybe the Garbage collection plugin can still remove the objects. Check it out.

  • Thank you Jerome.

    making the objects owned/contained by the Site. The site is never removed.

    Yes, I also thought about this solution. It is simpler.

     Keep in mind that maybe the Garbage collection plugin can still remove the objects. Check it out.

    Can you guide me how to check this with the Garbage collection plugin?

    Thanks a lot.

  • Or creating a user just for the purpose of transferring all content formerly owned by accounts to be deleted to. Maybe easier to handle it this way if the content is supposed to be accessible on site as some plugins might rather expect the owner being of type user and not site.

    As for the garbage collection: the plugin searches the database for orphaned data (I think maybe mostly metadata/metastrings). If these database entries have an owner_id pointing to an entity no longer existing it gets removed. That's a rather short explanation (that might even in parts be wrong as I've not looked into the code to see what exactly is done). Anyway, the hook might not help to prevent deletion because the garbage collection would rather use raw MySQL code instead of making use of Elgg API functions (that would call the hooks).

  • Thank you iionly.

    From your answer I understand Jerome's comment about the Garbage collection plugin. The prevent deletion hook can create orphans which may be deleted by the garbage collection. So, this approach is not recommended.

     Or creating a user just for the purpose of transferring all content formerly owned by accounts to be deleted to. Maybe easier to handle it this way if the content is supposed to be accessible on site as some plugins might rather expect the owner being of type user and not site.

    I do not understand this comment. Could you please explain it?

    Do you think that making the objects owned/contained by the Site is not advisable?

    Thank you very much.

     

  • We can put the owner and container in in metadata