XF 2.2 If delete user delete also IMG in data

mcatze

Well-known member
For my membermap add-on is create for every user a minimap in the data path. Now i want to delete these user-based image when the user will be deleted.
Is there a proper way to do this? I tried it with extending userDeleteCleanInit(), but it doesn't work.

I have a existing function for deleting these images as Service and it works fine as a action in ACP when i edit a user.

Can someone give me a hint? I am not so familiar with XF2 coding.
 
Extend XF\Entity\User _postDelete() to add your deletion method, for example:

PHP:
$memberMap = $this->app()->service('\YourAddonId\MemberMap:User\MemberMap', $this);
$memberMap->deleteMemberMapForUserDelete();
 
Last edited:
For my membermap add-on is create for every user a minimap in the data path. Now i want to delete these user-based image when the user will be deleted.
Is there a proper way to do this? I tried it with extending userDeleteCleanInit(), but it doesn't work.

I have a existing function for deleting these images as Service and it works fine as a action in ACP when i edit a user.

Can someone give me a hint? I am not so familiar with XF2 coding.

The approach Lawrence suggested will definitely work and would let you use the service you've already coded, but since you mentioned userDeleteCleanInit() you might also want to consider using the corresponding code event listener instead. This will allow you to avoid adding a class extension for a very small change. If you're unfamiliar with code events in XF, check out this explanation: https://xenforo.com/docs/dev/criteria/#adding-code-event-listener.

In order to utilize the event, you'll need to set up a Listener class containing a static function called userDeleteCleanInit() as well as a user_delete_clean_init code event listener in the ACP. Pay close attention to the function signature---the $deletes array must be passed by reference so your listener method can add elements to the array. For example your class might look something like this:

Code:
<?php

namespace Your\Addon;

class Listener
{
    public static function userDeleteCleanInit(
        \XF\Service\User\DeleteCleanUp $deleteService,
        array &$deletes
    ) {
        $deletes['your_addon_table_1'] = 'user_id = ?';
        $deletes['your_addon_table_2'] = 'user_id = ?';
        // etc.
    }
}
 
@Zig Thanks for your explanation. This part is done so far. I only have problems with the deleting of the images.
 
  • Like
Reactions: Zig
And it is easier as i thought. I usually extended the XF\Entity\User for some other reasons and i was wrong with my explanation of the existing function. I set these function in the extended entity and so i have just four rows to write..
PHP:
protected function _postDelete()
{
    parent::_postDelete();

    return $this->removeMinimapIfExists();
}

Sometimes i think too complicated. But thanks you both for helping me.
 
Last edited:
This part is done so far. I only have problems with the deleting of the images.

The add-on I am currently creating stores images in the data directory for users. The above service example is what I use to delete the images when a user is deleted: \XF\Util\File::deleteFromAbstractedPath($this->user->Profile->getAbstractedMessageBannerPath($code));

In my extended user profile entity:

PHP:
    public function getAbstractedMessageBannerPath($size)
    {
        $userId = $this->user_id;

        return sprintf('data://eae_message_banners/%s/%d/%d.jpg',
            $size, floor($userId / 1000), $userId
        );
    }
 
@Lawrence Thats the way we (@Hoffi ) did this with the minimap. Also the function for deleting the image was created. It was just the little part when a user is deleted also delete associated images. (y)
 
Back
Top Bottom