Skip to content

Deindex entries if user is suspended or deleted #277

Answered by janhenckens
zizther asked this question in Q&A
Discussion options

You must be logged in to vote

craft\services\Users has events that run on suspend and unsuspend, both before and after so those should be what you need for this.

To deindex an item you can call the unsearchable() behaviour on it, and to reindex them again if the account is restored can use searchable().

Code-wise that could look like this:

        Event::on(
            Users::class,
            Users::EVENT_AFTER_SUSPEND_USER,
            function (UserEvent $event) {
                $userContent = Entry::findAll(['relatedTo' => $event->user]);
                foreach($userContent as $entry){
                    $entry->unsearchable();
                }
            }
        );

Replies: 2 comments 6 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
6 replies
@janhenckens
Comment options

@zizther
Comment options

@janhenckens
Comment options

@janhenckens
Comment options

@zizther
Comment options

Answer selected by janhenckens
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants